tags:

views:

4882

answers:

3

How to create a WCF application without using the svcutil.exe tool?

+3  A: 

What version of Visual Studio are you using?

VS2008 incorporates the functionality of the svcutil tool. To generate the proxy, click on the Solution Explorer and select 'Add Service Reference', you can then enter the URL of the server, give the namespace a name and off you go.

You may want to expand your question to provide some more details of what you are wanting to achieve.

Simon Temlett
+3  A: 

The svcutil.exe tool (and the equivalent functionality in VS2008) are normally used like this: -

You point it at a service description (a WSDL document) and it generates client-side code and config to help you make a client that can communicate with the services described in the WSDL. The tools can do other things as well, but this is what they're most often used for. I assume it's this use of svcutil.exe that you're referring to.

So if you have an existing service, maybe written using some other stack (not WCF), using svcutil makes your life easier than it would otherwise be. But remember, the code and config generated could have been written by you. Normally the config looks verbose because the tool puts all the defaults in there. If you wrote the config yourself you could probably omit most of it.

If you are writing client and service yourself, using WCF, there is almost no reason why you'd use svcutil.exe. When you define your [ServiceContract] you can use that same definition in both client and service. When you create your service config file it's a simple job to create a client config file based on it - most of the content is the same.

In the end, there's no substitute for understanding what all the code/config generated by svcutil.exe actually means. Then you'll be in a position to work without it.

Martin
+5  A: 

One way to build your WCF client without using svcutil.exe is to use ChannelFactory. What you would need to do is build the WCF service interface in a separate assembly from the service implementation. You can then reference that interface assembly from both your client and your service.

Once you have the reference, you could use the following code to create a WCF client:

var factory = new ChannelFactory<IMyWcfService>();
var wcfClient = factory.CreateChannel();
bool closedSuccessfully = false;

try
{
    // Now you can make calls on the wcfClient object
    ((ICommunicationObject)wcfClient).Close();
    closedSuccessfully = true;
}
finally
{
    if (!closedSuccessfully)
    {
        ((ICommunicationObject)wcfClient).Abort();
    }
}

Note the cleanup routine. You can't use a using statement reliably to clean up the wcfClient variable. If you do, and the object is in a faulted state, you'll get an exception thrown when the using() block goes out of scope and Dispose() is executed on the wcfClient variable. See Jesse Ezell's blog

Jeremy Wiebe
+1. This appears to be what I was looking for.Will the ChannelFactory read all the settings out of the App.config like a normal proxy class would?
Orion Edwards
Note: The Call to CreateChannel will return something that implements IMyWcfService. This won't have the Close and Abort methods - you need to call them directly on the factory
Orion Edwards
Yes, the ChannelFactory will read settings from app.config. I believe one of the overloads allows you to specify which client configuration to use by name. You can also do everything in code by passing in a binding for the channel to use and ignore config entirely. With regards to closing the client proxy (IMyWcfService), you can cast it to ICommunicationObject and call Close() on that object.
Jeremy Wiebe