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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With