【问题标题】:How to understand whether a service call is "Restful service call" or "standard wcf call"?如何理解一个服务调用是“Restful 服务调用”还是“标准 wcf 调用”?
【发布时间】:2015-10-30 23:24:00
【问题描述】:

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

[OperationContract]
string GetDataWcf();

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

我有一个检查类来检查传入和传出的消息。我想知道是否对 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));
    }
}

提前致谢

【问题讨论】:

  • 为什么需要在消息检查器中知道?操作名称就够了吗?
  • @TomRedfern,这个类是一个通用类,对于每种类型都应该使用这个检查器类。
  • 但是为什么你需要知道呢?根据用户是调用rest还是soap,你会采取什么不同的行动?

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


【解决方案1】:
  1. 您可以检查传入请求和/或传出响应的内容类型。 例如,如果 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 并据此做出决定。

【讨论】:

  • 或者user-agent可能是?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 2015-06-19
  • 2017-05-25
  • 2017-04-09
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
相关资源
最近更新 更多