[英]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
DispatcherMessageInspector DispatcherMessageInspector
Dispatcher 调度员
Service Operation 服务运营
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.