How do I know if a "Restful service call" or a "standard wcf call"?
I have a wcf service currently in use. I need to add a restful method inside my wcf service. As below as an example:
[OperationContract]
string GetDataWcf();
[OperationContract]
[WebGet(UriTemplate = "Employee/{id}")]
Employee GetEmployeeById(string id);
I have a validation class that validates incoming and outgoing messages. I want to know what if a service call was called to serve a service or a wcf service. How can I figure it out.
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Uri requestUri = request.Headers.To;
HttpRequestMessageProperty httpReq = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
OkTrace.WriteLine(string.Format("{0} {1}", httpReq.Method, requestUri));
foreach (var header in httpReq.Headers.AllKeys)
{
OkTrace.WriteLine(string.Format("{0}: {1}", header, httpReq.Headers[header]));
}
if (!request.IsEmpty)
{
OkTrace.AddBlankLine();OkTrace.WriteLineOnly(MessageToString(ref request));
}
return requestUri;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
OkTrace.WriteLine(string.Format("Response to request to {0}:", (Uri)correlationState));
HttpResponseMessageProperty httpResp = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
OkTrace.WriteLine(string.Format("{0} {1}", (int)httpResp.StatusCode, httpResp.StatusCode));
if (!reply.IsEmpty)
{
OkTrace.AddBlankLine();
OkTrace.WriteLineOnly(MessageToString(ref reply));
}
}
Thank you in advance
+3
source to share
1 answer
- You can check the content type of the incoming request and / or outgoing response. For example, if the ContentType is "application / soap + xml", this means that this is a call to a standard WCF service. Or you can check if the content type is "application / json" or "application / xml" and then this is a restful service call. This would allow other content types to be treated as standard WCF requests (no WCF-REST requests).
You can access the response content type here:
HttpResponseMessageProperty httpResp = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
String contentType = httpResp.Headers[HttpResponseHeader.ContentType)];
- Another option is to check the request url and make a determination based on that.
+2
source to share