【问题标题】:Getting an error: Contract requires Duplex, but Binding 'BasicHttpBinding' doesn't support it or isn't configured properly to support it出现错误:合同需要 Duplex,但绑定“BasicHttpBinding”不支持它或未正确配置以支持它
【发布时间】:2012-12-22 22:47:17
【问题描述】:

我有一个 WCF 服务和一个 Silverlight 5 客户端。 我定义了以下接口:

[ServiceContract(Namespace = "Silverlight", CallbackContract = typeof(IDuplexClient))]
public interface IDuplexService
{
    [OperationContract]
    void Subscribe(string userId);

    [OperationContract]
    void Unsubscribe(string userId);
}

[ServiceContract]
public interface IDuplexClient
{
    [OperationContract(IsOneWay = true)]
    void PushNotification(string msg);
}

这是我的 Web.config 文件:

<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

当我尝试运行我得到的服务时:

由于编译过程中出现异常,无法激活服务“/ServerService.svc”。异常消息是:Contract requires Duplex,但 Binding 'BasicHttpBinding' 不支持或未正确配置以支持它。

我知道我需要向 Web.config 添加一些属性,但是无论我在哪里(以及我尝试过的任何东西),我都无法让它工作。

我是 WCF 的新手,希望您能就该主题提供帮助。我所有的谷歌搜索都让我无处可去,在这里问同样问题的人得到的答案对我不起作用。

所以我决定放弃搜索,只是问问。

更新:我使用此链接创建界面 - http://msdn.microsoft.com/en-us/library/cc645027%28v=vs.95%29.aspx

【问题讨论】:

    标签: c# wcf silverlight wcf-binding duplex


    【解决方案1】:

    如果这是您对 WCF 的 web.config 配置的范围,那么您缺少定义合同的 部分:

    <services>
      <service name="WebApplication1.Service1">
        <endpoint address="" binding="wsDualHttpBinding" contract="WebApplication1.IService1" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    

    如果您确实指定了此部分,则另一个可能的原因是合同名称不完全限定;它必须包含完整的命名空间,而不仅仅是合约的名称。

    这是完整的 System.ServiceModel 配置:

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
        <services>
          <service name="WebApplication1.Service1">
            <endpoint address="" binding="wsHttpBinding" contract="WebApplication1.IService1" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
      </system.serviceModel>
    

    本例中应用命名空间为WebApplication1,服务的类名为Service1(即Service1.svc),Service1实现的接口为IService1。

    【讨论】:

    • 你能解释一下MyService,MySLServiceBehavior,MySLService,IMyService和IMetadataExchange是什么吗?这在初学者的眼中并不那么明显。另外我不需要添加一些&lt;binding&gt; 属性吗?
    • 稍微清理了答案以使其更易于理解并添加了完整的配置。
    • 谢谢,但我还是没有看到完整的配置。
    • 将 wsHttpBinding 更改为 wsDualHttpBinding 并且没有错误!谢谢! :-)
    • 好消息!我已经更新了答案,以防其他人在路上遇到这个问题。
    猜你喜欢
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    相关资源
    最近更新 更多