August 17, 2010

Wcf facility - IWcfClientFactory, AsWcfClient, AsWcfService and PublishMetadata

It's been a few weeks since I last blogged about the Wcf facility (or about anything at all), but it seems the development hasn't stopped for the WCF facility. In this post I will briefly show some of the changes and some new additions.

AsWcfClient and AsWcfService

Previously you would use the ActAs() method to let the WCF facility take care of the implementation, either with a WCF client or a WCF service. I didn't like that method as it was a bit too generic, it takes a object. I guess the WCF facility team didn't like it either, because the methods AsWcfClient and AsWcfService were added. The AsWcfClient takes either a IWcfEndpoint or a IWcfClientModel[] and the AsWcfService takes a IWcfServiceModel[]. This ways it's a lot more discoverable.


The IWcfClientFacility provides a way to lazy-load WCF clients. This method uses the typed factory facility to work. With the typed factory facility you will be able resolve objects with parameters at runtime, without having a direct reference to your container. Krzysztof has written a great article about the typed factory facility, so check that out first.

To get things working you need to add the typed factory facility to your container (note: at the time of writing the WCF facility requires the typed factory facility to be registered first). Next you make sure your service gets a IWcfClientFactory from the container, either through constructor injection or property injection. The IWcfClientFactory basicly has two methods, the GetClient to create a WCF client based on the parameters and Release to release a previously created WCF client. All GetClient overloads requires a generic type, this type will be type of the WCF client. The different GetClient overloads let you specify a IWcfClientModel, IWcfEndpoint, configuration name or the url. Don't forget to release your WCF client through the Release method on the IWcfClientFactory instance.


When you create a service using the WCF facility to provide the configuration, you will notice that you cannot add a service reference using visual studio (even if you would want that). The reason is that by default the WCF service doesn't expose the metadata (wsdl/mex). With the PublishMetadata method you will be able to expose metadata. You can use this method on a WcfServiceModel<T>, like the DefaultServiceModel. To also expose the wsdl you will need to call the EnableHttpGet method on the action:
.AsWcfService(new DefaultServiceModel()
.AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding()).At("basic"))
.PublishMetadata(o => o.EnableHttpGet()))

kick it on