Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create WCF service client with specified address without specifying configuration name

Tags:

c#

wcf

Is there a way to create an instance of a WCF service client in C# with a specified endpoint address without specifying a configuration name?

By default, clients have these constructors:

    public ServiceClient() 
    public ServiceClient(string endpointConfigurationName)
    public ServiceClient(string endpointConfigurationName, string remoteAddress)

Obviously, there is a default configuration, because of the first constructor. What I want is to only specify the 2nd parameter of the final constructor. Right now, I'm struggling through reading the configuration elements of using ConfigurationManager to figure it out, but it seems horribly cumbersome. Is there a cleaner way?

like image 912
recursive Avatar asked Sep 29 '11 15:09

recursive


People also ask

What is endpoint configuration name in WCF?

WCF formalizes this relationship in the form of an endpoint. The endpoint is the fusion of the address, contract, and binding (see Figure 1-8). Every endpoint must have all three elements, and the host exposes the endpoint. Logically, the endpoint is the service's interface and is analogous to a CLR or COM interface.

What is binding configuration in WCF?

bindingConfiguration : A string that specifies the binding name of the binding to use when the endpoint is instantiated. The binding name must be in scope at the point the endpoint is defined. The default is an empty string.

What is BasicHttpBinding?

Basic binding is offered by the BasicHttpBinding class. It uses the HTTP protocol to transport and represent a WCF service as an ASP.NET web service (ASMX web service), so that old clients who use ASMX web services can consume the new services conveniently.


1 Answers

I prefer not to use the endpoint configuration in the .config file. I normally do something like this:

        BasicHttpBinding basicbinding = new BasicHttpBinding();
        basicbinding.SendTimeout = TIMEOUT;
        basicbinding.OpenTimeout = TIMEOUT;
        ServiceClient client = new ServiceClient(basicbinding, new EndpointAddress(new Uri("http://xxxxx")));
like image 86
Graymatter Avatar answered Sep 22 '22 14:09

Graymatter