简体   繁体   English

Wcf:所有回应都在哪里?

[英]Wcf : Where are all the responses going?

I have some REST web services implemented like this : 我有一些实现这样的REST Web服务:

[ServiceContract]
public interface IRESTService
{
    [WebGet(UriTemplate = "GetEveryone", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    EveryoneDTO GetEveryone();
}

public class RESTService : IRESTService
{
    public EveryoneDTO GetEveryone()
    {
        // [...] Some processing
        return everyone;
    }
}

Where is going my everyone object? everyone反对去哪儿? I mean, it must happens some things to transform the objects to JSON and send them. 我的意思是,必须发生一些事情才能将对象转换为JSON并发送它们。 My debbugging won't lead me any further. 我的放荡行为不会进一步引导我。

I'm interested in this because, let's say, I want to process every string contained in every objects I send back (maybe for encoding purposes), how/where would I be able to implement a middleware that intercept every object and be able to alter them easily before sending them? 我对此很感兴趣,因为,我想处理发送回每个对象中包含的每个字符串(可能出于编码目的),如何/在何处实现能够拦截每个对象并能够在发送之前轻松更改它们?

A good solution is to implement a Custom Message Inspector . 一个好的解决方案是实现自定义消息检查器
There are two interfaces you can implement, depending on client or server side: 您可以实现两个接口,具体取决于客户端或服务器端:

IClientMessageInspector for client and IDispatchMessageInspector for server. 客户端的IClientMessageInspector和服务器的IDispatchMessageInspector You can implement both on same class and assembly and use what is more convenient, because message inspectors are extensions and you can configure (Web.config for instance) which you want to use. 您可以在相同的类和程序集上实现,也可以使用更方便的方法,因为消息检查器是扩展,您可以配置要使用的内容(例如Web.config)。

IDispatchMessageInspector implements AfterReceiveRequest and BeforeSendReply methods, so you can intercept messages when you receive the request and before to send the reply, very usefull to your scenario. IDispatchMessageInspector实现AfterReceiveRequestBeforeSendReply方法,因此您可以在收到请求时以及发送答复之前拦截消息,这对您的情况非常有用。

Here is the MSDN message-inspectors documetation 这是MSDN消息检查器文档

A simple example of implementation: 一个简单的实现示例:

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage();
        var m = buffer.CreateMessage().ToString();
        return null;
    }

Note that you must copy the original message, so you can extract the XML and convert to an object to your pourpose (log, change, etc) 请注意,您必须复制原始消息,以便可以提取XML并将其转换为您想要的对象(日志,更改等)

If i understood well, you would like to intercept WCF data output before sending it to your client. 如果我理解得很好,您希望在将WCF数据输出发送到客户端之前先对其进行拦截。

there are various ways of doing that, what's in the link below is one of them : 有多种方法可以做到,下面的链接是其中之一:

https://www.codeguru.com/csharp/.net/net_wcf/learn-to-create-interceptors-in-wcf-services.htm https://www.codeguru.com/csharp/.net/net_wcf/learn-to-create-interceptors-in-wcf-services.htm

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM