我有一个当前正在使用的 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));
}
}
提前致谢
最佳答案
- 您可以检查传入请求和/或传出响应的内容类型。 例如,如果 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)];
- 另一种选择是检查请求的 URL 并据此做出决定。
关于c# - 如何理解服务调用是 "Restful service call"还是 "standard wcf call"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31879026/