Saturday, July 07, 2007

Generating client code for a WCF service: part II

I mentioned in Part I that svcutil.exe will generate a nice client proxy code for your service. It appeared not so nice at the end and I spent some time trying to figure out the correct contract type description for the client configuration. It ended up to be a very small tweaking - I just brought all types under the single namespace umbrella. This code was generated (some attributes and code lines are removed for simplicity):

namespace WCFServiceNamespace
{
//WCF Service classes mapping {...}
}

[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IServiceContract")]
public interface IServiceContract {...}

public interface IServiceContractChannel
: IServiceContract System.ServiceModel.IClientChannel{}

public partial class WCFServiceClient
: System.ServiceModel.ClientBase<IServiceContract>, IServiceContract {...}

The contract interface IServiceContract here is one of the most important parts - before everything happens, the application should find the contract type to create the instance of the proxy class. As you can see, the interface is outside the namespace. This separation reflects the real order of things but I would prefer convenience. So to avoid confusion, let's bring all types under the client's main namespace ClientNamespace:

namespace ClientNamespace.WCFServiceNamespace
{
//WCF Service classes mapping {...}

[System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IServiceContract")]
public interface IServiceContract {..}

public interface IServiceContractChannel
: IServiceContract System.ServiceModel.IClientChannel {}

public partial class ProductsServiceClient
: System.ServiceModel.ClientBase<IServiceContract>, IServiceContract {...}
}

Now the proxy can be created easily with the following endpoint configuration:

<endpoint address="net.pipe://localhost/WCFServicePipe" binding="netNamedPipeBinding"
contract="IServiceContract" name="binding_IServiceContract" />

No comments:


© 2008-2013 Michael Goldobin. All rights reserved