WCF client factories

Flower provides a factory class which generates reusable WCF client proxies for arbitrary services. Additionally, the factory can incorporate multiple clients into a single one for load balancing and failover.

To create an autogenerated WCF client, define it in a service container by using Flower.Services.Clients.WcfClientFactory object.

	name: 'MyServiceContainer',
	configuration: {
		MyService: {
			$factoryObject: 'Factory',
			$factoryMethod: 'Create',

			$constructor: {
				// This is the binding to be used by the client.
				binding: { $ref: 'defaultNetTcpBinding' },
				remoteAddress: 'net.tcp://hostname:10000/MyService'

		Factory: {
			$type: 'Flower.Services.Clients.WcfClientFactory, Flower.Services.Clients',

			// This is the assembly qualified name of the service contract interface.
			Contract: 'ServiceContracts.IMyService, ServiceContracts'

		// You can also specify a list of Spring.Aop advisors and WCF client behaviors
		// to be applied to the proxy.
		Advisors: [ ... ],

		Behaviors: [ ... ]

Instead of calling 'Create' factory method, you can call the following methods to create a composite proxy:
  • CreateBalancingClient - the client will invoke next proxy each time until the end of the list and then start from the first. If a proxy fails with a CommunicationException except FaultException, the proxy will call the next one. If all proxies failed, the exception is rethrown.
  • CreateFailoverClient - the client will invoke the first proxy each time. If it fails, the next proxy will be invoked. If all proxies failed, the exception is rethrown.
  • CreateBroadcastClient - the client will invoke all the proxies each time. If any of them fails, the exception is rethrown.

All these methods accept a list of proxy objects. The proxies must implement the service contract interface specified in the 'Contract' property of the factory.

If you'd like to create WCF proxies in the code, you can use a more convenient generic version of WcfClientFactory. It accepts the service contract as a type parameter and its 'Create...' methods produce instances of the service contract interface.

var factory = new WcfClientFactory<IMyService>();
var client = factory.Create("net.tcp://hostname:10000/MyService", new NetTcpBinding());

Last edited Jan 30, 2014 at 5:25 PM by dbratus, version 1


No comments yet.