问题描述
我在客户端应用程序中连接了一个 WCF 服务.我在配置文件中使用以下内容.
I have a WCF service that I am connecting in client application. I am using following in configuration file.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyNameSpace.TestService" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:9100/TestService" binding="basicHttpBinding"
bindingConfiguration="MyNameSpace.TestService" contract="TestService.IService" name="MyNameSpace.TestService" />
</client>
</system.serviceModel>
在代码中,我在这个服务上调用API如下,
In the code, I am calling API on this service as follows,
TestServiceClient client = new TestServiceClient()
client.BlahBlah()
现在我想以语法方式定义端点.那怎么办呢?我注释掉了配置文件中的部分,因为我认为我必须在 TestServiceClient 实例上放置一些代码以动态添加端点,但随后在实例化 TestServiceClient 时抛出以下异常.
Now I want to defined endpoint porgramatically. How can that be done? I commented out section from config file as I was thinking I will have to put some code on TestServiceClient instance to add endpoint dynamically but then it throws following exception at the point where TestServiceClient is instantiated.
找不到引用合同的默认端点元素ServiceModel 客户端配置中的TestService.IService"部分.这可能是因为没有找到配置文件您的应用程序,或者因为没有与此匹配的端点元素合同可以在客户元素中找到.
我怎样才能做到这一点?此外,任何关于以编程方式添加端点的代码示例的观点都将不胜感激.
How can I accomplish this? Also any point on code examples for adding endpoint programmatically will be appreciated.
推荐答案
要以编程方式创建端点和绑定,您可以在服务上执行此操作:
To create endpoints and bindings programmatically, you could do this on the service:
ServiceHost _host = new ServiceHost(typeof(TestService), null);
var _basicHttpBinding = new System.ServiceModel.basicHttpBinding();
//Modify your bindings settings if you wish, for example timeout values
_basicHttpBinding.OpenTimeout = new TimeSpan(4, 0, 0);
_basicHttpBinding.CloseTimeout = new TimeSpan(4, 0, 0);
_host.AddServiceEndpoint(_basicHttpBinding, "http://192.168.1.51/TestService.svc");
_host.Open();
您还可以在服务配置中定义多个端点,并选择在运行时动态连接到哪个端点.
You could also define multiple endpoints in your service config, and choose which one to connect to dynamically at run time.
在客户端程序上,您将执行以下操作:
On the client program you would then do this:
basicHttpBinding _binding = new basicHttpBinding();
EndpointAddress _endpoint = new EndpointAddress(new Uri("http://192.168.1.51/TestService.svc"));
TestServiceClient _client = new TestServiceClient(_binding, _endpoint);
_client.BlahBlah();
这篇关于以编程方式添加端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!