如何理解服务调用是"Restful service call"还是"standard wcf call"?

How to understand whether a service call is "Restful service call" or "standard wcf call"?

我有一个当前正在使用的 wcf 服务。我需要在我的 wcf 服务中添加一个 restful 方法。像下面这样的示例:

[OperationContract]
string GetDataWcf();

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

我有一个检查 class 检查传入和传出的消息。我想知道是否对 rest 服务或 wcf 服务进行了服务调用。我怎么能理解这个。

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

提前致谢

  1. 您可以检查传入请求的内容类型and/or 传出响应。 例如,如果 ContentType 是 "application/soap+xml",则表明它是对标准 WCF 服务的调用。或者您可以检查内容类型是 "application/json" 还是 "application/xml",那么这是对 restful 服务的调用。这将允许将其他内容类型视为标准 WCF 请求(非 WCF-REST 请求)。

访问响应内容类型的方法如下:

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

String contentType = httpResp.Headers[HttpResponseHeader.ContentType)];
  1. 另一种选择是检查请求的 URL 并据此做出决定。