C# 没有国际奥委会的AOP建议
我需要在现有的WCF服务中引入AOP,基本上用于异常包装和日志记录。 我需要一个.NET4(不是4.5)中的AOP框架,而不需要进行IOC注入,因为在现有代码中引入它的成本太高。 在我的研究中,我发现:C# 没有国际奥委会的AOP建议,c#,.net,wcf,aop,C#,.net,Wcf,Aop,我需要在现有的WCF服务中引入AOP,基本上用于异常包装和日志记录。 我需要一个.NET4(不是4.5)中的AOP框架,而不需要进行IOC注入,因为在现有代码中引入它的成本太高。 在我的研究中,我发现: 波斯夏普:非常好而且直截了当,但是有报酬,我需要一个免费的 NConcern:最新版本是为.NET4.5构建的,以前的版本有我的代码错误。它还与CNetpune一起工作,CNetpune修改程序集,而不是我想要的 Spring.NET:没有找到与国际奥委会进行AOP的方法 还有一些太老了,不能
- 波斯夏普:非常好而且直截了当,但是有报酬,我需要一个免费的
- NConcern:最新版本是为.NET4.5构建的,以前的版本有我的代码错误。它还与CNetpune一起工作,CNetpune修改程序集,而不是我想要的
- Spring.NET:没有找到与国际奥委会进行AOP的方法
- 还有一些太老了,不能再养了
有什么建议吗?您可以使用WCF拦截器实现AOP,如下所示:
/// <summary>
/// This class responsible to inspect service request and response
/// Service operations is also logged within this class
/// </summary>
internal class BaseServiceInspector : IClientMessageInspector, IDispatchMessageInspector, IParameterInspector
{
private Guid requestIdentifier;
private DateTime requestDate;
private string serviceResult = String.Empty;
private string consumerIPAddress = String.Empty;
private string operationName = String.Empty;
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
try
{
requestIdentifier = Guid.NewGuid();
requestDate = DateTime.Now;
operationName = ServiceHelper.GetServiceOperationName(OperationContext.Current);
consumerIPAddress = ServiceHelper.GetServiceConsumerIPAddress(OperationContext.Current);
return null;
}
catch
{
throw;
}
}
public object BeforeCall(string operationName, object[] inputs)
{
try
{
ServiceHelper.LogServiceIsCalled(operationName, requestIdentifier, consumerIPAddress, JsonConvert.SerializeObject(inputs, Formatting.Indented));
}
catch
{
//Dont break the flow for log exception
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
TimeSpan executeDuration = DateTime.Now - requestDate;
ServiceHelper.LogServiceIsExecuted(operationName, executeDuration.Milliseconds,requestIdentifier, consumerIPAddress, serviceResult);
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
}
首先创建BaseServiceInspector。实施IDispatchMessageInspector和IParameterInspector
使用方法AfterReceiveRequest初始化请求属性,BeforeCall在调用服务之前记录,以及AfterCall记录调用服务
我的实现是这样的:
/// <summary>
/// This class responsible to inspect service request and response
/// Service operations is also logged within this class
/// </summary>
internal class BaseServiceInspector : IClientMessageInspector, IDispatchMessageInspector, IParameterInspector
{
private Guid requestIdentifier;
private DateTime requestDate;
private string serviceResult = String.Empty;
private string consumerIPAddress = String.Empty;
private string operationName = String.Empty;
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
try
{
requestIdentifier = Guid.NewGuid();
requestDate = DateTime.Now;
operationName = ServiceHelper.GetServiceOperationName(OperationContext.Current);
consumerIPAddress = ServiceHelper.GetServiceConsumerIPAddress(OperationContext.Current);
return null;
}
catch
{
throw;
}
}
public object BeforeCall(string operationName, object[] inputs)
{
try
{
ServiceHelper.LogServiceIsCalled(operationName, requestIdentifier, consumerIPAddress, JsonConvert.SerializeObject(inputs, Formatting.Indented));
}
catch
{
//Dont break the flow for log exception
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
TimeSpan executeDuration = DateTime.Now - requestDate;
ServiceHelper.LogServiceIsExecuted(operationName, executeDuration.Milliseconds,requestIdentifier, consumerIPAddress, serviceResult);
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
}
希望对你有用
您还可以查看my github repo以了解详细的实施情况:
您可以使用WCF拦截器实现AOP,如下所示:
/// <summary>
/// This class responsible to inspect service request and response
/// Service operations is also logged within this class
/// </summary>
internal class BaseServiceInspector : IClientMessageInspector, IDispatchMessageInspector, IParameterInspector
{
private Guid requestIdentifier;
private DateTime requestDate;
private string serviceResult = String.Empty;
private string consumerIPAddress = String.Empty;
private string operationName = String.Empty;
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
try
{
requestIdentifier = Guid.NewGuid();
requestDate = DateTime.Now;
operationName = ServiceHelper.GetServiceOperationName(OperationContext.Current);
consumerIPAddress = ServiceHelper.GetServiceConsumerIPAddress(OperationContext.Current);
return null;
}
catch
{
throw;
}
}
public object BeforeCall(string operationName, object[] inputs)
{
try
{
ServiceHelper.LogServiceIsCalled(operationName, requestIdentifier, consumerIPAddress, JsonConvert.SerializeObject(inputs, Formatting.Indented));
}
catch
{
//Dont break the flow for log exception
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
TimeSpan executeDuration = DateTime.Now - requestDate;
ServiceHelper.LogServiceIsExecuted(operationName, executeDuration.Milliseconds,requestIdentifier, consumerIPAddress, serviceResult);
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
}
首先创建BaseServiceInspector。实施IDispatchMessageInspector和IParameterInspector
使用方法AfterReceiveRequest初始化请求属性,BeforeCall在调用服务之前记录,以及AfterCall记录调用服务
我的实现是这样的:
/// <summary>
/// This class responsible to inspect service request and response
/// Service operations is also logged within this class
/// </summary>
internal class BaseServiceInspector : IClientMessageInspector, IDispatchMessageInspector, IParameterInspector
{
private Guid requestIdentifier;
private DateTime requestDate;
private string serviceResult = String.Empty;
private string consumerIPAddress = String.Empty;
private string operationName = String.Empty;
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
try
{
requestIdentifier = Guid.NewGuid();
requestDate = DateTime.Now;
operationName = ServiceHelper.GetServiceOperationName(OperationContext.Current);
consumerIPAddress = ServiceHelper.GetServiceConsumerIPAddress(OperationContext.Current);
return null;
}
catch
{
throw;
}
}
public object BeforeCall(string operationName, object[] inputs)
{
try
{
ServiceHelper.LogServiceIsCalled(operationName, requestIdentifier, consumerIPAddress, JsonConvert.SerializeObject(inputs, Formatting.Indented));
}
catch
{
//Dont break the flow for log exception
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
TimeSpan executeDuration = DateTime.Now - requestDate;
ServiceHelper.LogServiceIsExecuted(operationName, executeDuration.Milliseconds,requestIdentifier, consumerIPAddress, serviceResult);
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
}
希望对你有用
您还可以查看my github repo以了解详细的实施情况:
我们通过免费版的PostSharp构建了这一功能。你试过了吗?PostSharp是付费的,我们负担不起,wa有10多个课程要处理。我现在明白了,太糟糕了,他们以前有一个免费版本,不限于10个课程。也许是Windows Castle?很好的动态城堡使工作,拦截器很容易实现。只需更改类实例化即可创建代理实例化。还需要将所有方法更改为“虚拟”。。。但我已经准备好了AOP。谢谢我们通过PostSharp的免费版本来构建它。你试过了吗?PostSharp是付费的,我们负担不起,wa有10多个课程要处理。我现在明白了,太糟糕了,他们以前有一个免费版本,不限于10个课程。也许是Windows Castle?很好的动态城堡使工作,拦截器很容易实现。只需更改类实例化即可创建代理实例化。还需要将所有方法更改为“虚拟”。。。但我已经准备好了AOP。谢谢