Creating the WCF client code class from a WCF host.

A WCF consumer (client) needs to have some references to the types in the WCF host. If you are building everything on a single computer and testing, you can simply have your client project refer to the host project and then it can see the types, and can create a Binding as follows:

 

static void InitSomeService()
{
string hostAddress = System.Configuration.ConfigurationManager.AppSettings[“SomeHostUriHttp”];
EndpointAddress address = new EndpointAddress(new Uri(hostAddress));
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<ISomeService> factory = new ChannelFactory<ISomeService>(binding, address);
solService = factory.CreateChannel();
}

 

When you deploy this to a remote server this won’t really work because you don’t deploy the host dlls with the client, and if you were developing a client to talk to a 3rd party WCF service then you wouldn’t have the host files to deploy anyway.

So you need to create copies (or atleast representations ) of the original ISomeService interface and all the other datacontracts. You do this with the svcutil too inlcuded with Visual Studio. The easiest way is to fire up the host and run it. Then open the VS command line and type:

svcutil http://localhost:8000/yourservice

 

It will output the needed files into the same folder as svcutil, and tell you where they are. Now include those cs file in your project, and remove the references to the HOST project if you were referencing the HOST project previously.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s