12. Four Tenets of Service Orientation SERVICE ORIENTATION Compatibility Based On Policy Share Schema & Contract, Not Class Services Are Autonomous Boundaries Are Explicit
27. Service Contract An Opt-In Model [ServiceContract] public interface ICalculator { [OperationContract] int DoMath(int a, int b, string op); // Not exposed as part of the external contract :-) void MethodRequiredForImplementation(bool b); }
28. Operations Types Message DoXmlMath( Message m); Untyped (“Universal”) MathResponse DoMsgMath( MathRequest msg); int DoParamsMath( int a, int b, string op ); Typed Message Parameterized Operations (shorthand for TM)
29. Service Contract: Names [ServiceContract( Namespace ="http://TechEd.WCF.Intro")] public interface IGreetings { [OperationContract( Name =“SayHello", Action ="http://TechEd.WCF.Intro/HelloRequest", ReplyAction ="http://TechEd.WCF.Intro/HelloResponse")] string Greet(string name); [OperationContractAttribute( Action = "*" )] void UnrecognizedMessageHandler( Message msg ); } class GreetingService : IGreetings { public string Greet(string name) { return “Hello, " + name; } public void UnrecognizedMessageHandler(Message msg) { Console.WriteLine("Unrecognized message: " + msg.ToString()); } }
30.
31. Service Contract: OneWay [ServiceContract] public interface IOneWayCalculator { [OperationContract( IsOneWay=true )] void DoMath(MathRequest request); }
32. Service Contract: Duplex [ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(ICalculator Results ) ] public interface ICalculatorProblems { [OperationContract(IsOneWay=true)] void DoMath(MathRequest request); } public interface ICalculatorResults { [OperationContract(IsOneWay=true)] void DisplayResults(MathResponse response); }
33. Service Contract: Faults t ry { return n1 / n2; } catch (DivideByZeroException e) { MyMathFault f = new MyMathFault (n1, n2); FaultReason r = new FaultReason("Divide By Zero"); throw new Fault Exception < MyMathFault >( f, r ); } [ServiceContract(Session=true)] public interface ICalculator { [OperationContract] [FaultContract(typeof( MyMathFault ))] int DoMath(int a, int b, string op); }
34. Service Contract: Versioning [ServiceContract] public interface ICalculator2 : ICalculator { [OperationContract(IsOneWay=true)] void SolveAndStoreProblem (ComplexProblem p); }
35. Service Contract: Streams [ServiceContract ] public interface IStreamCalculator { [OperationContract] Stream Fibonacci(int iterations); }
36. Data Contract [DataContract] public enum Position { [ EnumMember ] Employee, [ EnumMember ] Manager, [ EnumMember( Value = “ Vendor" ) ] Contractor, NotASerializableEnumeration }
37. Data Contract: Names [DataContract( Name=“Complex”, Namespace=“http://BigMath.Samples” )] public class ComplexNumber { [DataMember( Name=“RealPart” )] public double Real = 0.0D; [DataMember( Name=“ImaginaryPart” )] public double Imaginary = 0.0D; public ComplexNumber(double r, double i) { this.Real = r; this.Imaginary = i; } }
38. Data Contract: Enumerations [DataContract] public enum Position { [EnumMember] Employee, [EnumMember] Manager, [EnumMember(Value = “Vendor")] Contractor, NotASerializableEnumeration }
39. Message Contract [MessageContract] public class ComplexProblem { [MessageHeader(Name="Op", MustUnderstand=true)] public string operation; [MessageBodyMember] public ComplexNumber n1; [MessageBodyMember] public ComplexNumber n2; [MessageBodyMember] public ComplexNumber solution; // Constructors… }
40. Mapping Contracts (1/2) [ MessageContract ] public class MyRequest { [MessageHeader] public int Amount; [MessageBody] public ItemInfo Info; } [ DataContract ] public class ItemInfo { [DataMember] public int ID; [DataMember] public double Cost; [DataMember] public string Name; } wsdl:message wsdl:part xsd:element
43. Bindings & Binding Elements Transport IPC MSMQ Custom TCP HTTP Protocol Encoders .NET TX Custom Security Reliability Binding HTTP TX Security Reliability Text Text Binary Custom TCP Binary
44.
45. System-Provided Bindings N = None | T = Transport | M = Message | B = Both | RS = Reliable Sessions Binding Interop Security Session TX Duplex BasicHttpBinding BP 1.1 N, T N N n/a WSHttpBinding WS M , T, X N , T, RS N , Yes n/a WSDualHttpBinding WS M RS N , Yes Yes WSFederationBinding Federation M N , RS N , Yes No NetTcpBinding .NET T , M T ,RS N , Yes Yes NetNamedPipeBinding .NET T T , N N , Yes Yes NetPeerTcpBinding Peer T N N Yes NetMsmqBinding .NET T , M, X N N , Yes No MsmqIntegrationBinding MSMQ T N N , Yes n/a
49. Choosing Bindings Any Protocol Any Binding Any Binding WCF WCF MSMQ WCF COM+/ES WCF MSMQ Protocol WS-* Protocols MSMQ Binding Moniker ASMX/WSE3 WCF WS-* Protocols Http/WS Binding Other Platform WCF WS-* Protocols Http/WS Binding
50.
51.
52. Behaviors Client-Side Behaviors Service-Side Behaviors Service Caller Be Be C B A C B A A B C C B A
53.
54. Example: Security Service Client Be Be Bindings Move Claims in Messages Behaviors Implement Security Gates Behaviors Provide Credentials C B A C B A A B C C B A
55. Example: Transactions Service Caller Be Bindings Flow Transactions Behaviors AutoEnlist and AutoComplete C B A C B A A B C C B A