首页 > 解决方案 > 如何嵌入 app.config 并对其 Web 服务端点进行硬编码?

问题描述

我真的不知道绑定在 app.config 中做什么端点,但我知道我需要一个独立的 .exe 文件,仅此而已。如何在我的 exe 中嵌入该文件,或者如何在不使用 app.config 的情况下对整个内容进行硬编码。我有一个返回字符串值的小型 Web 服务。我的 app.config 文件如下所示:

`

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="APIServiceSoap" />
      </basicHttpBinding>
      <customBinding>
        <binding name="APIServiceSoap12">
          <textMessageEncoding messageVersion="Soap12" />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://www.example.com/APIService.asmx"
          binding="customBinding" bindingConfiguration="APIServiceSoap12"
          contract="MyWebAPI.APIServiceSoap" name="APIServiceSoap12" />
    </client>
  </system.serviceModel>
</configuration>`

mu 代码的开头是这样的: MyWebAPI.APIServiceSoapClient client = new MyWebAPI.APIServiceSoapClient(); 任何形式的帮助都将不胜感激。谢谢!

标签: c#visual-studio-2017

解决方案


因为您调用的是使用 WCF 而不是 WCF 的ASMXWeb 服务SOAP 1.2,所以事情变得有点复杂,很可能有几种方法可以做到这一点,但我使用的是:

TextMessageEncodingBindingElement tmebe = new TextMessageEncodingBindingElement();
tmebe.MessageVersion = MessageVersion.Soap12;

TransportBindingElement tbe = new HttpTransportBindingElement();

CustomBinding binding = new CustomBinding(new BindingElement[] { tmebe, tbe });

EndpointAddress endpointAddress = new EndpointAddress("http://localhost/TestWebService/WebService1.asmx");
using (WebService1SoapClient client = new WebService1SoapClient(binding, endpointAddress))
{
    String result = client.HelloWorld();
    Debug.WriteLine(result);
}

推荐阅读