1

我们正在开发一个基于

  • .NET 4.5.1
  • MVC 5.2.2
  • 欧文
  • WebAPI 2.2
  • SignalR 2.2.0
  • 温莎城堡 3.3.0
  • Wcf 集成工具 3.3.0

为了解决控制器,我们使用下面页面中描述的 ControllerFactory 类:http: //docs.castleproject.org/Windsor.Windsor-tutorial-part-two-plugging-Windsor-in.ashx

为了解决依赖关系,我们使用 WindsorDependencyResolver 类:

public class WindsorDependencyResolver : IDependencyResolver
{
    public IWindsorContainer Container { get; private set; }

    public WindsorDependencyResolver(IWindsorContainer windsorContainer)
    {
        Container = windsorContainer;
    }

    public IDependencyScope BeginScope()
    {
        return new WindsorDependencyScope(this.Container);
    }

    public object GetService(Type serviceType)
    {
        return this.Container.Kernel.HasComponent(serviceType) ? this.Container.Resolve(serviceType) : null;
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return this.Container.ResolveAll(serviceType).Cast<object>().ToArray();
    }

    public void Dispose()
    {
    }
}

public class WindsorDependencyScope : IDependencyScope
{
    public IWindsorContainer Container { get; set; }
    public IDisposable Scope { get; set; }

    public WindsorDependencyScope(IWindsorContainer container)
    {
        this.Container = container;
        this.Scope = container.BeginScope();
    }

    public object GetService(Type serviceType)
    {
        return this.Container.Kernel.HasComponent(serviceType) ? this.Container.Resolve(serviceType) : null;
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return this.Container.ResolveAll(serviceType).Cast<object>().ToArray();
    }

    public void Dispose()
    {
        this.Scope.Dispose();
    }
}

请记住,我们不会使用 Windsor 容器解析 SignalR 的 IHub 类,它们是由 OWIN 系统在管道中实例化的。Startup.cs 代码如下所示:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        app.MapSignalR();
    }
}

所有控制器、wcf 服务客户端和拦截器(日志记录类除外)都在项目中使用 LifestylePerWebRequest 注册。然而,我们用于记录的类是单例的。

下面的 Web.config 中有一个设置:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        ...
        <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
        ...
    </modules>
</system.webServer>

因此,当我们尝试在 SignalR 集线器中解析 wcf 客户端(具有每个 Web 请求的生活方式)时,我们会得到以下异常:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Scope cache was already disposed. This is most likely a bug in the calling code.'.
   at Castle.MicroKernel.Lifestyle.Scoped.ScopeCache.get_Item(Object id)
   at Castle.MicroKernel.Lifestyle.Scoped.DefaultLifetimeScope.GetCachedInstance(ComponentModel model, ScopedInstanceActivationCallback createInstance)
   at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
   at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Resolve(Type service, IDictionary arguments)
   at Castle.Windsor.WindsorContainer.Resolve[T]()
   at UIServer.WebUI.Hubs.MailThreadHub.Broadcast(MailMessageListDto mailMessage) in c:\Development\DDD\UIServer.WebUI\Hubs\MailThreadHub.cs:line 92

在调用 Container.Resolve() 方法之前,我可以在调试器窗口中看到 HttpContext。顺便说一句,我可以解决单例日志记录类。

有趣的一点是我的队友没有任何例外。主要区别在于我们的操作系统版本。我在 windows 8.1 中运行代码,我的队友在 windows 7 中运行它。

我们仅针对信号器集线器获得此例外。我们在其他任何地方都没有例外。我们如何解决这个问题?

4

1 回答 1

0

我在 Startup 类中使用 ServiceLocator 来解析 IDependencyResolver。它看起来像:

internal class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        var config = new HttpConfiguration();
        config.Routes.MapHttpRoute("ActionApi", "{controller}/{action}/{id}", new {id = RouteParameter.Optional});

        config.DependencyResolver = ServiceLocator.Instance.Resolve<IDependencyResolver>();

        appBuilder.UseWebApi(config);
    }
}

也许是帮助。

于 2015-02-16T13:17:56.877 回答