420 likes | 668 Views
Introduction to WCF Architecture and Extensibility. Risman Adnan ISV Lead, Microsoft Indonesia rismana@microsoft.com http://geeks.netindonesia.net/blogs/risman. My Motivation. Explain how WCF works Give you 1 st experience with WCF & VS 2008 Duplex / Request reply session .
E N D
Introduction to WCFArchitecture and Extensibility Risman Adnan ISV Lead, Microsoft Indonesia rismana@microsoft.com http://geeks.netindonesia.net/blogs/risman
My Motivation • Explain how WCF works • Give you 1st experience with WCF & VS 2008 • Duplex / Request reply session
Our problem Volume of Software Increases Software Ability to Adapt Decreases!!! Time
WCF in a Nutshell Address Binding Contract Behavior Request/Response InstancingBehavior HTTPTransport WS-SecurityProtocol ConcurrencyBehavior http://... Peer Transport net.p2p://... Throttling Behavior WS-RMProtocol TCP Transport net.tcp://... MetadataBehavior NamedPipeTransport WS-CoordProtocol One-Way net.pipe://... Error Behavior TransactionBehavior MSMQTransport DuplexChannel net.msmq://... CustomBehavior SecurityBehavior Duplex CustomTransport CustomProtocol xxx://... Externally visible, per-endpoint Opaque, per-service, endpoint, or operation
C C C B B B A A A ArchitectureProgramming Model Client Service Bv Bv Message Bv Bv Address Binding Contract (Where) (How) (What)
C C B B A A ArchitectureLayering Service Bv Bv
C C C B B B A A A Architecture: Description & Runtime Client Service Bv Bv Message Bv Bv ChannelFactory<T> ServiceHost Description Runtime Description Runtime
Type Loader Programming Model Description Config Config Loader
WSDL/Policy Metadata Import Metadata Export Type Loader Description Runtime Programming Model Runtime Builder Description Code Spit Config Gen Config Loader Config
Description ServiceDescription ServiceEndpoint * Binding ContractDescription OperationDescription * * 1 to n
Description ServiceDescription public class CalculatorService : ICalculator { ... } Name Namespace Config Name Service Type Behaviors
Description ServiceDescription <system.serviceModel> <services> <service name="CalculatorService"> <endpoint address="http://..." binding="wsHttpBinding” contract="ICalculator" /> </service> </services> </system.serviceModel> ServiceEndpoint * EndpointAddress ListenURI Behaviors
Description <system.serviceModel> <services> <service name="CalculatorService"> <endpoint address="http://..." binding="customBinding” bindingConfiguration="RelHttp" contract="ICalculator" /> </service> </services> <customBinding> <binding name=“RelHttp" > <reliableSession/> <httpTransport/> </binding> </bindings> </system.serviceModel> ServiceDescription <system.serviceModel> <services> <service name="CalculatorService"> <endpoint address="http://..." binding="wsHttpBinding” contract="ICalculator" /> </service> </services> </system.serviceModel> ServiceEndpoint * Binding Name BindingElement[]
Description ServiceDescription [ServiceContract(Namespace=“...")] public interface ICalculator { [OperationContract] double Add(double n1, double n2); [OperationContract] double Sub(double n1, double n2); } ServiceEndpoint * Binding ContractDescription Session ContractName Namespace Behaviors
Description ServiceDescription [ServiceContract(Namespace=“...")] public interface ICalculator { [OperationContract] double Add(double n1, double n2); [OperationContract] double Sub(double n1, double n2); } ServiceEndpoint * Binding OperationDescription * ContractDescription Name IsOneWay Behaviors … OperationDescription *
ArchitectureChannel Stack Client Dispatcher Protocol2 Protocol2 Protocol1 Protocol1 Transport Transport
Description ServiceDescription <system.serviceModel> <!–- endpoints configured here --> <bindings> <customBinding> <binding name=“ReliableHttp" > <reliableSession/> <httpTransport/> </binding> </bindings> </system.serviceModel> CustomBinding binding = new CustomBinding(); binding.Name = “ReliableHttp”; binding.Elements.Add( new ReliableSessionBindingElement()); binding.Elements.Add( new HttpTransportBindingElement()); //create a channel listener or //create a ServiceEndpoint ServiceEndpoint * Binding Name BindingElement[]
User Bindings public class MyReliableHttpBinding : Binding { // BindingElement members // ctor initializing BindingElements // any properties you want to expose as settings // override Scheme public override BindingElementCollection CreateBindingElements() { BindingElementCollectionbindingElements = new BindingElementCollection(); bindingElements.Add(this.httpsTransport); bindingElements.Add(this.reliableSession); return bindingElements.Clone(); } }
CommunicationObject • Predictable state transitions • Unified communication resource model • Default implementation • Events • Timeouts • Exception Contract Created Opening Opened Closing Closed Faulted
Bindings, Listeners, Factories, and Channels Binding Binding BuildChannel Factory(Uri) BuildChannel Listener(Uri) Open() Open() ChannelListener ChannelListener ChannelListener ChannelListener ChannelListener ChannelFactory Create Channel() Accept Channel() Open() Open() Channel Channel Send() Receive()
ServiceHost • Constructor: Service Type • Features • AddServiceEndpoint() overloads • Description • CommunicationObject • TimeOuts: Open, Close • Events • Open()/Close()/Abort()
Working With ServiceHost • “Outside-In” • new ServiceHost() • Mess with it directly • Open() it • “Inside-Out” • Subclass ServiceHost • Implement OnOpening() • new ServiceHost() • Open() it
ServiceHost.ctor 1. Reflect Over T .ctor(…); ServiceDescription [ServiceBehavior( ConcurrencyMode = ConcurrencyMode.Multiple)] public class CalculatorService : ICalculator { [OperationContract] [OperationBehavior(TransactionScopeRequired=true)] double Add(double n1, double n2){...} } ContractDescription OperationDescription *
1. Reflect Over T ServiceHost.ctor .ctor(…); ServiceDescription 2. Load Config <system.serviceModel> <services> <service name="CalculatorService" behaviorConfiguration="Bv1"> <!-- endpoint config --> </service> </services> <behaviors> <serviceBehaviors> <behavior name=“Bv1"> <serviceMetadata httpGetEnabled="True"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> ServiceEndpoint * Binding <system.serviceModel> <services> <service name="CalculatorService"> <endpoint address="http://..." binding="wsHttpBinding” contract="ICalculator" /> </service> </services> </system.serviceModel> ContractDescription OperationDescription *
1. Reflect Over T 2. Load Config .Description { get; } ServiceHost .ctor(…); 3. Build Runtime .Open();
ServiceDescription ServiceDescription ServiceEndpoint * Binding ServiceEndpoint ContractDescription Binding OperationDescription * ContractDescription OperationDescription OperationDescription OperationDescription
ServiceDescription ChannelDispatcher * ServiceEndpoint EndpointDispatcher ContractDescription Binding DispatchRuntime OperationDescription DispatchOperation OperationDescription DispatchOperation OperationDescription DispatchOperation Add(…) Sub(…) Mul(…)
ServiceDescription ServiceDescription ChannelDispatcher * ChannelDispatcher * ServiceEndpoint EndpointDispatcher ServiceEndpoint ServiceEndpoint EndpointDispatcher ContractDescription Binding DispatchRuntime ContractDescription Binding DispatchRuntime OperationDescription DispatchOperation OperationDescription DispatchOperation OperationDescription DispatchOperation OperationDescription DispatchOperation OperationDescription DispatchOperation OperationDescription DispatchOperation Add(…) Sub(…) Mul(…)
1. Reflect Over T 2. Load Config .Description { get; } .Open(); ServiceHost .ctor(…); 3. Build Runtime
Behaviors [MyBehavior] 1. Reflect Over T .ctor(…); <myBehavior /> 2. Load Config .Description.Behaviors.Add(new MyBehavior()); .Description 3. Build Runtime 4. Apply Behaviors Behaviors .Open(); 5. Open Resources
Behaviors public interface I*Behavior { void Validate(...); void AddBindingParameters(...); void ApplyDispatchBehavior(...); void ApplyClientBehavior(...); }
Channel ChannelDispatcher * EndpointDispatcher DispatchRuntime DispatchRuntime DispatchOperation DispatchOperation DispatchOperation Add(…) Sub(…) Mul(…)
Channel DispatchRuntime DispatchOperation DispatchOperation DispatchOperation Add(…) Sub(…) Mul(…)
Message Inspection Extending the DispatcherOverview DispatchOperation Add(…) DispatchOperation Sub(…) Channel DispatchRuntime DispatchOperation Mul(…) public interface IDispatchMessageInspector { object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContextinstanceContext); void BeforeSendReply(ref Message reply, object correlationState); }
Parameter Inspection Message Formatting Message Inspection Operation Selector Operation Invoker Extending the DispatcherOverview DispatchOperation Add(…) DispatchOperation Sub(…) Channel DispatchRuntime DispatchOperation Mul(…) public interface IOperationInvoker { object Invoke(object instance, object[] inputs, out object[] outs); IAsyncResultInvokeBegin(object instance, object[] inputs, ...); object InvokeEnd(object instance, out object[] outputs, ...); } public interface IDispatchFormatter { void DeserializeRequest(Message message, object[] parameters); Message SerializeReply(MessageVersionmessageVersion, object[] parameters, object result); } public interface IParameterInspector { object BeforeCall(string operationName, object[] inputs); void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState); } public interface IDispatchMessageInspector { object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContextinstanceContext); void BeforeSendReply(ref Message reply, object correlationState); } public interface IDispatchOperationSelector { string SelectOperation(ref Message message); }
.Description.Behaviors.Add(new MyBehavior()); <myBehavior /> [MyBehavior] Service / Endpoint Behaviors Operation Behaviors Extending the DispatcherBehaviors DispatchOperation Sub(…) Message Formatting Message Inspection Operation Selector Parameter Inspection Operation Invoker Channel DispatchRuntime
Summary • WCF uses a layered architecture: Messaging & Service Model • Description is central • Behaviors participate in description to runtime conversion • Use Behaviors to tweak the runtime: Validation, Throttles, Extensions etc. Programming Model Description Runtime
Community Resources • http://geeks.netindonesia.net • http://www.netfx3.com • http://msdn.microsoft.com • http://www.codeplex.com • http://www.sf.net • http://www.asp.net • http://www.windowsforms.net • http://www.silverlight.net
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.