WCF netTcpBinding问题:合同需要Duplex,但Binding' BasicHttpBinding'不支持它,或者没有正确配置以支持它

时间:2016-09-09 14:14:46

标签: wcf wcf-binding nettcpbinding wcf-callbacks duplex-channel

我试图在WCF服务中创建回调。到目前为止服务使用的是basicHttpBinding,所以我想为netTcpBinding添加另一个端点。服务已在IIS中托管。首先它在IIS 6中托管,但后来我安装了IIS 7.

所以,我收到以下错误:

  

请求的服务' net.tcp:// localhost:1801 / MyServiceName.svc / NetTcpExampleAddress'无法激活。有关详细信息,请参阅服务器的诊断跟踪日志。

查看日志时,这是消息:

enter image description here

所以主要错误是:

  

合同需要Duplex,但Binding' BasicHttpBinding'不支持它,或者没有正确配置以支持它。

以下是我的配置文件:

服务器的 Web.config

<system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="demoServiceNetTcpBinding">
          <security mode="None"/>
        </binding>
      </netTcpBinding>
      <basicHttpBinding>
        <binding name="demoServiceHttpBinding" receiveTimeout="00:05:00" sendTimeout="00:05:00" maxReceivedMessageSize="2147483647">
          <security mode="None"/>
        </binding>        
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="MyServerName.MyServiceName">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:1801/MyServiceName.svc/"/>
            <add baseAddress="http://localhost:1800/MyServiceName.svc/"/>
          </baseAddresses> 
        </host>
    <endpoint 
        address="NetTcpExampleAddress" 
        binding="netTcpBinding" 
        bindingConfiguration="demoServiceNetTcpBinding" 
        contract="MyServerName.SharedContract.IMyServiceName"/>
    <endpoint 
        address="BasicHttpExampleAddress" 
        binding="basicHttpBinding" 
        bindingConfiguration="demoServiceHttpBinding" 
        contract="MyServerName.SharedContract.IMyServiceName"/>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

客户端的 App.config

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="demoServiceNetTcpBinding">
          <security mode="None"/>
        </binding>
      </netTcpBinding>
      <basicHttpBinding>
        <binding name="demoServiceHttpBinding" receiveTimeout="00:05:00" sendTimeout="00:05:00" maxReceivedMessageSize="2147483647">
          <security mode="None"/>
        </binding>        
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint name="NetTcpExampleName"
          address="net.tcp://localhost:1801/DicomQueryService.svc/NetTcpExampleAddress"
          bindingConfiguration ="demoServiceNetTcpBinding"
          contract="MyServerName.SharedContract.IMyServiceName"
          binding="netTcpBinding" />
      <endpoint name="BasicHttpExampleName"
          address="http://localhost:1800/MyServiceName.svc/BasicHttpExampleAddress"
          bindingConfiguration ="demoServiceHttpBinding"
          contract="MyServerName.SharedContract.IMyServiceName"
          binding="basicHttpBinding" />
    </client>
  </system.serviceModel>

我的IIS中的设置:

enter image description here

enter image description here

如果您需要其他任何代码,请告知我们,我会更新问题。

编辑1:

以下是代码中有关我如何从客户端(客户端)调用服务的详细信息:

public class MyCommandClass : IMyServiceCallback
{
    public MyCommandClass()
    {
        var ctx = new InstanceContext(new MyCommandClass());
        DuplexChannelFactory<MyServerName.SharedContract.IMyServiceName> channel = new DuplexChannelFactory<MyServerName.SharedContract.IMyServiceName>(ctx, "NetTcpExampleName");
        MyServerName.SharedContract.IMyServiceName clientProxy = channel.CreateChannel();
        clientProxy.MyFunction(); //debug point is comming here and then it throws the error
        clientProxy.ProcessReport();
        (clientProxy as IClientChannel).Close();
        channel.Close();
    }

    public void Progress(int percentageCompleted)
    {
        Console.WriteLine(percentageCompleted.ToString() + " % completed");
    }
}

其中接口(在服务器端)定义为:

[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyServiceName
{
    [OperationContract]
    void MyFunction();

    [OperationContract(IsOneWay = true)]
    void ProcessReport();
}

public interface IMyServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void Progress(int percentageCompleted);    
}

和服务(在服务器端)定义为:

public class MyServiceName: IMyServiceName
{
    public void MyFunction()
    {
        //do something
    }

    public void ProcessReport()
    {
        //trigger the callback method
        for (int i = 1; i <= 100; i++)
        {
            Thread.Sleep(100);
            OperationContext.Current.GetCallbackChannel<IMyServiceCallback>().Progress(i);
        }
    }
}

到目前为止我的方法只是一个演示。一旦与此问题相关的错误得到修复,那么我将开始开发这些方法。

1 个答案:

答案 0 :(得分:1)

您的服务合同需要双工连接(您具有ServiceCallback属性)。因此,此服务公开的所有端点都必须支持双工连接。 Net.tcp支持它,但basicHttp不支持,所以你现在不能将basicHttp用于你的服务。