简体   繁体   English

WCF服务中的操作筛选器

[英]Action Filters in WCF Services

Is there something Like ActionFilterAttribute (From ASP.NET MVC) in WCF Services (Or anything like that). 是否有像WCF服务中的ActionFilterAttribute (来自ASP.NET MVC)(或类似的东西)。 basically what I want to do is to log what is coming and goint to and from my services, and I don't want to write the logging code in every single ServiceContract s. 基本上我想要做的是记录来自我的服务的内容和goint,我不想在每个ServiceContract编写日志代码。 yes the question is very general but you understand the idea what I want to do. 是的,这个问题非常笼统,但你理解我想做的事情。

Yes there is it called as MessageInspectors/ParameterInspectors , here you can read about them http://msdn.microsoft.com/en-us/library/aa717047%28v=vs.110%29.aspx 是的,它被称为MessageInspectors / ParameterInspectors,在这里你可以阅读它们http://msdn.microsoft.com/en-us/library/aa717047%28v=vs.110%29.aspx

This is exactly what you are looking for , WCF custom behavior log logging http://www.codeproject.com/Articles/243352/LoggingBehavior-How-to-Connect-Log-prints-with-the 这正是您正在寻找的,WCF自定义行为日志记录http://www.codeproject.com/Articles/243352/LoggingBehavior-How-to-Connect-Log-prints-with-the

Only confusing thing is you can have message inspector on WCF service and WCF proxy as well , in your case you need only for service side 只有令人困惑的事情是你可以在WCF服务和WCF代理上有消息检查器,在你的情况下你只需要服务端

I had to read a lot to find this out, I'm not an expert in WCF but given this information is a little scarce I'm sharing what have worked for me. 我必须阅读很多才能找到这个,我不是WCF的专家,但鉴于这些信息有点稀缺,我正在分享对我有用的东西。

My Solution consists of using an OperationBehavior and a DispatcherMessageInspector 我的解决方案包括使用OperationBehavior和DispatcherMessageInspector

OperationBehavior OperationBehavior

  • Allows you to change the binding information, validate de operation description, and apply dispatcher behaviors. 允许您更改绑定信息,验证de操作描述以及应用调度程序行为。

DispatcherMessageInspector DispatcherMessageInspector

  • Allows you to inspect and change the messages that are sent for your service. 允许您检查和更改为您的服务发送的消息。

Dispatcher 调度员

  • Gets the messages from the communication channels and sends to the right operation, and get the result back to the caller. 从通信通道获取消息并发送到正确的操作,并将结果返回给调用者。

Service Operation 服务运营

  • are your service methods 是你的服务方式

CODE SOLUTION 代码解决方案

MESSAGE INSPECTOR 消息检查员

   public class MyMessageInspector : IDispatchMessageInspector
    {
        List<string> targetOperations = new List<string>();

        public MyMessageInspector(OperationDescription operation)
        {
            this.AddOperation(operation);
        }

        public void AddOperation(OperationDescription operation)
        {
            this.targetOperations.Add(operation.Messages[0].Action);
        }

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            if (TargetOperationMatchesRequest(request))
            {
                request = ChangeMessage(request);

                return true;
            }else
            {
                return false;
            }
        }


        public bool TargetOperationMatchesRequest(Message request)
        {
            string requestAction = request.Headers.To.AbsolutePath;
            requestAction =  requestAction.Substring(requestAction.LastIndexOf("/"));

            string targetOperation = "";
            foreach (string targetOperationPath in targetOperations)
            {
                targetOperation = targetOperationPath.Substring(targetOperationPath.LastIndexOf("/"));
                if (targetOperation.Equals(requestAction))
                {
                    return true;
                }
            }
            return false;
        }


        public Message ChangeMessage(Message oldMessage)
        {
            Message newMessage = request.CreateBufferedCopy(Int32.MaxValue).CreateMessage();
            //Change your message

            return newMessage;

        }


        public void BeforeSendReply(ref Message reply, object correlationState)
        {
        }
    }

OPERATION 操作

public class MyOperation : Attribute, IOperationBehavior
    {
        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
        }

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

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {

            MyMessageInspector inspector = dispatchOperation.Parent.MessageInspectors
                .Where(x => x is MyMessageInspector)
                .FirstOrDefault() as MyMessageInspector;

            if (inspector != null)
            {
                inspector.AddOperation(operationDescription);
            }
            else
            {
                inspector = new MessageInspectors(operationDescription);
                dispatchOperation.Parent.MessageInspectors.Add(inspector);
            }
        }

        public void Validate(OperationDescription operationDescription)
        {
        }
    }

CONTRACT 合同

[ServiceContract]
public interface IService
{
    [OperationContract]
    [MyOperation]
    OutputData MyMethod(InputData inputData);
}

SERVICE 服务

    public class Service : IService
    {
            [WebInvoke(Method = "POST", UriTemplate = "/json/MyMethod", RequestFormat = WebMessageFormat.Json,  ResponseFormat = WebMessageFormat.Json)]
            public OutputData MyMethod(InputData inputData)
            {
               //Implementation
               return new OutputData();

            }
    }

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

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