【问题标题】:How to programmatically connect to a WCF service that is hosted in IIS如何以编程方式连接到 IIS 中托管的 WCF 服务
【发布时间】:2015-07-30 21:22:39
【问题描述】:

托管在 IIS 中的 WCF 服务的绑定和客户端端点如下所示。

<bindings>
    <customBinding>
          <binding name="notSecureBinding">
              <binaryMessageEncoding />
              <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
          </binding>
          <binding name="SecureBinding">
              <binaryMessageEncoding />
              <httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
          </binding>
      </customBinding>
  </bindings>



<client>
      <endpoint address="http://ServerName.myDomain.org/ADSearcher/Service1.svc"
                binding="customBinding"
                bindingConfiguration="notSecureBinding"
                contract="GetData.GetData"
                name="notSecureBinding" />

      <endpoint address="https://ServerName.myDomain.org/ADSearcher/Service1.svc"
                binding="customBinding"
                bindingConfiguration="SecureBinding"
                contract="GetData.GetData"
                name="SecureBinding" />
  </client>

现在我要做的是连接到该服务并读取我想要的数据,而不向我的 asp.net mvc 4 应用程序添加任何服务引用。

我在下面尝试了这段代码

BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress("http://ServerName.myDomain.org/ADSearcher/Service1.svc");
IService1 ADUser = new ChannelFactory<IService1>(basicHttpBinding, endpointAddress).CreateChannel();
DataTable ADUserInfo = ADUser.GetADUserList(strUserName, strFirstName, strLastName, strEmail, domain);

但是上面的代码抛出了下面的错误。

由于 EndpointDispatcher 的 ContractFilter 不匹配,接收方无法处理带有 Action 'http://tempuri.org/IService1/GetADUserList' 的消息。这可能是因为合约不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。

这似乎是绑定不匹配,因为在 WCF 配置中,我使用的是“customBinding”,但我无法在 C# 代码中定义它,只能找到“BasicHttpBinding”。

有谁知道我如何通过 C# 代码成功连接到这个 IIS 托管的 WCF 服务并调用我的“GetADUserList”方法而不向我的 MVC 应用程序添加服务引用?

【问题讨论】:

  • 据我所见,您没有在服务器上使用 BasicHttpBinding。您正在使用 CustomBinding。您还使用 GetData.GetData 的合同,而不是 IService1。

标签: c# asp.net-mvc wcf wcf-binding


【解决方案1】:

System.ServiceModel 命名空间中有一个 CustomBinding 类可供您使用。

Binding customBinding = new CustomBinding(
             new BinaryMessageEncodingBindingElement(),
             new HttpTransportBindingElement 
             { 
               MaxReceivedMessageSize = 2147483647, 
               MaxBufferSize = 2147483647 
             });
EndpointAddress endpointAddress = new EndpointAddress("http://ServerName.myDomain.org/ADSearcher/Service1.svc");
IService1 ADUser = new ChannelFactory<IService1>(customBinding, endpointAddress).CreateChannel();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2012-05-31
    • 2022-12-13
    • 2013-02-02
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    相关资源
    最近更新 更多