当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - "InvalidOperationException" Exceptions while Creating MVC Web API

问题描述:

I am trying to create a MVC API using repository pattern. Following is my code

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

}

}

}

I am also using following code to register it with a container

var container = new UnityContainer();

container.RegisterType<IInsurerDAL, InsurerDAL>(new HierarchicalLifetimeManager());

GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

still I am getting following issue when I try to use this Web API :

Resolution of the dependency failed, type =

\"InformationServices.API.Controllers.InsurerController\", name =

\"(none)\".\r\nException occurred while: while resolving.\r\nException

is: InvalidOperationException - The current type,

InformationServices.Repository.IInsurerRepository, is an interface and

cannot be constructed. Are you missing a type mapping?

网友答案:

You're pointing and resolving to other container which doesn't have the registration of your interface to the concrete class. You should make them the same container.

Abstract steps:

  1. You create and set the UnityContainer as you're default DependencyResolver Container
  2. You register the mapping between types inside the container. i.e container.Register<IInsurerDAL, InsurerDal>
  3. You're using Constructor Dependency Injection by passing desired type object. i.e InsurerRepository(IInsurerDAL insurerDal)
  4. You saving\using the resolved object to you're desires. - Not mandatory.

Actual code changes:

Open you're Global.asax and add

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

And at you're InsurerRepository constructor remove to the following lines:

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

And change you're InsurerRepository constructor to the following using Dependency injection principle.

public InsurerRepository(IInsurerDAL insurerDal) {
    oInsurerDAL = insurerDal;

And change the InsurerDAL oInsurerDAL; field to IInsurerDAL oInsurerDAL;

分享给朋友:
您可能感兴趣的文章:
随机阅读: