Vous êtes sur la page 1sur 28

Christian Weyer | thinktecture

christian.weyer@thinktecture.com

Jenseits von System.ServiceModel

WCF Tools und Libraries

Christian Weyer
Solution architect and principal consultant at thinktecture Focus on distributed applications service orientation, workflows cloud computing interoperability pragmatic end-to-end solutions
Windows Server, WCF, WF, MSMQ, Windows Azure platform

Microsoft MVP for Windows Azure (Architecture) Independent Microsoft Regional Director for Germany
http://blogs.thinktecture.com/cweyer christian.weyer@thinktecture.com
2

Agenda
Modeling contracts Development process Implementation strategies Configuration On the Wire Communication Under Load Misc

Modeling & Metadata


WCF is strong for interoperability scenarios (read: Web Services in the WS-* empire) Contract modeling can be important to succeed in interoperability Various approaches to improve the level of interoperability contract-first modeling flattening WSDL XSD and WSDL are the contracts in non-pure-WCF environments least common denominator

Schema-based Contract-First
Think in XSD and WSDL first Two basic steps design your contract's data, messages and interface generate code from the contract Tool support would be nice

Flattening WSDL
Still, a number of Web Services stacks do not understand factored-out WSDLs and we are not always dealing with the latest builds and versions, anyway We need a way to put all the WSDL data into one file WCF by default generates multiple files/links Watch out for XML namespaces...

Development Process
Having a look at what a service provides and how it behaves can help understanding how to integrate it into your application architecture simple integration testing services WCF is complex and understanding WCF means seeing WCF debugging services and consumers Not always are the services already implemented and available need ways to mock services

WcfTestClient
Need to test services whether implemented with WCF or other stacks use WSDL metadata Visual Studio comes with a generic services client test tool WcfTestClient.exe in C:\Program Files (x86)\Microsoft Visual Studio 10.0\ Common7\IDE very basic features, but good enough to take a first stab

wcfstorm
WCF & Web Services test client and performance test tool free lite version available (no functional & perf testing)

MockingBird
MockingBird Service Simulator configurable message interceptor backed by a generic simulation engine can act as a stand-in for any web service includes a set of tools to help build and test mock services Plays nicely with the contract-first approach

10

WCF Debugger Visualizers


Visual Studio 2005 shipped with a feature called debugger visualizers visually view useful information about objects during debugging WCF Visualizers contains Message ChannelDispatcher ServiceDescription ServiceHost ServiceEndpoint (for the client side) ClientRuntime Binding SecurityContext Security OperationContext
11

Implementation Strategies
Mapping a good architecture seperates the data transfer structures (aka DTOs) from an internal object entity representation need a way to map the data on the outside to the entities on the inside and vice versa manual mapping may be too error-prone and tedious Code generation svcutil and Add Service Reference... are not customizable customizable code generation can be a life safer when using a ChannelFactory-based approach code generation can ease the pain e.g. enhance the DTOs on the consumer side to implement some base functionality
12

AutoMapper
An object-to-object mapper Uses a fluent configuration API to define an object-object mapping strategy Uses a convention-based matching algorithm to match up source to destination values Geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects better suited for serialization, communication, messaging anti-corruption layer between the domain and application layer Combine mapper with a bootstrap task in a ServiceHost pre-init time-consuming setup Alternative: EmitMapper

13

T4 Templates
T4 is the Text Template Transformation Toolkit built into VS 2010 syntax smells like old ASP code T4 editors available through 3rd party offerings tangible T4 Editor Clarius VisualT4 Look into T4 Toolbox includes several core classes that form a micro-framework for building composite code generators generate multiple output files use existing templates for LINQ to SQL, Entity Framework Use T4 to generate e.g. async contracts from sync interfaces client-side DTOs which inherit from a base class and implement UI-specific interfaces
14

Configuration
When COM was the DLL hell, then WCF is maybe the configuration hell A trend can be seen to put configuration into code instead of .config files WCF supports this very well with its verbose API If you need to learn the WCF configuration syntax XSD IntelliSense in Visual Studio SvcConfigEditor tool, especially for diagnostics setup If you need to use WCF configuration files consider the configSource feature

15

SvcConfigEditor
C:\Program Files (x86)\Microsoft Visual Studio 10.0\ Common7\IDE

16

configSource
Feature in the .NET Framework configuration system since .NET 2.0 factor your XML-based configuration into several files works on any configuration WCFs system.serviceModel is a configuration section group need to use configSource attribute on the sub elements
<configuration> <system.serviceModel> <services configSource="services.config" > </services> <bindings configSource="bindings.config"> </bindings> <behaviors configSource=behaviors.config"> </behaviors> </system.serviceModel> </configuration>
17

On the Wire
In WCF the truth is on the wire. Period. ...and even beyond the wire WCF messages can be traced messages on the transport and the service model layer not the verbatim message WCF can emit verbose tracing data integrated with System.Diagnostics Use SvcTraceViewer to inspect both traces and messages merge your own traces Use Fiddler to see the actual HTTP/S traffic for WCF SOAP and Web/HTTP/REST services

18

SvcTraceViewer
C:\Program Files (x86)\Microsoft Visual Studio 10.0\ Common7\IDE

19

Fiddler
Acts as a system web proxy You can tweak the requests in Fiddler to test different values or HTTP methods (for WCF Web/HTTP/REST) Fiddler can be extended with custom inspectors e.g. for WCF binary encoder

20

Communication
Optimized data serialization can save bandwidth and also communication/processing time built-in binary encoder does a pretty good job better alternatives through 3rd party encoders/serializers can be useful in mobile scenarios Pub/sub need for communication patterns beyond request-response should be firewall-friendly should be able to span multiple platforms

21

Protocol Buffers
protobuf binary serialization format used by Google for much of their data communications small in size: efficient data storage (far smaller than xml) cheap to process both at the client and server platform independent: portable between different programming architectures extensible: add new data to old messages protobuf-net for .NET developers can be integrated with WCF by using [ProtoBehavior] by using a Stream type

22

Fast InfoSet
Joint standard by the ITU-T and ISO/IEC that specifies a binary encoding of the XML Information Set (InfoSet) Most compact encoding when compared to text, binary and MTOM encodings depending on the structure of the message Widely available on Java as well as on other platforms For .NET & WCF we need to purchase WCFXtensions from Noemax pre-built bindings with different channels can use FI encoder in any binding (via FiMessageEncodingBindingElement)

23

Duplex messaging
WCF offers simple duplex messaging callback contracts binding with duplex support (compositeDuplex binding element in the stack) TCP-based duplex may not even be able to establish a channel through a firewall HTTP-based duplex with wsDualHttp is pretty useless two mutual HTTP connections Long (or smart) polling as viable duplex approach DuplexHttp binding from MSDN not compatible with Silverlights polling duplex, but can be combined

24

WCF Load Test


Load testing your WCF services based on what you already have Takes a WCF message trace file or Fiddler trace and a WCF client proxy (or a WCF interface contract) Generates a unit test replays the same sequence of calls found in the trace file unit test can then be used to generate load tests and load-test the target service

25

Misc
Dealing with real-world aspects of data in WCF thinktecture DataObjectModel: http://dataobjectmodel.codeplex.com/ New HTTP features in WCF.vNext new web/REST pipeline jQuery support WebSockets implementation http://wcf.codeplex.com/

26

Resources
http://blogs.thinktecture.com/cweyer christian.weyer@thinktecture.com Data types interoperability between .NET and Java http://msdn.microsoft.com/en-us/netframework/gg413252.aspx WSCF.blue http://wscfblue.codeplex.com/ MockingBird http://mockingbird.codeplex.com/ WCF Debugger Visualizer http://wcfvisualizer.codeplex.com/ AutoMapper http://automapper.codeplex.com/ EmitMapper http://emitmapper.codeplex.com T4 Toolbox http://t4toolbox.codeplex.com/
27

Resources
protobuf.-net http://code.google.com/p/protobuf-net/ WCF Xtensions http://www.noemax.com/products/wcfx/index.html http://www.noemax.com/products/fastinfoset/features.html DuplexHttpBinding http://archive.msdn.microsoft.com/duplexhttp Fiddler http://www.fiddler2.com/fiddler2/ http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx ?ProjectName=silverlightws&DownloadId=12007 http://identitymodel.codeplex.com/releases/view/52187 WCF Load Test http://wcfloadtest.codeplex.com/ WcfStorm http://www.wcfstorm.com/wcf/home.aspx
28