我在使用Unity 注入时,其他暂时都没有问题。能够正常使用。但是当在
//这是使用接口的静态类
public static class ExceptionHander
{
public static IBLL.ISysExceptionBLL Bll { set; get; }//在这个地方调试时 Bll为null,如果去掉类中所以static静态状态就能正常使用。
//调试时提示 Bll.Create(model) “未将对象引用设置到对象的实例。”--Bll=null。
public static void writeError()
{
SysException model = new SysException()
{
Id = Utils.CreateGuidID,
Message = ex.Message,
CreateTime = Utils.NowDateTime
};
Bll.Create(model);
}
}
//只是接口
public interface ISysExceptionBLL
{
int Create(SysException entity);
}
我的Unity注入代码:
public class DependencyRegisterType
{
//系统注入
public static void Container_Sys(ref UnityContainer container)
{
container.RegisterType<ISysExceptionBLL, SysExceptionBLL>();
container.RegisterType<ISysExceptionDAL, SysExceptionDAL>();
}
}
Global中注入IOC
protected void Application_Start()
{
//注入 Ioc
var container = new UnityContainer();
DependencyRegisterType.Container_Sys(ref container);
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
请帮帮忙看看,后面还有一个问题要请教。在线等待中。谢谢!
您好,
你可以直接吧static 去掉.