Jose M. Vilomar
Jose M. Vilomar

Reputation: 487

Call The Same WCF Service From Multiples Hosted Servers

I am a little consused with how to acomplish this task. The question is, How can I Call a WCF Services from Multiples Hosted Servers. The WCF is the same for all the Hosted Apps. Same Contract, Same Binding Type, Etc. I am trying to call it in this way because I will host the services in multiples Servers and I need the service to do the same in all of them. I have to call it from one client. VS 2010, .Net Framework 4.0., C#.

Thanks,

Upvotes: 2

Views: 2147

Answers (2)

aaaa bbbb
aaaa bbbb

Reputation: 3043

I use a function like this:

public static MyWcfClientType GetWcFClient(string hostName)
{
    MyWcfClientType client = new MyWcfClientType();

    // Build a new URI object using the given hostname
    UriBuilder uriBld = new UriBuilder(client.Endpoint.Address.Uri);
    uriBld.Host = hostName;

    // Set a new endpoint address into the client
    client.Endpoint.Address = new EndpointAddress(uriBld.ToString());
    return client;
}

Of course use your own type for the "MyWcfClientType"

Upvotes: 1

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

It depends how you plan to create service proxy in the client application. If you want to add service reference it is enough to add it from one server and then create separate endpoint configuration for other servers - all endpoints configurations will exactly same except the address (you can do the same in code). When you call services you will create proxy instance for each server and you will pass name of the endpoint (defined in configuration) for each server like:

 foreach(var endpointName in myStoredEndpointNames)
 {
   var proxy = new MyServiceProxy(endpointName);
   proxy.CallSomeOperation();
 }

Another approach is not using add service reference. In such case you must share contracts between server and client application and you can use ChannelFactory. This class is factory for client proxies which are created by calling CreateChannel. You can pass endpoint configuration name, endpoint address or binding and endpoint address when calling this method.

Upvotes: 2

Related Questions