在WCF调试服务时,会发生元数据交换错误

时间:2014-02-04 10:47:05

标签: c# wcf

我正在尝试使用wsDualHttpBinding实现WCF双工应用程序。

我正在按照以下链接提供的步骤:

Duplex WCF Services

当我运行该服务时,它显示“元数据交换错误”

我在这里附上我的代码。

请帮助。

[1] Service1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;

namespace WCFDuplex
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class CalculatorService : ICalculatorDuplex
    {

        double result;
        string equation;
        ICalculatorDuplexCallback callback = null;
        public CalculatorService()
        {
            result = 0.0D;
            equation = result.ToString();
            callback = OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
        }
        public void Clear()
        {
            callback.Equation(equation + "=" + result.ToString());
            result = 0.0D;
            equation = result.ToString();
        }
        public void AddTo(double n)
        {
            result += n;
            equation += " + " + n.ToString();
            callback.Equals(result);
        }
        public void SubtractFrom(double n)
        {
            result -= n;
            equation += " + " + n.ToString();
            callback.Equals(result);
        }
        public void MultiplyBy(double n)
        {
            result *= n;
            equation += " * " + n.ToString();
            callback.Equals(result);
        }
        public void DivideBy(double n)
        {
            result /= n;
            equation += " / " + n.ToString();
            callback.Equals(result);
        }

    }
}

[2] IService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WCFDuplex
{
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples" , SessionMode = SessionMode.Required, CallbackContract = typeof(ICalculatorDuplexCallback))]
    public interface ICalculatorDuplex
    {

        [OperationContract(IsOneWay = true)]
        void Clear();
        [OperationContract(IsOneWay = true)]
        void AddTo(double n);
        [OperationContract(IsOneWay = true)]
        void SubtractFrom(double n);
        [OperationContract(IsOneWay = true)]
        void MultiplyBy(double n);
        [OperationContract(IsOneWay = true)]
        void DivideBy(double n);
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    public interface ICalculatorDuplexCallback
    {
        [OperationContract(IsOneWay = true)]
        void Equals(double result);
        [OperationContract(IsOneWay = true)]
        void Equation(string eqn);
    }
}

[3] Web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="CalculatorServiceBehavior" name="WCFDuplex.CalculatorService">
        <endpoint address="" binding="wsDualHttpBinding" contract="WCFDuplex.ICalculatorDuplex"/>
        <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:12029/Service1.svc"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <protocolMapping>
        <add binding="wsDualHttpBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

2 个答案:

答案 0 :(得分:1)

1)在您的web.config中,您的元端点使用了错误的绑定。将其从netTcpBinding更改为mexHttpBinding

<service behaviorConfiguration="CalculatorServiceBehavior" name="WCFDuplex.CalculatorService">
    <endpoint address="" binding="wsDualHttpBinding" contract="WCFDuplex.ICalculatorDuplex"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    <host>
        <baseAddresses>
            <add baseAddress="http://localhost:12029/Service1.svc"/>
        </baseAddresses>
    </host>
</service>

2)您的架构设置为https://,但您似乎正在使用http://将架构更改为http://

<protocolMapping>
    <add binding="wsDualHttpBinding" scheme="http" />
</protocolMapping>  

答案 1 :(得分:1)

删除MEX端点。 Mex端点要求启用匿名身份验证。

<services> 
    <service behaviorConfiguration="CalculatorServiceBehavior" name="WCFDuplex.CalculatorService">
     <endpoint address="" binding="wsDualHttpBinding" 
      contract="WCFDuplex.ICalculatorDuplex"/>       </endpoint> 
    </service>  </services>

如果问题仍然存在,那么请检查您正在使用的IIS应用程序池的标识是什么?身份需要有权访问%windir%\ temp目录。否则,您无法检索元数据信息。