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?