Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Unity.mvc3-未为HierarchyCallifeTimeManager管理的一次性类调用Dispose方法_Asp.net Mvc_Asp.net Mvc 3_Dependency Injection_Unity Container_Unit Of Work - Fatal编程技术网

Asp.net mvc Unity.mvc3-未为HierarchyCallifeTimeManager管理的一次性类调用Dispose方法

Asp.net mvc Unity.mvc3-未为HierarchyCallifeTimeManager管理的一次性类调用Dispose方法,asp.net-mvc,asp.net-mvc-3,dependency-injection,unity-container,unit-of-work,Asp.net Mvc,Asp.net Mvc 3,Dependency Injection,Unity Container,Unit Of Work,下面是我的代码,问题是UnitOfWork类的dispose方法没有被调用。对于DI,我在Asp.net Mvc3应用程序中使用Unity v2.1.505和Unity.Mvc3 v1.2 [assembly: PreApplicationStartMethod(typeof(Program), "Initialize")] namespace Practice.DependencyResolution.Concrete { public class Program { private

下面是我的代码,问题是UnitOfWork类的dispose方法没有被调用。对于DI,我在Asp.net Mvc3应用程序中使用Unity v2.1.505和Unity.Mvc3 v1.2

[assembly: PreApplicationStartMethod(typeof(Program), "Initialize")]

namespace Practice.DependencyResolution.Concrete
{
public class Program
{
    private static IUnityContainer container;

    public static void Initialize()
    {
        if (container == null) container = new UnityContainer();

        string databaseSource = Settings.Default.DatabaseSource;

        var dependencyMapperType = Type.GetType("Practice.DependencyResolution.Concrete." + databaseSource + "DependencyMapper", true);
        var dependencyMapper = (IDependencyMapper)Activator.CreateInstance(dependencyMapperType);
        var dependencyMapperContext = new DependencyMapperContext(dependencyMapper);
        dependencyMapperContext.MapDependencies(container);


        ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container));
        var locator = new UnityServiceLocator(container);
        ServiceLocator.SetLocatorProvider(() => locator);
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}
}

internal class DependencyMapperContext
{
    private IDependencyMapper dependencyMapper;

    public DependencyMapperContext(IDependencyMapper dependencyMapper)
    {
        this.dependencyMapper = dependencyMapper;
    }

    public void MapDependencies(IUnityContainer container)
    {
        dependencyMapper.MapDependencies(container);

    }
}

internal class AnyDependencyMapper : IDependencyMapper
{
    public void MapDependencies(IUnityContainer container)
    {
        container.RegisterType<ISupplierRepository, SupplierRepository>();
        container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
    }
}

public class UnitOfWork : IUnitOfWork
{
    private readonly TransactionScope transactionScope;
    private readonly ModelDataContext context;
    private bool disposed = false;

    public UnitOfWork()
    {
        transactionScope = new TransactionScope();
        this.context = new ModelDataContext();
    }

    ModelDataContext IUnitOfWork.Context
    {
        get
        {
            Debug.WriteLine("context get called");

            return context;
        }
    }

    public void Commit()
    {
        if (disposed) throw new ObjectDisposedException("transactionScope");
        transactionScope.Complete();
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposed == false)
        {
            if (disposing)
            {
                if (context != null)
                {
                    context.Dispose();
                }

                if (transactionScope != null)
                {
                    transactionScope.Dispose();
                }
                disposed = true;
            }
        }
    }

    public void Dispose()
    {
        Debug.WriteLine("Access dispose called");
        if (HttpContext.Current != null && HttpContext.Current.Error != null)
        {
            //transaction transactionScope will be disposed automatically, do nothing
        }
        else
        {
            Commit();
        }

        Dispose(true);

        GC.SuppressFinalize(this);
    }
}

 public class SupplierRepository : ISupplierRepository
{
    private readonly IUnitOfWork unitOfWork;
    private bool disposed = false;

    public SupplierRepository(IUnitOfWork unitOfWork)
    {
        this.unitOfWork = unitOfWork;
    }

    public IList<SupplierItem> GetAll()
    {
        return unitOfWork.Context.SupplierItems.ToList();
    }

    public SupplierItem GetById(object id)
    {
        return unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == (int)id);
    }

    public void Insert(SupplierItem entity)
    {
        unitOfWork.Context.SupplierItems.InsertOnSubmit(entity);
        unitOfWork.Context.SubmitChanges();
    }

    public void Delete(object id)
    {
        var supplier = unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == (int)id);
        unitOfWork.Context.SupplierItems.DeleteOnSubmit(supplier);
        unitOfWork.Context.SubmitChanges();
    }

    public void Delete(SupplierItem entityToDelete)
    {
        Delete(entityToDelete.SupplierID);
    }

    public void Update(SupplierItem entityToUpdate)
    {
        var supplier = unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == entityToUpdate.SupplierID);
        supplier.Address = entityToUpdate.Address;
        supplier.City = entityToUpdate.City;
        supplier.CompanyName = entityToUpdate.CompanyName;
        supplier.ContactName = entityToUpdate.ContactName;
        supplier.ContactTitle = entityToUpdate.ContactTitle;
        supplier.Country = entityToUpdate.Country;
        supplier.Fax = entityToUpdate.Fax;
        supplier.HomePage = entityToUpdate.HomePage;
        supplier.Phone = entityToUpdate.Phone;
        supplier.PostalCode = entityToUpdate.PostalCode;
        supplier.Region = entityToUpdate.Region;
        unitOfWork.Context.SubmitChanges();
    }

    public SupplierItem GetDefault()
    {
        return new SupplierItem();
    }
}
[程序集:预应用程序启动方法(程序类型),“初始化”)]
命名空间Practice.DependencyResolution.Concrete
{
公共课程
{
专用静态IUnityContainer容器;
公共静态void Initialize()
{
如果(container==null)container=newunitycontainer();
字符串databaseSource=Settings.Default.databaseSource;
var dependencyMapperType=Type.GetType(“Practice.dependencysolution.Concrete.”+databaseSource+“DependencyMapper”,true);
var dependencyMapper=(IDependencyMapper)Activator.CreateInstance(dependencyMapperType);
var dependencyMapperContext=新的dependencyMapperContext(dependencyMapper);
DependencyApperContext.MapDependencies(容器);
ControllerBuilder.Current.SetControllerFactory(新UnityControllerFactory(容器));
var定位器=新的UnityServiceLocator(容器);
ServiceLocator.SetLocatorProvider(()=>locator);
SetResolver(新UnitedDependencyResolver(容器));
}
}
}
内部类依赖项ApperContext
{
私人IDependencyMapper依赖者apper;
public DependencyApperContext(IDependencyMapper DependencyApper)
{
this.dependencyMapper=dependencyMapper;
}
公共void映射依赖项(IUnityContainer容器)
{
dependencyMapper.MapDependencies(容器);
}
}
内部类AnyDependencyMapper:IDependencyMapper
{
公共void映射依赖项(IUnityContainer容器)
{
container.RegisterType();
RegisterType(新的层次结构CallifetimeManager());
}
}
公共类UnitOfWork:IUnitOfWork
{
私有只读事务范围事务范围;
私有只读ModelDataContext上下文;
私有布尔=假;
公共工作单元()
{
transactionScope=新transactionScope();
this.context=new ModelDataContext();
}
ModelDataContext IUnitOfWork.Context
{
得到
{
WriteLine(“上下文被调用”);
返回上下文;
}
}
公共无效提交()
{
如果(已处置)抛出新的ObjectDisposedException(“transactionScope”);
transactionScope.Complete();
}
受保护的虚拟void Dispose(bool disposing)
{
if(disposed==false)
{
如果(处置)
{
if(上下文!=null)
{
context.Dispose();
}
if(transactionScope!=null)
{
transactionScope.Dispose();
}
这是真的;
}
}
}
公共空间处置()
{
Debug.WriteLine(“调用了Access dispose”);
if(HttpContext.Current!=null&&HttpContext.Current.Error!=null)
{
//事务处理范围将自动处理,不执行任何操作
}
其他的
{
提交();
}
处置(真实);
总干事(本);
}
}
公共类SupplierRepository:ISupplierRepository
{
私有只读IUnitOfWork;
私有布尔=假;
公共供应商存储库(IUnitOfWork unitOfWork)
{
this.unitOfWork=unitOfWork;
}
公共IList GetAll()
{
返回unitOfWork.Context.SupplierItems.ToList();
}
公共供应商项目GetById(对象id)
{
返回unitOfWork.Context.SupplierItems.SingleOrDefault(a=>a.SupplierID==(int)id);
}
公共作废插入(供应商项目实体)
{
unitOfWork.Context.SupplierItems.InsertOnSubmit(实体);
unitOfWork.Context.SubmitChanges();
}
公共无效删除(对象id)
{
var supplier=unitOfWork.Context.SupplierItems.SingleOrDefault(a=>a.SupplierID==(int)id);
unitOfWork.Context.SupplierItems.DeleteOnSubmit(供应商);
unitOfWork.Context.SubmitChanges();
}
公共作废删除(SupplierItem entityToDelete)
{
删除(entityToDelete.SupplierID);
}
公共作废更新(供应商项目实体更新)
{
var supplier=unitOfWork.Context.SupplierItems.SingleOrDefault(a=>a.SupplierID==entityToUpdate.SupplierID);
supplier.Address=entityToUpdate.Address;
supplier.City=entityToUpdate.City;
supplier.CompanyName=entityToUpdate.CompanyName;
supplier.ContactName=entityToUpdate.ContactName;
supplier.ContactTitle=entityToUpdate.ContactTitle;
supplier.Country=entityToUpdate.Country;
supplier.Fax=entityToUpdate.Fax;
supplier.HomePage=entityToUpdate.HomePage;
supplier.Phone=entityToUpdate.Phone;
supplier.PostalCode=entityToUpdate.PostalCode;
supplier.Region=entityToUpdate.Region;
unitOfWork.Context.SubmitChanges();
}
公共供应商项目GetDefault()
{
返回新的SupplierItem();
}
}

我不熟悉DI和Unity,提前感谢。

我确实听说您正在使用MVC 3。尽管如此,如果您有可能更新到MVC4,那么新的Unity 3支持MVC开箱即用,并与HierarchyCallifeTimeManager配合使用


不过,我不熟悉Unity.Mvc3 NuGet软件包(Microsoft不支持该软件包)。

谢谢Julian,但我在vs2010中使用的是.net framework 4,它不支持Unity版本3,我只能使用Unity版本2。