views:

221

answers:

1

Hello.

I'm trying to connect an application (the client) to an exposed WCF service, but not through the application configuration file, but in code.

How should I go about doing this?

Thanks.

+6  A: 

You'll have to use the ChannelFactory class.

Here's an example:

var myBinding = new BasicHttpBinding();
var myEndpoint = new EndpointAddress("http://localhost/myservice");
var myChannelFactory = new ChannelFactory<IMyService>(myBinding, myEndpoint);

IMyService client = null;

try
{
    client = myChannelFactory.CreateChannel();
    client.MyServiceOperation();
    client.Close();
}
catch
{
    client.Abort();
}

Related resources:

Enrico Campidoglio
Great, thanks.As an addition, here's how to get the IMyService object to use in your application: http://msdn.microsoft.com/en-us/library/ms733133.aspx
Andrei