如何理解服务电话是否是"休息服务电话"或"标准wcf电话"?

时间:2015-08-07 13:32:11

标签: c# web-services wcf rest wcf-rest

我目前使用的是wcf服务。我需要在我的wcf服务中添加一个restful方法。如下所示:

[OperationContract]
string GetDataWcf();

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

我有一个检查传入和传出消息的检查类。我想知道如果服务呼叫是休息服务或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 个答案:

答案 0 :(得分:2)

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

    HttpResponseMessageProperty httpResp = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
    
    String contentType = httpResp.Headers[HttpResponseHeader.ContentType)];
    
    1. 另一个选择是检查请求的URL并根据该决定做出决定。