日期:2014-05-17 浏览次数:20419 次
public Test() { IUnityContainer unityContainer = new UnityContainer(); unityContainer.RegisterType<DbContext, Model.DefaultDbContext>("DefaultDbContext"); var dbContext = unityContainer.Resolve<DbContext>("DefaultDbContext"); }
private IUnityContainer unityContainer = null;
------解决方案--------------------
直接一个静态只读
C#单例=静态只读
上面这个等的也不知道是不是有问题,但是我确实在一本《.net面试宝典》的书上见过单例模式的写法
static readOnly
------解决方案--------------------
也可以写在Gloable.asax
------解决方案--------------------
嗯,改了昵称过来,灌水。
------解决方案--------------------
private const string AppContainerKey = "APPLICATION_CONTAINER"; protected void Application_Start(object sender, EventArgs e) { IUnityContainer applicationContainer = new UnityContainer(); applicationContainer.LoadConfiguration(); ApplicationContainer = applicationContainer; } public IUnityContainer ApplicationContainer { get { return (IUnityContainer)this.Application[AppContainerKey]; } set { this.Application[AppContainerKey] = value; } } //使用 namespace.ApplicationContainer得到实例