如何在服务级别在 WCF 中记录 JSON 响应
我有一个 WCF 服务,它将 JSON 返回给客户端。
这是 app.config
<system.serviceModel>
<services>
<service name="MyServices.MyService">
<endpoint address="" behaviorConfiguration="JsonBehavior" binding="webHttpBinding"
contract="Myervices.IMyervice">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/MyEmulator/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
这是服务公开的示例操作
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, Method = "GET", UriTemplate = "me/{id}")]
JsonObject GetObject(string id, string connection_type);
现在,我想记录服务从客户端接收到的每个请求以及发送回客户端的响应...在这种情况下它是 JSOn。
我创建了一个自定义 MessageInspector,如下所示
public class MyMessageInspector : IDispatchMessageInspector
{
private bool LoggingEnabled;
#region IDispatchMessageInspector Members
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
Guid correlationState = Guid.NewGuid();
if (this.LoggingEnabled)
{
StringBuilder message = new StringBuilder();
message.AppendFormat("CorrelationState = {0}{1}", correlationState, Environment.NewLine);
message.AppendFormat("Local Address = {0}{1}", channel.LocalAddress, Environment.NewLine);
message.AppendFormat("Remote Address = {0}{1}", channel.RemoteAddress, Environment.NewLine);
message.AppendLine("Request");
message.AppendLine(request.ToString());
message.AppendLine();
this.logger.LogMessage(message.ToString());
}
return correlationState;
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
if (this.LoggingEnabled)
{
StringBuilder message = new StringBuilder();
message.AppendFormat("CorrelationState = {0}{1}", correlationState, Environment.NewLine);
message.AppendLine("Reply");
message.AppendLine(reply.ToString());
message.AppendLine();
this.logger.LogMessage(message.ToString());
}
}
#endregion
}
但这似乎没有记录任何响应..response.Tostring() 只说“...stream”
如果我的服务以 SOAP XML 格式返回数据,则上述内容有效..但是我希望它记录 JSON 响应,即当我调用 REST 服务端点时在浏览器中下载的 *.js 文件内的文本。例如: http://localhost:8080/MyService/me/2/
任何想法我如何能达到这个目的吗?
I have a WCF service that returns JSON to the clients.
This is the app.config
<system.serviceModel>
<services>
<service name="MyServices.MyService">
<endpoint address="" behaviorConfiguration="JsonBehavior" binding="webHttpBinding"
contract="Myervices.IMyervice">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/MyEmulator/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
This is a sample opertaion exposed by the service
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, Method = "GET", UriTemplate = "me/{id}")]
JsonObject GetObject(string id, string connection_type);
Now, I would like to log every request that the service receives from clients and response that is being sent back to clients...in this case it sin JSOn.
I have create a custom MessageInspector as follows
public class MyMessageInspector : IDispatchMessageInspector
{
private bool LoggingEnabled;
#region IDispatchMessageInspector Members
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
Guid correlationState = Guid.NewGuid();
if (this.LoggingEnabled)
{
StringBuilder message = new StringBuilder();
message.AppendFormat("CorrelationState = {0}{1}", correlationState, Environment.NewLine);
message.AppendFormat("Local Address = {0}{1}", channel.LocalAddress, Environment.NewLine);
message.AppendFormat("Remote Address = {0}{1}", channel.RemoteAddress, Environment.NewLine);
message.AppendLine("Request");
message.AppendLine(request.ToString());
message.AppendLine();
this.logger.LogMessage(message.ToString());
}
return correlationState;
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
if (this.LoggingEnabled)
{
StringBuilder message = new StringBuilder();
message.AppendFormat("CorrelationState = {0}{1}", correlationState, Environment.NewLine);
message.AppendLine("Reply");
message.AppendLine(reply.ToString());
message.AppendLine();
this.logger.LogMessage(message.ToString());
}
}
#endregion
}
But this doesnt seem to log any of the resposne.. the response.Tostring() only says "...stream"
the Above works if my service returns data in SOAP XML format..but i want it to log JSON response which is the text inside the *.js file downloaded in the browser when i call the REST service endpoint. ex: http://localhost:8080/MyService/me/2/
any ideas how I can achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码提供了一种处理 REST 消息的机制,并且作为奖励允许 SOAP 正常传递。这有点侵入性(流被读取破坏,因此代码需要重新生成它),但我还没有找到更好的方法。
http://code.msdn.microsoft.com/WCF-REST-Message -检查器-c4b6790b
This code provides a mechanism for working with REST messages, and as a bonus allows SOAP to pass through as normal. It's a little intrusive (the stream is destroyed by reading so the code needs to regenerate it) but I haven't found any better way.
http://code.msdn.microsoft.com/WCF-REST-Message-Inspector-c4b6790b