.NET Architect Specialties: SOA, Xml, Distributed Messaging Primarily work on System Integration & Information Sharing projects 2009 Microsoft MVP, Solutions Architect Twitter @cdeweese – use #stldodn for conference http://christopherDeweese.com/ Avid Xbox player (when kids are sleeping) About Chris Deweese
Agenda Why & What of WCF Building a service Lessons Learned
Why Services? Encapsulation of business capabilities. “Unit of work exposed to the world” – Juval Lowy Support multiple front-ends with one service Exposes data model using contracts (Schema, WSDL) Improves interoperability and automation
What is WCF? Framework for building service-oriented applications Separates the logic of services from the channels they communicate on Messages based on Simple Object Access Protocol (SOAP) Support for REST & Plain-old Xml (POX) in .NET 3.5
Why WCF? Evolution of service oriented thinking Remoting, Message Queuing, and ASMX were designed on different stacks WCF unifies the programming model for exposing services through different channels (Web, Message Queues, Sockets, In-Process) Service logic and function is not dependent on the channel or transport
Key Concepts Service – Unit of work exposed to the world. Endpoint – the collection of the services address, binding, and contract. Address – The network location of the service(e.g., http://localhost/home/home.svc) Binding – How you interact with the service at the wire level. Channel – The transport mechanism that moves the message between service and client. Dispatcher – Manages execution of service code by routing messages to the appropriate service instance and method. Behavior – How you control the local execution of a service.
WCF Extensibility WCF built for extensibility Base classes & interfaces provided which are the gateway to extending WCF Leverages dependency injection principle and factories to hook your extensions into the framework at runtime Support for building custom channels, bindings, etc Lots of “knobs and switches” to tweak WCF through configuration or code attributes
Address “Dude where’s my service?” The location of the service on the network where it can be reached. http://localhost/pizza.svc net.msmq://localhost/private/pizzaservice net.tcp://localhost:6000/pizzaservice Set via configuration or through code
Binding “How do I talk to this thing?” The protocol and policies used to connect to the service at it’s address. WCF Provided Bindings Http(BasicHttp, WsHttp, WsDualHttp, WsFederation) NetMsmq MsmqIntegration NetNamedPipe NetTcp NetPeerTcp Set via configuration or through code
Contract “What’s it going to do for me?” Defines the operations, inputs, outputs, and message exchange patterns of the service. Defined using an Interface; wired into WCF by using the ServiceContract attribute. Methods use the OperationContract attribute. Classes use the DataContract attribute and members use the DataMember attribute. WCF Serialization is “Opt-In”
Hosting WCF Services In Process Windows Service Web Service (IIS) Windows Activation Services WAS allows you to host a service on any binding
Some Opinion, Your Mileage May Vary The default WCF Service project is a good start Placing the contracts and service implementation in the same project creates some amount of coupling Following the Web Service Software Factory guidance a recommended practice is to separate the contracts and implementation Separating the host project leaves you open to target different hosting environments, Web, Windows Service, Windows Activation
Lessons Learned Configuration complexity increases when using multiple services and clients in one application .NET 4.0 WCF is touting less configuration. One-way and two-way bindings require different contracts. One-way operations must have a void return (Sub in VB.Net) Very extensible but requires knowledge of WCF stack and careful implementation Example: Message handlers can corrupt messages if used improperly Out of the box you still have to do some work to build a properly scalable and maintainable service – WCF is not “drag & drop” To an architect this is not a bad thing!