In my WCF service I was getting 405 method not allowed
error and then came across a post which suggest to have the following in Application_BeginRequest
of my WCF host:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Accept, Content-Type,customHeader");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
"POST,GET,OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
"172800");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials",
"true");
HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers",
"customHeader");
HttpContext.Current.Response.AddHeader("Content-type",
"application/json");
HttpContext.Current.Response.End();
}
else
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Accept, Content-Type,customHeader");
HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers",
"customHeader");
HttpContext.Current.Response.AddHeader("Content-type",
"application/json");
}
}
But I am hosting my service using a console application.
using (ServiceHost sc = new ServiceHost(typeof(DataRetriever)))
{
sc.Open();
foreach (var endPoints in sc.Description.Endpoints)
{
Console.WriteLine(endPoints.Address);
}
Console.ReadKey();
sc.Close();
}
So how do I include the headers in the console app.
This can be done. You'd need a shared variable operating between the host (console) exe and the web service classes. You'd have to run a continuous loop after calling WebService.Open(), checking this shared variable for input. The code would look something like this:
this would be in the main class of your web service, referencing and updating the "HeaderString" shared variable from your console.
Hopefully you find this usefull. Good luck.
In WCF, headers can be accessed via an instance of the class OperationContext, which is accessible via the OperationContext.Current (when available).
The naive way to approach this problem is to simply use this property within the method of your service:
For completeness, the code used to host this service within the Console Application (no config required) is:
A .NET client would access the headers like this:
If you have Fiddler, you can also see the headers using this tool.
Whilst this method will do what you want, it is questionable whether you want to mix your business logic (contained within the implementation of
IMyService
), and the logic controlling the "out-of-band" information attached to the message.A cleaner separation is gained by implementing
IDispatchMessageInspector
, which allows you to intercept calls on the server and modify the message as it comes in and out:The headers are accessed from a .NET client in the same way as before. It's worth noting that you can pass information from the
AfterReceiveRequest
method to theBeforeSendReply
, as the object returned in the former method is passed as thecorrelationState
parameter in the latter. This would be useful if the headers you return are dependent on the headers of the incoming message - as your example suggests.Finally, to install this functionality on the service, you need to modify the hosting code as follows:
which we can do by virtue of the fact that
ServerInterceptor
implementsIEndpointBehavior