Presentation is loading. Please wait.

Presentation is loading. Please wait.

Windows Communication Foundation (“Indigo”): Writing Reliable and Transacted Distributed Applications Shy “BAD-P” Cohen COM307 Program Manager Connected.

Similar presentations


Presentation on theme: "Windows Communication Foundation (“Indigo”): Writing Reliable and Transacted Distributed Applications Shy “BAD-P” Cohen COM307 Program Manager Connected."— Presentation transcript:

1 Windows Communication Foundation (“Indigo”): Writing Reliable and Transacted Distributed Applications Shy “BAD-P” Cohen COM307 Program Manager Connected Systems Division Microsoft Corporation

2 2 Moving To Service Orientation Threads versus Messages BAD P! BAD, BAD P! Why don’t you come back? Please hurry… ♫ Keeping it Consistent All together now!

3 3 Challenges Of Implementing Reliable Distributed Systems Communication Issues Network unavailable Connection drops Network loses messages Messages may arrive out of order Messages may take different paths Retried messages may arrive after the ones that followed them Processing Issues Messages lost when processing fails Interrelated Messages processed individually Failure may leave the distributed system in an inconsistent state Messages can’t be retried without side effects Retried messages arriving twice, reordering, etc.

4 4 Making It Reliable Reliable Sessions Transactions Queues

5 5 Sessions Channel Service Instance Channel Proxy SessionSession

6 6Appendix Sessions: Managing Lifetime [ServiceContract(Session=true)] public interface ICounter { [OperationContract(/*IsInitiating=true, IsTerminating=false*/)] int Initialize(int value); [OperationContract(IsIntiating=false)] int Add(int value); [OperationContract(IsInitiating=false, IsTerminating=true)] int Done(); }

7 7 Sessions: Managing State [ServiceContract(Session=true)] public interface IEchoContract { [OperationContract] int Echo(string input); } [ServiceBehavior(InstanceMode=InstanceMode.PrivateSession)] public class EchoService: IEchoContract { int i=0; public string Echo(string input) { Console.WriteLine(input); return i++; } ۞ Calls to Echo will initiate the session and the session will terminate when the client closes the channel. ۞ To share a session between two or more clients use SharedSession. ۞ ۞Appendix

8 8 Session State And Lifetime

9 9 Reliable Sessions Assurances Messages are delivered exactly once, in the same order as they were sent Alternatively, you can choose to have them delivered in order in which they were received Resilient to Transport disconnections SOAP or transport intermediary failures Features Connection verification and maintenance Congestion and flow control

10 10 Reliable Sessions Enabling Provided on Standard Bindings netTcpBinding netTcpBinding (off by default) wsHttpBinding wsHttpBinding (off by default) wsDualHttpBinding wsDualHttpBinding (always on) Can be added to any custom binding

11 11 Ordered And Unordered Reliable Sessions

12 12 Reliable Sessions: Binding Rqmts [BindingRequirements(RequireOrderedDelivery=true)] [ServiceBehavior(InstanceMode=InstanceMode.PrivateSession)] public class EchoService: IEchoContract { int i=0; public string Echo(string input) { Console.WriteLine(input); return i++; } ۞ Also satisfied by the TCP and Named Pipes transports ۞Appendix

13 13 Keeping It Consistent Atomic Transactions versus Compensation Trading off coupling and complexity Atomic Transactions: simpler, tighter coupling Compensation: more complex, looser coupling Both have their place Choose the right model for the situation

14 14 Transactions: Participation [ServiceContract] public interface IMyContract { [OperationContract] [TransactionFlow(TransactionFlowOption.Required)] bool Transfer1(Account from, Account to, decimal amount); [OperationContract] [TransactionFlow(TransactionFlowOption.Required)] bool Transfer2(Account from, Account to, decimal amount); } Interface Definition ۞ The options are Allowed, NotAllowed, and Required ۞Appendix

15 15 Transactions: Interaction [BindingRequirements( TransactionFlowRequirements=RequirementsMode.Require)] [ServiceBehavior( TransactionAutoCompleteOnSessionClose = true, ReleaseServiceInstanceOnTransactionComplete = true)] public class MyService: IMyContract { [OperationBehavior( TransactionScopeRequired = true, TransactionAutoComplete = true)] public bool Transfer1(Account from, Account to, decimal amount) {... } [OperationBehavior( TransactionScopeRequired = true, TransactionAutoComplete = false)] public bool Transfer2(Account from, Account to, decimal amount) {... OperationContext.Current.SetTransactionComplete(); } Service Programmer Appendix

16 16 Transactions: Usage TransactionScope transaction; using (scope = new TransactionScope()) { proxyForServiceOne.Transfer1(AccountOne, AccountTwo, amount); proxyForServiceTwo.Transfer1(AccountThree,AccountFour,amount); UpdateLocalCache(AccountOne, AccountTwo, amount); scope.Complete(); } Client Programmer Appendix

17 17 Transactions: Control <binding configurationName="SampleBinding“ transactionFlow=“true" /> Service Administrator Appendix

18 18 Transacted Operations

19 19 Transactions Summary Service Developer (Code) Declares willingness to participate in an incoming (client initiated) transaction on contract operations No transaction sharing on one-way operations! * Specified implementation behavioral aspects at the service and method level Client Developer (Code) Creates transactions Includes services & local TX-ed resources in their scope Service Administrator (Config) On/Off switch controls TX flow support for an endpoint It’s about Trust

20 20 Queues Increase availability Mask network or service unavailability Support scale out Multiple readers from a single queue Provide load leveling Handle average, not peak load Are a building block for compensating business transactions Reliable, durable messaging to capture distributed state changes Need to compensate for errors

21 21 Queues Make one-way operations possible even when the service isn’t available Supported via MSMQ Programmer uses regular WCF channels IT Pro manages the familiar MSMQ service MSMQMSMQ ClientService

22 22 Queues: Basic Consumption [ServiceContract] interface IMessageDisplayer { [OperationContract(IsOneWay=true)] void DisplayText(string text); } [BindingRequirements( QueuedDeliveryRequirements=BindingRequirementsMode.Require)] class Displayer : IMessageDisplayer { public void DisplayText(string text) {…} } Service programmer Defines ordinary one-way operations Optionally demands queued delivery Appendix

23 23 Queues: Basic Configuration <endpoint address = "net.msmq://MyServer/private/MyQueue/" binding = "netMsmqBinding" contract = "QueuedMessaging.IMessageDisplayer, QueuedMessaging" /> Service administrator configures endpoint ۞ Equivalent public queue address: “net.msmq://MyServer/MyQueue/" ۞Appendix

24 24 Basic Queued Messaging

25 25 Queues Composing with TXs Operation requires 2 transactions TX between client and queue to send Another TX between service and queue to receive If a transaction aborts Message sent under the TX are discarded Messages received under the TX are retried

26 26 Queues MEPs Datagrams Standalone messages Can be sent and received with or without using a transaction “Exactly Once” or “Best Effort” delivery “Sessiongram” A grouping of related messages Always processed together under a single TX “Exactly Once, In Order” delivery

27 27 Queues: Sessions & TXs [ServiceContract(Session=true)] public interface: IOrders{…} [BindingRequirements( QueuedDeliveryRequirements=RequirementsMode.Require)] [ServiceBehavior(InstanceMode = InstanceMode.PrivateSession)] class PurchasingService : IOrders { [OperationBehavior( TransactionScopeRequired = true, TransactionAutoComplete = false)] public void OpenPurchaseOrder(string name) {…} [OperationBehavior( TransactionScopeRequired = true, TransactionAutoComplete = false)] public void AddLineItem(string item, float price, int quantity) {…} [OperationBehavior( TransactionScopeRequired = true, TransactionAutoComplete = true)] public void EndPurchaseOrder(int totalItems, float totalPrice) {…} } Service programming Appendix

28 28 Queues: Sessions & TXs TransactionScope scope; using (scope= new TransactionScope()) { using (OrdersProxy orderd = new OrdersProxy("ServiceEndpoint") { orders.OpenPurchaseOrder("Order #1"); orders.AddLineItem("Blue widget", 1.5f, 2); orders.AddLineItem("Red widget", 2f, 2); orders.EndPurchaseOrder(4, 7); orders.Close(); } scope.Complete(); } Client programming Appendix

29 29 Queues, Transactions, And Sessions

30 30 Queues The dead letter queue Stores messages that could not be delivered Can be processed like any other queue New in Windows Vista: User specified DLQ If not specified, the default is the system-wide DLQ With a shared DLQ, processing service needs to implement all the contracts for all the messages going into it Alternatively, use System.Messaging to process messages from the Dead Letter Queue

31 31 Queues Poison messages Messages for whom processing fails, repeatedly, are considered “poison” Failure = the processing transaction aborted Message properties indicate how many times a message was delivered MsmqMessageProperty msmqMessageProperty; msmqMessageProperty = OperationContext.Current.IncomingMessageProperties[MsmqMessageProperty.Name] as MsmqMessageProperty; if (prop.TotalAbortCount > myRetryThreshold) { /* Do something */ }

32 32 Queues Poison messages New in Windows Vista: Automatic protection against repeated failures By limiting NUMBER of retries By controlling HOW retries are done <binding configurationName=“QB" maxImmediateRetries="2" retryCycleDelay="0:0:10" maxRetryCycles="3" rejectAfterLastRetry="false" /> 10 seconds between attempts 3 retry cycles 2 retries To Poison Message Queue 1 st attempt

33 33 Q’s Failure compensation Set up compensation services on the sending and receiving sides <binding configurationName="MyQueueBinding“... timeToLive="0:2:0" deadLetterQueue= "net.msmq://MyClient/private/myCustomDLQ" /> <endpoint address ="net.msmq://MyServer/private/MyQueue;poison/” bindingSectionName="netProfileMsmqBinding" bindingConfiguration ="MyQueueBinding" contractType="Queue.IPurchaseOrder, Queues" />

34 34 When Faced With Challenges WCF provides sophisticated session mgmt Session lifetime and associated state WCF assures … that messages arrive exactly once … in the order in which they were sent WCF provides transacted state management Computation and communication WCF provides high availability Durability, atomicity, and error handling

35 35 Community Resources At the PDC More on Windows Communication Foundation Attend the other WCF presentations Talk to me and the other WCF folks at the Lounge, Ask The Experts, or at any other time More on System.Transactions Go talk to Max (COM lounge) or Jim (FUN lounge) Attend FUN320 tomorrow Do the HOLs: WCF HOLs, FUN HOL13 (TXF & TXR) After the PDC MSDN dev center: http://msdn.microsoft.com/webservices/ MSDN Forums Channel 9 tag: http://channel9.msdn.com/tags/Indigo Contact me: Shy.Cohen@Microsoft.com

36 © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Download ppt "Windows Communication Foundation (“Indigo”): Writing Reliable and Transacted Distributed Applications Shy “BAD-P” Cohen COM307 Program Manager Connected."

Similar presentations


Ads by Google