简体   繁体   English

使用指定的地址创建WCF服务客户端,而不指定配置名称

[英]Create WCF service client with specified address without specifying 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? 有没有办法在C#中使用指定的端点地址创建WCF服务客户端的实例而不指定配置名称?

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. 现在,我正在努力阅读使用ConfigurationManager来解决它的配置元素,但它看起来非常麻烦。 Is there a cleaner way? 有更干净的方式吗?

I prefer not to use the endpoint configuration in the .config file. 我不想在.config文件中使用端点配置。 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... 如果你想要实际上只是想要调用服务而不必了解有关WCF服务和配置处理的所有信息,那么在C#中你可以做...

        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. 以上假设您生成了服务引用,并且不要求配置信息存在于任何位置,不存在于生成的服务引用中,不存在于DLL中,也不存在于可执行文件中。 No configuration. 没有配置。 None. 没有。

I use the above in a true standalone service proxy dll. 我在一个真正的独立服务代理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, eg one for HTTP and another for Net.Tcp). 通过指定配置名称,服务客户端将自动从.config文件加载配置,您需要知道的是要使用的配置(您可以有多个,例如一个用于HTTP而另一个用于Net.Tcp)。 The remoteAddress, of course, just tells WCF where to make the connection. 当然,remoteAddress只是告诉WCF在哪里建立连接。

If you are having trouble configuring the client settings themselves, make sure you're using the WCF Service Configuration tool. 如果您自己配置客户端设置时遇到问题,请确保使用WCF服务配置工具。 It works for both the service config as well as the client config. 它适用于服务配置和客户端配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM