Richard O
Richard O

Reputation: 139

500 Internal Server Error on a self hosted WCF Service

I am attempting to develop a new WCF service that is hosted using the ServiceHost object. I am able to get the console application to start and I can see that it is binding to port 80 via netstat. Using WireShark I am also able to see that the client is able to connect to that port and send over the data. I had a problem early on with the amount of data that is being sent in the SOAP message from the client, but was able to resolve that issue by setting the max receive size on the binding. The HTTP 500 error that I am getting is:

The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).

The following is my WCF code and my service code.

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();
    }
}

The following is my service contract. It is fairly simple and only has a single Operation. It is expected that it will be called upon the receipt of a SOAP based message.

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

The following the my implementation of the service contract

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

Here is what I am getting from my client (the strReport was very long so I excluded it)

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>

Any help resolving this issue would be greatly appreciated.

Regards, Richard

Upvotes: 4

Views: 2762

Answers (1)

Mike Goodwin
Mike Goodwin

Reputation: 8880

Do you want your service to be a SOAP service or a REST service?

On the service side, it is configured to be a REST services (because you are using WebHttpBinding). However, the request from your client is a SOAP request. If your client is a WCF client, it is probably using wsHttpBinding or basicHttpBinding. Both of these are for SOAP.

You can either:

  1. Change your service to use basicHttpBinding or wsHttpBindding to match your client (if you want to use SOAP), or
  2. Change your client to use webHttpBinding (if you want to use REST). This will need more changes because your operation contract is not properly Attributed for REST. And anyway, WCF is not a good option for REST. The ASP.Net Web API is much simpler and better supported.

Also

  1. the Action specified in your operation contract should be just ReportData rather than the namespace qualified version. You may not need it at all in fact.
  2. you can remove the ReplyAction (or specify a proper value if your client needs it)

Generally, you do not need to specify these. I'm not an expert on the innards of SOAP but I believe that WCF will specify these values based on the method name if you don't specify them. Uniqueness of method names in .Net will ensure that the action/replyaction are unique in that case.

With these changes in place It Works On My Machine (TM)

Upvotes: 2

Related Questions