在winform中消费WCF服务,如何动态设置Endpoint元素和契约
Consume WCF service in winform, how to dynamically set Endpoint element and contract
我已经构建了一个测试 WCF 服务 Service1.svc
我已将服务的服务引用添加到我的 Winform 中。它运行良好,我可以轻松地在 winform 中使用 WCF 服务。但是我遇到了一个主要问题:
- 当我重命名或删除 'MyProject.exe.config' 文件时。它显示错误'无法找到名称为 BasicHttpBinding_IService1 和合同 ServiceReferenct1.IService1
的端点元素,因为 'MyProject.exe.config' 文件包含我不想要的绑定和端点地址与客户或任何人分享。
有没有办法在不使用 'MyProject.exe.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:
如果您没有服务配置,您可以手动创建代理。
这是一个例子:
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("YourEndPoint");
var channelFactory = new ChannelFactory<YourInterface>(binding, endpoint);
YourInterface client = null;
client = channelFactory.CreateChannel();
client.YourOperation();
在上面的示例中,我使用了 BasicHttpBinding。如果您正在使用其他绑定,只需使用正确的 class,例如 NetTcpBinding。
如果您在 try/catch 块中处理您的服务,您可以处理此错误并向您的客户发送最友好的消息。
希望对您有所帮助。
我已经构建了一个测试 WCF 服务 Service1.svc 我已将服务的服务引用添加到我的 Winform 中。它运行良好,我可以轻松地在 winform 中使用 WCF 服务。但是我遇到了一个主要问题:
- 当我重命名或删除 'MyProject.exe.config' 文件时。它显示错误'无法找到名称为 BasicHttpBinding_IService1 和合同 ServiceReferenct1.IService1
的端点元素,因为 'MyProject.exe.config' 文件包含我不想要的绑定和端点地址与客户或任何人分享。
有没有办法在不使用 'MyProject.exe.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:
如果您没有服务配置,您可以手动创建代理。
这是一个例子:
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("YourEndPoint");
var channelFactory = new ChannelFactory<YourInterface>(binding, endpoint);
YourInterface client = null;
client = channelFactory.CreateChannel();
client.YourOperation();
在上面的示例中,我使用了 BasicHttpBinding。如果您正在使用其他绑定,只需使用正确的 class,例如 NetTcpBinding。
如果您在 try/catch 块中处理您的服务,您可以处理此错误并向您的客户发送最友好的消息。
希望对您有所帮助。