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

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

我有一个当前正在使用的 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));
    }
}

提前致谢

最佳答案

  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 并据此做出决定。

关于c# - 如何理解服务调用是 "Restful service call"还是 "standard wcf call"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31879026/

相关文章:

c# - 使用泛型扩展接口(interface)不可分配给父级 c#

json - 仅针对聚合根更新 RESTful 资源

java - WebLogic 10.3 中的 Web 服务部署问题

web-services - WebService 端点 url 具有 https 时的 SSL 异常

c# - IDispatchMessageInspector 记录纯文本 SOAP 消息?

wcf - 为什么 WCF 会忽略我的 TokenProvider?

.net - WCF Windows 服务未连接到数据库

C# 闭包变量作用域

c# - 浮点值的舍入

c# - 使用 swashbuckle 将摘要或供应商扩展添加到 swagger 中的路径