4

依存性注入にautofacを使用し、ORMとしてMindscapeのLightspeedを使用して、ASP.NETMVCWebサイトを作成しています。Lightspeed UnitOfWorkに依存し、Logonコントローラーにサービスを提供するUserRepositoryクラスがあります。

問題:UnitOfWorkは、UserRepositoryが使用を終了する前に破棄されます。

  public class UserRepository : IUserRepository
  {
    private readonly BluechipModelUnitOfWork _unitOfWork;

    public UserRepository(BluechipModelUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public Principal GetPrincipal(string name)
    {
        // This line throws an ObjectDisposedException - UnitOfWork is already disposed.
        return _unitOfWork.Principals.FirstOrDefault(p => p.Name == name);
    }
    ...

Global.asaxでは、依存関係の配線は次のように行われます。

public class MvcApplication : HttpApplication, IContainerProviderAccessor
{
    private static void RegisterAutofac()
    {
        var builder = new ContainerBuilder();

        // Register the lightspeed context as a singleton
        builder.RegisterInstance(new LightSpeedContext<BluechipModelUnitOfWork>("LightSpeedBluechip"))
            .As<LightSpeedContext<BluechipModelUnitOfWork>>()
            .SingleInstance();

        // Register the unit of work constructor so that a new instance is bound to each HttpRequest
        builder.Register(c => c.Resolve<LightSpeedContext<BluechipModelUnitOfWork>>().CreateUnitOfWork())
            .As<BluechipModelUnitOfWork>()
            .InstancePerLifetimeScope();

        // Register user repository to be one instance per HttpRequest lifetime
        builder.Register(c => new UserRepository(c.Resolve<BluechipModelUnitOfWork>()))
            .As<IUserRepository>()
            .InstancePerLifetimeScope();

        builder.Register(c => new CurrentUserService(
                                  c.Resolve<HttpSessionState>(),
                                  c.Resolve<IUserRepository>(),
                                  c.Resolve<IMembershipService>())
            ).As<ICurrentUserService>()
            .CacheInSession();

        builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
        builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired().InjectActionInvoker();
        builder.RegisterModelBinders(Assembly.GetExecutingAssembly());

        // Set the container provider up with registrations.    
        _containerProvider = new ContainerProvider(builder.Build());

        // Set the controller factory using the container provider.    
        ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(_containerProvider));

上記の登録を考えると、なぜautofacはUnitOfWorkを破棄するのでしょうか(

4

1 に答える 1

5

私は問題を突き止めることができました-それはばかげていますが微妙な落とし穴です...私は次のように登録していたCurrentUserServiceクラスを持っていました:

    builder.Register(c => new CurrentUserService(
                                  c.Resolve<HttpSessionState>(),
                                  c.Resolve<IUserRepository>(),
                                  c.Resolve<IMembershipService>())
            ).As<ICurrentUserService>()
            .CacheInSession();

問題はCacheInSession()です。これは、CurrentUserServiceがIUserRepositoryに依存しているためです。これは、autofacが忠実に注入していましたが、最初のリクエストの最後に破棄しました。

これにより、依存性注入を配線するときに注意すべき、明らかでありながら微妙なことが明らかになります。

高次の扶養家族は、彼らが依存しているサービスと常に同じかそれより短い存続期間を持っていることを確認してください。私の場合、解決策は上記のコードを変更することでした。

        builder.Register(c => new CurrentUserService(
                                  c.Resolve<HttpSessionState>(),
                                  c.Resolve<IUserRepository>(),
                                  c.Resolve<IMembershipService>())
            ).As<ICurrentUserService>()
            .InstancePerLifetimeScope();

....これにより、CurrentUserServiceが依存しているインスタンスを存続させることができなくなります。

于 2010-11-15T07:53:14.590 に答える