mercredi 22 juin 2016

"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?

Aucun commentaire:

Enregistrer un commentaire