自托管WCF服务上的500内部服务器错误

时间:2013-08-08 20:13:46

标签: wcf

我正在尝试开发使用ServiceHost对象托管的新WCF服务。我能够启动控制台应用程序,我可以看到它通过netstat绑定到端口80。使用WireShark我还能够看到客户端能够连接到该端口并通过数据发送。我早期遇到了来自客户端的SOAP消息中发送的数据量的问题,但是能够通过在绑定上设置最大接收大小来解决该问题。我得到的HTTP 500错误是:

由于EndpointDispatcher上的ContractFilter不匹配,无法在接收方处理带有Action''的消息。这可能是由于合同不匹配(发送方与接收方之间的操作不匹配)或发送方与接收方之间的绑定/安全性不匹配。检查发件人和收件人是否具有相同的合同和相同的约束(包括安全要求,例如邮件,传输,无)。

以下是我的WCF代码和我的服务代码。

public class MyWCFService
{
    private ServiceHost _selfHost;

    public void Start()
    {
        Uri baseAddress = new Uri(@"http://192.168.1.10");

        this._selfHost = new ServiceHost(typeof(MyServiceImpl), baseAddress);

        try {
            WebHttpBinding binding = new WebHttpBinding();
            binding.MaxBufferSize = 524288;
            binding.MaxReceivedMessageSize = 524288;
            this._selfHost.AddServiceEndpoint(typeof(IMyServiceContract), binding, "MyService");

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            this._selfHost.Description.Behaviors.Add(smb);

            this._selfHost.Open();
        }
        catch ( CommunicationException ce ) {
            this._selfHost.Abort();
        }
    }

    public void Stop()
    {
        this._selfHost.Close();
    }
}

以下是我的服务合同。它非常简单,只有一个操作。预计在收到基于SOAP的消息时将调用它。

[ServiceContract(Namespace = "http://www.exampe.com")]
public interface IMyServiceContract
{
    [OperationContract (Action="http://www.example.com/ReportData", ReplyAction="*")]
    string ReportData( string strReport );
}

以下是我对服务合同的实施

class MyServiceImpl : IMyServiceContract
{
    public string ReportData( string strReport )
    {
        return "ok";
    }
}

这是我从客户端获得的内容(strReport很长,所以我将其排除在外)

POST /MyService HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://www.example.com/ReportData"
Host: 192.168.1.10
Content-Length: 233615
Expect: 100-continue
Connection: Keep-Alive

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <ReportData xmlns="http://www.example.com/">
            <strReport>
                ........
            </strReport>
        </ReportData>
    </soap:Body>
</soap:Envelope>

非常感谢任何解决此问题的帮助。

此致 理查德

1 个答案:

答案 0 :(得分:2)

您希望您的服务是SOAP服务还是REST服务?

在服务端,它被配置为REST服务(因为您使用的是WebHttpBinding)。但是,来自客户端的请求是SOAP请求。如果您的客户端是WCF客户端,则可能使用wsHttpBinding或basicHttpBinding。这两个都是针对SOAP的。

你可以:

  1. 更改您的服务以使用basicHttpBinding或wsHttpBindding来匹配您的客户端(如果您想使用SOAP),或
  2. 更改您的客户端以使用webHttpBinding(如果您想使用REST)。这将需要更多更改,因为您的操作合同未正确归因于REST。无论如何,WCF不是REST的好选择。 ASP.Net Web API更简单,更好地支持。
  3. 另外

    1. 操作合同中指定的Action应该只是ReportData而不是命名空间限定版本。事实上你根本不需要它。
    2. 您可以删除ReplyAction(或在客户需要时指定正确的值)
    3. 通常,您无需指定这些。我不是SOAP内部的专家,但我相信如果你不指定它们,WCF将根据方法名称指定这些值。 .Net中方法名称的唯一性将确保在这种情况下action / replyaction是唯一的。

      进行这些更改 It Works On My Machine(TM)