如何使用 MessageInspector 在 WCF 上实现编排

发布于 2024-11-15 04:03:51 字数 153 浏览 1 评论 0 原文

我有一个 WCF 服务,我使用 MessageInspector (继承 IDispatchMessageInspector)检查它,

我想在运行服务之前做一些事情,结果我不想运行服务。

我想阻止客户端调用,但客户端没有收到异常。

你能帮助我吗

I have a WCF Service, And I inspect that using MessageInspector ( inherit IDispatchMessageInspector)

I want to do some thing before running service and result of that ı want not run service.

I want to prevent client call but client dont receive exception.

Can you help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-11-22 04:03:51

此方案类似于 MSDN WCF 论坛中标题为“IDispatchMessageInspector.AfterReceiveRequest - 跳过操作并手动生成自定义响应”。如果这是您所需要的(当您在检查器中收到消息时,您决定要跳过服务操作,但向客户端返回消息并且客户端不应该看到异常),那么这个答案应该适用于你也是。请注意,您需要以与客户端期望的格式相同的格式创建响应消息,否则您将遇到异常。

此代码使用三个(许多)WCF 扩展点来实现该场景:一个消息检查器(正如您所提到的您正在使用的)、一个消息格式化程序和一个操作调用程序。我在关于 WCF 扩展性的持续系列博客中介绍了它们,网址为 http://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx

public class Post_55ef7692_25dc_4ece_9dde_9981c417c94a
{
    [ServiceContract(Name = "ITest", Namespace = "http://tempuri.org/")]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }
    }
    static Binding GetBinding()
    {
        BasicHttpBinding result = new BasicHttpBinding();
        return result;
    }
    public class MyOperationBypasser : IEndpointBehavior, IOperationBehavior
    {
        internal const string SkipServerMessageProperty = "SkipServer";
        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(new MyInspector(endpoint));
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.Formatter = new MyFormatter(dispatchOperation.Formatter);
            dispatchOperation.Invoker = new MyInvoker(dispatchOperation.Invoker);
        }

        public void Validate(OperationDescription operationDescription)
        {
        }

        class MyInspector : IDispatchMessageInspector
        {
            ServiceEndpoint endpoint;
            public MyInspector(ServiceEndpoint endpoint)
            {
                this.endpoint = endpoint;
            }

            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                Message result = null;
                HttpRequestMessageProperty reqProp = null;
                if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
                {
                    reqProp = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
                }

                if (reqProp != null)
                {
                    string bypassServer = reqProp.Headers["X-BypassServer"];
                    if (!string.IsNullOrEmpty(bypassServer))
                    {
                        result = Message.CreateMessage(request.Version, this.FindReplyAction(request.Headers.Action), new OverrideBodyWriter(bypassServer));
                    }
                }

                return result;
            }

            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                Message newResult = correlationState as Message;
                if (newResult != null)
                {
                    reply = newResult;
                }
            }

            private string FindReplyAction(string requestAction)
            {
                foreach (var operation in this.endpoint.Contract.Operations)
                {
                    if (operation.Messages[0].Action == requestAction)
                    {
                        return operation.Messages[1].Action;
                    }
                }

                return null;
            }

            class OverrideBodyWriter : BodyWriter
            {
                string bypassServerHeader;
                public OverrideBodyWriter(string bypassServerHeader)
                    : base(true)
                {
                    this.bypassServerHeader = bypassServerHeader;
                }

                protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
                {
                    writer.WriteStartElement("EchoResponse", "http://tempuri.org/");
                    writer.WriteStartElement("EchoResult");
                    writer.WriteString(this.bypassServerHeader);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
        }

        class MyFormatter : IDispatchMessageFormatter
        {
            IDispatchMessageFormatter originalFormatter;
            public MyFormatter(IDispatchMessageFormatter originalFormatter)
            {
                this.originalFormatter = originalFormatter;
            }

            public void DeserializeRequest(Message message, object[] parameters)
            {
                if (message.Properties.ContainsKey(MyOperationBypasser.SkipServerMessageProperty))
                {
                    Message returnMessage = message.Properties[MyOperationBypasser.SkipServerMessageProperty] as Message;
                    OperationContext.Current.IncomingMessageProperties.Add(MyOperationBypasser.SkipServerMessageProperty, returnMessage);
                    OperationContext.Current.OutgoingMessageProperties.Add(MyOperationBypasser.SkipServerMessageProperty, returnMessage);
                }
                else
                {
                    this.originalFormatter.DeserializeRequest(message, parameters);
                }
            }

            public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
            {
                if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(MyOperationBypasser.SkipServerMessageProperty))
                {
                    return null;
                }
                else
                {
                    return this.originalFormatter.SerializeReply(messageVersion, parameters, result);
                }
            }
        }

        class MyInvoker : IOperationInvoker
        {
            IOperationInvoker originalInvoker;

            public MyInvoker(IOperationInvoker originalInvoker)
            {
                if (!originalInvoker.IsSynchronous)
                {
                    throw new NotSupportedException("This implementation only supports synchronous invokers");
                }

                this.originalInvoker = originalInvoker;
            }

            public object[] AllocateInputs()
            {
                return this.originalInvoker.AllocateInputs();
            }

            public object Invoke(object instance, object[] inputs, out object[] outputs)
            {
                if (OperationContext.Current.IncomingMessageProperties.ContainsKey(MyOperationBypasser.SkipServerMessageProperty))
                {
                    outputs = null;
                    return null; // message is stored in the context
                }
                else
                {
                    return this.originalInvoker.Invoke(instance, inputs, out outputs);
                }
            }

            public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
            {
                throw new NotSupportedException();
            }

            public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
            {
                throw new NotSupportedException();
            }

            public bool IsSynchronous
            {
                get { return true; }
            }
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
        endpoint.Behaviors.Add(new MyOperationBypasser());
        foreach (var operation in endpoint.Contract.Operations)
        {
            operation.Behaviors.Add(new MyOperationBypasser());
        }

        host.Open();
        Console.WriteLine("Host opened");

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

        Console.WriteLine("And now with the bypass header");
        using (new OperationContextScope((IContextChannel)proxy))
        {
            HttpRequestMessageProperty httpRequestProp = new HttpRequestMessageProperty();
            httpRequestProp.Headers.Add("X-BypassServer", "This message will not reach the service operation");
            OperationContext.Current.OutgoingMessageProperties.Add(
                HttpRequestMessageProperty.Name,
                httpRequestProp);
            Console.WriteLine(proxy.Echo("Hello"));
        }

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

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

This scenario looks like the post in the MSDN WCF forum entitled "IDispatchMessageInspector.AfterReceiveRequest - skip operation and manually generate custom response instead". If this is what you need (when you receive a message in the inspector, you decide that you want to skip the service operation, but return a message to the client and the client should not see an exception), then this answer should work for you as well. Notice that you'll need to create the response message in the same format as the client expects, otherwise you'll have an exception.

This code uses three of the (many) WCF extensibility points to achieve that scenario, a message inspector (as you've mentioned you're using), a message formatter and an operation invoker. I've blogged about them in an ongoing series about WCF extensibility at http://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx.

public class Post_55ef7692_25dc_4ece_9dde_9981c417c94a
{
    [ServiceContract(Name = "ITest", Namespace = "http://tempuri.org/")]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }
    }
    static Binding GetBinding()
    {
        BasicHttpBinding result = new BasicHttpBinding();
        return result;
    }
    public class MyOperationBypasser : IEndpointBehavior, IOperationBehavior
    {
        internal const string SkipServerMessageProperty = "SkipServer";
        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(new MyInspector(endpoint));
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.Formatter = new MyFormatter(dispatchOperation.Formatter);
            dispatchOperation.Invoker = new MyInvoker(dispatchOperation.Invoker);
        }

        public void Validate(OperationDescription operationDescription)
        {
        }

        class MyInspector : IDispatchMessageInspector
        {
            ServiceEndpoint endpoint;
            public MyInspector(ServiceEndpoint endpoint)
            {
                this.endpoint = endpoint;
            }

            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                Message result = null;
                HttpRequestMessageProperty reqProp = null;
                if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
                {
                    reqProp = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
                }

                if (reqProp != null)
                {
                    string bypassServer = reqProp.Headers["X-BypassServer"];
                    if (!string.IsNullOrEmpty(bypassServer))
                    {
                        result = Message.CreateMessage(request.Version, this.FindReplyAction(request.Headers.Action), new OverrideBodyWriter(bypassServer));
                    }
                }

                return result;
            }

            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                Message newResult = correlationState as Message;
                if (newResult != null)
                {
                    reply = newResult;
                }
            }

            private string FindReplyAction(string requestAction)
            {
                foreach (var operation in this.endpoint.Contract.Operations)
                {
                    if (operation.Messages[0].Action == requestAction)
                    {
                        return operation.Messages[1].Action;
                    }
                }

                return null;
            }

            class OverrideBodyWriter : BodyWriter
            {
                string bypassServerHeader;
                public OverrideBodyWriter(string bypassServerHeader)
                    : base(true)
                {
                    this.bypassServerHeader = bypassServerHeader;
                }

                protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
                {
                    writer.WriteStartElement("EchoResponse", "http://tempuri.org/");
                    writer.WriteStartElement("EchoResult");
                    writer.WriteString(this.bypassServerHeader);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
        }

        class MyFormatter : IDispatchMessageFormatter
        {
            IDispatchMessageFormatter originalFormatter;
            public MyFormatter(IDispatchMessageFormatter originalFormatter)
            {
                this.originalFormatter = originalFormatter;
            }

            public void DeserializeRequest(Message message, object[] parameters)
            {
                if (message.Properties.ContainsKey(MyOperationBypasser.SkipServerMessageProperty))
                {
                    Message returnMessage = message.Properties[MyOperationBypasser.SkipServerMessageProperty] as Message;
                    OperationContext.Current.IncomingMessageProperties.Add(MyOperationBypasser.SkipServerMessageProperty, returnMessage);
                    OperationContext.Current.OutgoingMessageProperties.Add(MyOperationBypasser.SkipServerMessageProperty, returnMessage);
                }
                else
                {
                    this.originalFormatter.DeserializeRequest(message, parameters);
                }
            }

            public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
            {
                if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(MyOperationBypasser.SkipServerMessageProperty))
                {
                    return null;
                }
                else
                {
                    return this.originalFormatter.SerializeReply(messageVersion, parameters, result);
                }
            }
        }

        class MyInvoker : IOperationInvoker
        {
            IOperationInvoker originalInvoker;

            public MyInvoker(IOperationInvoker originalInvoker)
            {
                if (!originalInvoker.IsSynchronous)
                {
                    throw new NotSupportedException("This implementation only supports synchronous invokers");
                }

                this.originalInvoker = originalInvoker;
            }

            public object[] AllocateInputs()
            {
                return this.originalInvoker.AllocateInputs();
            }

            public object Invoke(object instance, object[] inputs, out object[] outputs)
            {
                if (OperationContext.Current.IncomingMessageProperties.ContainsKey(MyOperationBypasser.SkipServerMessageProperty))
                {
                    outputs = null;
                    return null; // message is stored in the context
                }
                else
                {
                    return this.originalInvoker.Invoke(instance, inputs, out outputs);
                }
            }

            public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
            {
                throw new NotSupportedException();
            }

            public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
            {
                throw new NotSupportedException();
            }

            public bool IsSynchronous
            {
                get { return true; }
            }
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
        endpoint.Behaviors.Add(new MyOperationBypasser());
        foreach (var operation in endpoint.Contract.Operations)
        {
            operation.Behaviors.Add(new MyOperationBypasser());
        }

        host.Open();
        Console.WriteLine("Host opened");

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

        Console.WriteLine("And now with the bypass header");
        using (new OperationContextScope((IContextChannel)proxy))
        {
            HttpRequestMessageProperty httpRequestProp = new HttpRequestMessageProperty();
            httpRequestProp.Headers.Add("X-BypassServer", "This message will not reach the service operation");
            OperationContext.Current.OutgoingMessageProperties.Add(
                HttpRequestMessageProperty.Name,
                httpRequestProp);
            Console.WriteLine(proxy.Echo("Hello"));
        }

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

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文