1

我在将 Ninject WCF 扩展与拦截扩展相结合的项目中遇到了问题。基本上取决于我如何设置我的配置,我会得到一些预期之外的不同对象生命周期结果。

我有一些测试代码,其中包含几个显示对象创建和处置的 Debug 语句。我在创建 IInterceptor 时看到了一些意外行为。属性拦截,只要我在绑定中正确设置生命周期,一切都会按照我期望的方式运行。但是我对绑定拦截感兴趣,例如:

        kernel.Bind<MyAspect>().ToSelf().InRequestScope();
        kernel.Bind<Service1>().ToSelf().InRequestScope().Intercept().With<MyAspect>();

我希望看到在每个 Wcf 服务调用(即虚拟的)上创建和处理 Aspect。我看到的是实际调用的第一个调用上的 dispose,然后我没有看到再次调用 create,但它调用了 MyAspect 类的原始实例。

我不知道在拦截器生命周期方面是否有最佳实践,但是我使用它的情况是应用于此程序集(多个服务)中的每个方法调用,用于许多事情,例如日志记录,一些节流等

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1 : IService1, IDisposable
{
    public virtual string GetData(int value)
    {
        Debug.WriteLine("GetData {0},{1}", value, _id);
        return string.Format("You entered: {0},{1}", value, _id);
    }

    private Guid _id;

    public Service1()
    {
        _id = Guid.NewGuid();
        Debug.WriteLine("Service1 constructed {0}", _id);
    }

    public void Dispose()
    {
        Debug.WriteLine("Service1 disposed {0}", _id);
    }

    ~Service1()
    {
        Debug.WriteLine("Service1 finalized {0}", _id);
    }
}

[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface IService1
{
    [OperationContract]
    string GetData(int value);
}
public class MyAspect:IInterceptor, IDisposable
{
    private Guid _id = Guid.NewGuid();
    public MyAspect()
    {
        Debug.WriteLine("MyAspect create {0}", _id);
    }

    public void Intercept(IInvocation invocation)
    {
        Debug.WriteLine("MyAspect Intercept {0}", _id);
        invocation.Proceed();
    }

    public void Dispose()
    {
        Debug.WriteLine("MyAspect dispose {0}", _id);
    }
}

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            RegisterServices(kernel);
            return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<MyAspect>().ToSelf().InRequestScope();
        kernel.Bind<Service1>().ToSelf().InRequestScope().Intercept().With<MyAspect>();
    }        
}
4

0 回答 0