在调试WCF服务时,出现了元数据交换错误。

4

我正在尝试使用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>

如果有人之前遇到过同样的问题并解决了,请回复我。 - Utkarsh Shah
2个回答

1
删除MEX终结点。 Mex终结点需要启用匿名身份验证。
<services> 
    <service behaviorConfiguration="CalculatorServiceBehavior" name="WCFDuplex.CalculatorService">
     <endpoint address="" binding="wsDualHttpBinding" 
      contract="WCFDuplex.ICalculatorDuplex"/>       </endpoint> 
    </service>  </services>

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

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>  

谢谢您的回复。我已经做了所有这些更改,但对我来说仍然无法正常工作。 - Utkarsh Shah
到底是什么导致了错误?你使用的是哪个URL来访问你的服务? - iamkrillin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接