本文介绍了没有IOC的AOP建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在现有的WCF服务中引入AOP,基本上用于异常包装和日志记录.我需要.NET 4(而不是4.5)中的AOP框架,而无需进行IOC注入,因为在现有代码中引入它的成本太高.在我的研究中,我发现了以下内容:

I need to introduce AOP in an existing WCF service, basically for exception wrapping and logging.I need an AOP framework in .NET 4 (not 4.5), without need to do IOC injection because it will be too costly to introduce in existing code.In my research I found the following:

  • PostSharp:非常好,直接,但是要付费,我需要一个免费的
  • NConcern:最新版本是针对.NET 4.5的版本,以前的版本对我的代码而言是错误的.它也可以与CNetpune一起使用,后者可以修改程序集,而不完全是我想要的.
  • Spring.NET:找不到使用IOC进行AOP的方法
  • 其他人太老了,不再维护了.

有什么建议吗?

推荐答案

您可以使用WCF拦截器来实现AOP,如下所示:

You can implement your AOP with using WCF Interceptors like this:

首先创建您的BaseServiceInspector.实施 IDispatchMessageInspector IParameterInspector

First create your BaseServiceInspector. Implement IDispatchMessageInspector and IParameterInspector

使用方法 AfterReceiveRequest 初始化请求属性,使用 BeforeCall 记录服务调用之前的日志,使用 AfterCall 记录服务调用的日志.

Use methods AfterReceiveRequest to initialize you request properties, BeforeCall to log before service is calling and AfterCall to log Service is Called.

我的实现是这样的:

/// <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;

    }
}

然后创建您的方面

/// <summary>
/// Customized service behavior for service types. If services are designed as WCF service
/// All services should have this attribute
/// </summary>
public class BaseServiceBehavior : Attribute, IServiceBehavior
{

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        BaseServiceInspector baseServiceInspector = new BaseServiceInspector();
        foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
            {
                endpointDispatcher.DispatchRuntime.MessageInspectors.Add(baseServiceInspector);

                foreach (var operation in endpointDispatcher.DispatchRuntime.Operations)
                {
                    operation.ParameterInspectors.Add(baseServiceInspector);
                }
            }
        }
    }
}

最后将您的方面添加到服务中:

Finally add your Aspect to Service:

[BaseServiceBehavior]
public class MathService : IMathService
{
    //...
}

希望它对您有用.

更多拦截器的信息

您也可以检查我的github存储库以了解详细实现:

You can check my github repo as well for detail implementation:

服务框架/

这篇关于没有IOC的AOP建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:16