以编程方式添加端点

编程入门 行业动态 更新时间:2024-10-28 18:28:33
本文介绍了以编程方式添加端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在客户端应用程序中连接了一个 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="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"部分.这可能是因为没有找到配置文件您的应用程序,或者因为没有与此匹配的端点元素合同可以在客户元素中找到.

Could not find default endpoint element that references contract 'TestService.IService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

我怎样才能做到这一点?此外,任何关于以编程方式添加端点的代码示例的观点都将不胜感激.

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, "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("192.168.1.51/TestService.svc")); TestServiceClient _client = new TestServiceClient(_binding, _endpoint); _client.BlahBlah();

更多推荐

以编程方式添加端点

本文发布于:2023-05-25 00:25:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/218666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!