75

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?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Alex
  • 7,432
  • 20
  • 75
  • 118
  • 1
    For anyone searching this up, take a look at this answer: http://stackoverflow.com/a/839941/592732 – MarioVW Mar 17 '14 at 18:18

2 Answers2

116

You'll have to use the ChannelFactory class.

Here's an example:

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

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

Related resources:

Daniel B
  • 3,109
  • 2
  • 33
  • 42
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • 4
    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 – Alex May 31 '10 at 12:46
  • You should cast `client` to `IClientClient` in order to close it though. – Dyppl May 25 '11 at 06:03
  • In my example I'm assuming that the `IMyService` interface inherits from [System.ServiceModel.ICommunicationObject](http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.aspx). I modified the sample code to make this clearer. – Enrico Campidoglio May 25 '11 at 09:58
  • @EnricoCampidoglio question: do you have to re-create the channel every time you want to make a call or can you store the IService in global variables to re-use throughout? When i test my connection using this method, it works, but then later if i try to execute a call in a separate method, i get a "no endpoint listening" error? – MaxOvrdrv Sep 18 '14 at 15:09
  • actually, i found out that you need the full url... not the ?wsdl address for this to work. Which makes sense. Everything now works properly. This is a great answer. – MaxOvrdrv Sep 18 '14 at 18:43
  • 2
    I combined this with [this answer](http://stackoverflow.com/a/573925/345659) and works great. Thanks – JumpingJezza Feb 19 '15 at 06:11
7

You can also do what the "Service Reference" generated code does

public class ServiceXClient : ClientBase<IServiceX>, IServiceX
{
    public ServiceXClient() { }

    public ServiceXClient(string endpointConfigurationName) :
        base(endpointConfigurationName) { }

    public ServiceXClient(string endpointConfigurationName, string remoteAddress) :
        base(endpointConfigurationName, remoteAddress) { }

    public ServiceXClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
        base(endpointConfigurationName, remoteAddress) { }

    public ServiceXClient(Binding binding, EndpointAddress remoteAddress) :
        base(binding, remoteAddress) { }

    public bool ServiceXWork(string data, string otherParam)
    {
        return base.Channel.ServiceXWork(data, otherParam);
    }
}

Where IServiceX is your WCF Service Contract

Then your client code:

var client = new ServiceXClient(new WSHttpBinding(SecurityMode.None), new EndpointAddress("http://localhost:911"));
client.ServiceXWork("data param", "otherParam param");