WCF DispatchMessageInspector - 处理并提供给操作 [英] WCF DispatchMessageInspector - Process and make available to operation

查看:170
本文介绍了WCF DispatchMessageInspector - 处理并提供给操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个DispatchMessageInspector为我的WCF服务,将每次操作前运行,执行一些处理,然后使该处理可供操作的结果。

I want to create a DispatchMessageInspector for my WCF service that will run before each operation, perform some processing and then make the result of that processing available to the operation.

创建MessageInspector容易。然而,当我做什么,我需要做的还有,在那里我可以把我打造,因此它可以用code在每个操作被访问的对象?在MessageInspector,将我只是把它存储在OperationConext,或者是有一个更清洁的解决方案?

Creating the MessageInspector is easy. However, after I do what I need to do there, where can I place the object that I create so it can be accessed by the code in each operation? In the MessageInspector, would I just store it in the OperationConext, or is there a cleaner solution?

推荐答案

你通常存储在邮件属性信息,然后通过对操作的操作环境访问它(请参阅下面的code为例)。

You'd normally store this information on the message properties, then access it via the operation context on the operation (see an example in the code below).

public class StackOverflow_7534084
{
    const string MyPropertyName = "MyProp";
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            Console.WriteLine("Information from the inspector: {0}", OperationContext.Current.IncomingMessageProperties[MyPropertyName]);
            return text;
        }
    }
    public class MyInspector : IEndpointBehavior, IDispatchMessageInspector
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            request.Properties[MyPropertyName] = "Something from the inspector";
            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "").Behaviors.Add(new MyInspector());
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.Echo("Hello"));

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

这篇关于WCF DispatchMessageInspector - 处理并提供给操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆