[英]Consume WCF service in winform, how to dynamically set Endpoint element and contract
I have build a test WCF Service Service1.svc I have added service reference of the service to my Winform. 我已经构建了一个测试WCF服务Service1.svc,并将该服务的服务引用添加到Winform中。 Its working good and i can easily consume the WCF service in winform.
它的工作良好,我可以轻松地在Winform中使用WCF服务。 But i got a major problem :
但是我遇到了一个主要问题:
App.config : App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://svc.phed.net/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
You have created a service.
您已创建服务。 To test this service, you will need to create a client and use it to call the service.
要测试此服务,您将需要创建一个客户端并使用它来调用该服务。 You can do this using the svcutil.exe tool from the command line with the following syntax:
您可以使用以下语法从命令行使用svcutil.exe工具执行此操作:
If you don't have the service configuration, you can create a proxy manually. 如果没有服务配置,则可以手动创建代理。
Here is an example: 这是一个例子:
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("YourEndPoint");
var channelFactory = new ChannelFactory<YourInterface>(binding, endpoint);
YourInterface client = null;
client = channelFactory.CreateChannel();
client.YourOperation();
In above example, I've used the BasicHttpBinding. 在上面的示例中,我使用了BasicHttpBinding。 If you're using another binding, just use the right class, for instance a NetTcpBinding.
如果使用其他绑定,则只需使用正确的类,例如NetTcpBinding。
If you handle your service in a try/catch block you can be able to handle this error and throw a most friendly message to your client. 如果您在try / catch块中处理服务,则可以处理此错误,并向客户发送最友好的消息。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.