我目前使用的是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));
}
}
提前致谢
答案 0 :(得分:2)
以下是访问响应内容类型的方法:
HttpResponseMessageProperty httpResp = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
String contentType = httpResp.Headers[HttpResponseHeader.ContentType)];