简体   繁体   English

在winform中使用WCF服务,如何动态设置Endpoint元素和协定

[英]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 : 但是我遇到了一个主要问题:

  1. When i rename or delete the 'MyProject.exe.config' file. 当我重命名或删除“ MyProject.exe.config”文件时。 It is showing error ' Could not find endpoint element with name BasicHttpBinding_IService1 and contract ServiceReferenct1.IService1 它显示错误'找不到名称为BasicHttpBinding_IService1和合同ServiceReferenct1.IService1的终结点元素

    Since 'MyProject.exe.config' file contains binding and endpoint address which i don't want to share with client or anyone. 由于“ MyProject.exe.config”文件包含我不想与客户端或任何人共享的绑定和端点地址。

    Is there any way to dynamically set endpoint element and contract without using 'MyProject.exe.config' file ? 有没有办法在不使用'MyProject.exe.config'文件的情况下动态设置端点元素和协定? 在此处输入图片说明

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>
  1. How can i change the Default Message shown by service: 如何更改服务显示的默认消息:

    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.

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