开发者

Create WCF service client with specified address without specifying configuration name

开发者 https://www.devze.com 2023-04-10 00:54 出处:网络
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?

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 cumb开发者_StackOverflowersome. Is there a cleaner way?


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")));


Your generated client should also have a constructor that looks like this:

public ServiceClient(
    System.ServiceModel.Channels.Binding binding,
    System.ServiceModel.EndpointAddress remoteAddress)
        : base(binding, remoteAddress) {
    }

You can call this one without an endpoint configuration.


If you want to actually just want to call a service without having to know everything there is to know about WCF services and configuration handling, in C # you can just do...

        String url = "http:\\somehost:someport\\pathToSomeService";

        EndpointAddress address = new EndpointAddress(url);
        Binding binding = new BasicHttpBinding();

        YourClient client = new YourClient(binding, address);

        // Call your client methods
        client.SomeMethod(parm1, parm2);

The above assumes you generated a service reference and does not require configuration information to exist anywhere, not in the generated service reference, not in the DLL and not in the executable. No configuration. None.

I use the above in a true standalone service proxy dll. It is standalone in the truest sense of the word as it is completely configurable with no dependence on the calling executable to provide anything.


Well, you could use the default constructor, but then you'd have to manually program in all of the configuration settings. By specifying the configuration name, the service client will automatically load the configuration in from the .config file, all you need to know is which configuration to use (you can have multiple, e.g. one for HTTP and another for Net.Tcp). The remoteAddress, of course, just tells WCF where to make the connection.

If you are having trouble configuring the client settings themselves, make sure you're using the WCF Service Configuration tool. It works for both the service config as well as the client config.

0

精彩评论

暂无评论...
验证码 换一张
取 消