5

I'm trying to implement a WCF Rest service without modifying the App.config file.

My Service Interface looks like so:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/teststr/?string={aStr}")]
    string TestString(string aStr);
}

The Service implementation is very basic:

public class TestService : IService
{
    public string TestString(string aStr = null)
    {
        Console.WriteLine("The Test() method was called at {0}"
            + "\n\rThe given string was {1}\n\r"
            , DateTime.Now.ToString("H:mm:ss")
            , aStr);

        return aStr;
    }
}

And my main Program that runs everything:

// Step 1 Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/ServiceSample/");

// Step 2 Create a ServiceHost instance
ServiceHost selfHost = new ServiceHost(typeof(TestService), baseAddress);

try
{
    // Step 3 Add a service endpoint.
    selfHost.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), 
        "TestService");

    // Step 4 Enable metadata exchange.
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    selfHost.Description.Behaviors.Add(smb);

    // Step 5 Start the service.
    selfHost.Open();
    Console.WriteLine("The service is ready.");
    Console.WriteLine("Press <ENTER> to terminate service.");
    Console.ReadLine();

    // Close the ServiceHostBase to shutdown the service.
    selfHost.Close();
}
catch (CommunicationException ce)
{
    Console.WriteLine("An exception occurred: {0}", ce.Message);
    selfHost.Abort();
}

When I run this and enter the following url:

http://localhost:8000/ServiceSample/TestService/teststr/?string=thisisatest

I get this message:

// The message with To '...' cannot be processed at the receiver, due to an 
// AddressFilter mismatch at the EndpointDispatcher. Check that the sender
// and receiver's EndpointAddresses agree.

I've looked at similar SO questions suggesting I add the <webHttp/> behavour (but isn't Step 4 doing that already?), others said adding [ServiceBehavior..]. None of these worked and I didnt find related SO questions useful.

Do I need to modify the App.config file? What am I doing wrong here?

Community
  • 1
  • 1
Serge P
  • 1,591
  • 8
  • 22
  • 42
  • 4
    It looks like you're trying to make a RESTful call to a SOAP service, which won't work - they're two entirely different flavors of web services. Try using [WebServiceHost](http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webservicehost%28v=vs.110%29.aspx) instead of `ServiceHost` and see if that helps. – Tim Sep 18 '14 at 02:17
  • Thanks @Tim that did it! How could I have missed that. – Serge P Sep 18 '14 at 02:28

2 Answers2

14

I realize that there is an answer in the comments above, but I'll try to state what I learned when trying to find a solution to my problem, since this is the first page that comes up in Google by searching the error message.

I was trying to call a 3rd Party SOAP Web Service from JavaScript (terrible I know), and I was running into this issue.

The SOAP 1.2 service I was calling required that I put a :To SOAP Addressing header in the <soap:Header> of the <soap:Envelope>. After doing this as well as adding the :Action that I got from WSDL, I was golden.

What the SOAP Header eventually looked like:

<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <wsa:To>(WebService URL)</wsa:To>
    <wsa:Action>(Action from WebService WSDL)</wsa:Action>
</soap:Header>

Helpful sites:

ToastyMallows
  • 4,203
  • 5
  • 43
  • 52
1

I had this issue and tried numerous fixes and eventually solved by using the WebServiceHost instead of the ServiceHost

Example:

var selfHost = new WebServiceHost(typeof(TestService), baseAddress);
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41