" InvalidOperationException异常"创建MVC Web API时的异常

时间:2016-06-23 02:12:17

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 asp.net-web-api

我正在尝试使用存储库模式创建MVC API。以下是我的代码

IInsurerDAL.cs

namespace InformationServices.DAL
{
    public interface IInsurerDAL
    {
        Task<DataTable> GetInsurer(string cover,string version);
    }
}

InsurerDAL.cs

namespace InformationServices.DAL
{
    public class InsurerDAL : IInsurerDAL
    {
        int commandTimeout;

    public InsurerDAL()
    {
        Int32.TryParse(ConfigurationManager.AppSettings["CommandTimeout"], out commandTimeout);
        if (commandTimeout <= 0)
        {
            commandTimeout = 30;
        }
    }
    public async Task<DataTable> GetInsurer(string cover, string version)
    {
                //some code 
    }    
    }
}

IInsurerRepository.cs

namespace InformationServices.Repository
{
    public interface IInsurerRepository
    {
        ConsumerDetails ConsumerDetails { get; set; }
        Task<ResponseModel<Insurer>> GetInsurer(string cover, string version);
    }
}

InsurerRepository.cs

namespace InformationServices.Repository
{
    public class InsurerRepository : IInsurerRepository, IDisposable
    {
        InsurerDAL oInsurerDAL;
        ConsumerDetails oConsumerDetails { get; set; }

        public ConsumerDetails ConsumerDetails
        {
            get
            {
                return oConsumerDetails;
            }
            set
            {
                oConsumerDetails = value;
            }
        }

        public InsurerRepository()
        {
            var container = new UnityContainer();
            oInsurerDAL = container.Resolve<InsurerDAL>();

            MapperRegistry.Mappers.Add(new DataReaderMapper());

            //To load the DataReaderMapper before we actually use it.
            var type = typeof(DataReaderMapper);
        }
        public async Task<ResponseModel<Insurer>> GetInsurer(string cover, string version)
        {

        }
        //Implementation of IDisposable interfase.
        protected void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (oInsurerDAL != null)
                {
                    oInsurerDAL = null;
                }
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}

InsurerController.cs

    namespace InformationServices.API.Controllers
{
    [RoutePrefix("V1")]
    [ControllerExceptionFilter]
    public class InsurerController : ApiController
    {
    private IInsurerRepository InsurerRepository;

    private static ILog logger = log4net.LogManager.GetLogger(typeof(InsurerController));

    public InsurerController(IInsurerRepository repository)
    {
        InsurerRepository = repository;
        InsurerRepository.ConsumerDetails = CommonFunctions.FetchConsumerDataFromHeader();
    }


    [HttpGet]
    [HttpOptions]
    public async Task<IHttpActionResult> GetInsurer(string cover, string version, string srcKey)
    {
    //code here
}
}

}

我也使用以下代码将其注册到容器

var container = new UnityContainer();
container.RegisterType<IInsurerDAL, InsurerDAL>(new HierarchicalLifetimeManager());
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

当我尝试使用此Web API时,我仍然遇到以下问题:

  

依赖项的解析失败,type =   \&#34; InformationServices.API.Controllers.InsurerController \&#34;,name =   \&#34;(无)\&#34;。\ r \ n发生以下情况时发生异常:解析时。\ r \ nException   is:InvalidOperationException - 当前类型,   InformationServices.Repository.IInsurerRepository,是一个接口和   无法建造。你错过了类型映射吗?

1 个答案:

答案 0 :(得分:0)

您指向并解析其他容器,该容器没有将您的界面注册到具体类。你应该把它们变成同一个容器。

抽象步骤:

  1. 您创建并设置UnityContainer,因为您的默认 DependencyResolver容器
  2. 您在容器内注册类型之间的映射。 container.Register<IInsurerDAL, InsurerDal>
  3. 您通过传递所需的类型对象来使用Constructor Dependency Injection InsurerRepository(IInsurerDAL insurerDal)
  4. 使用已解析的对象将您保存为“您希望”。 - 不是强制性的。
  5. 实际代码更改:

    打开你 Global.asax 并添加

    var container = new UnityContainer();
    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    

    在你的 InsurerRepository 构造函数中删除以下行:

    var container = new UnityContainer();
    oInsurerDAL = container.Resolve<InsurerDAL>();
    

    使用依赖注入原则将您的 InsurerRepository 构造函数更改为以下内容。

    public InsurerRepository(IInsurerDAL insurerDal) {
        oInsurerDAL = insurerDal;
    

    并将InsurerDAL oInsurerDAL;字段更改为IInsurerDAL oInsurerDAL;