3

I have a wcf service currently used. I need to add a restful method inside my wcf service. Like below as a sample:

[OperationContract]
string GetDataWcf();

[OperationContract]
[WebGet(UriTemplate = "Employee/{id}")]
Employee GetEmployeeById(string id);

I have an inspection class that inspects incoming and outgoing messages. I want to know that if a service call was made to rest service or wcf service. How can I understand this.

   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));
    }
}

Thanks in advance

Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
Omer
  • 8,194
  • 13
  • 74
  • 92

1 Answers1

2
  1. You can inspect the content type of the incoming request and/or outgoing response. For instance if ContentType is "application/soap+xml", then this would indicate it is a call to the standard WCF service. Or you could check if the content type is "application/json" or "application/xml", then this is a call to the restful service. This would allow for other content types to be treated as standard WCF requests (non WCF-REST requests).

Here's how you can access the content type of the response:

HttpResponseMessageProperty httpResp = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];

String contentType = httpResp.Headers[HttpResponseHeader.ContentType)];
  1. Another option is to inspect the URL of the request and make a determination based on that.
Don
  • 6,632
  • 3
  • 26
  • 34