简体   繁体   中英

How to Resolve the Component Asp.net Web Api using autofac

I'm working in Asp.net Web Api with Autofac container. I have tried in various way to register and resolve the component but still encountering error. I have commented few other code to understand other approaches I have tried.

"An exception of type 'System.ArgumentNullException' occurred in Autofac.dll but was not handled in user code Additional information: Value cannot be null. occurred"

 public class DynamicController : ApiController
{
  public  EntityDetails Entity { get; set; }

    public DataAccessRegisteration datareg { get; set; }
    
   
        
    [HttpGet]
    public string CreateRetreiveEntities()
    {
      AutofacCofig.Container.Resolve<EntityDetails>().CreateRetreiveEntities();
      //  this.Entity.CreateRetreiveEntities();
       // this.datareg.contact.CreateContract();

        String clientdetails =ClientEntity.CreateRetreiveEntities();
      
       return clientdetails;
    }
}

 public class ContractEntityDetails
{
public String  CreateRetreiveEntities()
    {
           ContractEntity contactEntity = new ContractEntity();
              //    contactId= datareg.contract.CreateContract();
      
        return string.Empty;
        
    }
 }



public  class AutofacCofig:Module
    {
        public static IContainer Container { get; set; }
        public static void configure()
        {
            var builder = new ContainerBuilder();
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyModules<ModuleRegistration>();
        


            var container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
        }
    }
 public class WebApiApplication : System.Web.HttpApplication
 {
     protected void Application_Start()
    {
        //builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        //  builder.RegisterType<DynamicController>()
        //  .InstancePerRequest();
        //builder.RegisterType<DataAccessRegisteration>();
             AutofacCofig.configure();
          AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
       
         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
         RouteConfig.RegisterRoutes(RouteTable.Routes);
     }
}

I have identified the solution after some amount of search. Please find the code below. This Solution works for me to create Asp.Net Webapi with Autofac(DI)

Controller

 public class DynamicController : ApiController
 {
    public readonly IEntityDetails1 _entitydetails;
    public DynamicController (IEntityDetails1 entitydetails)
    {
        _entitydetails = entitydetails;
    }
     [HttpGet]
    public string CreateRetreiveClientEntities()
    {
      String details = this._entitydetails.CreateRetreiveEntities();
              
        return details;
    }
 }

EntityDetails

   public class EntityDetails : IEntityDetails1
  {
   public readonly IContractEntity _contractEntity;
   
    public EntityDetails(IContractEntity contractEntity)
    {
        _contractEntity = contractEntity;          
    }
    public String CreateRetreiveEntities()
    {
         contractId=  this._contractEntity.CreateConrtact();                                 
    return contractId;
        
    }

 }

Contract.cs

public class ContactEntity : IContactEntity
{
    
    Guid contactId = new Guid();
    public Guid CreateContact()
    {
         contactId = Api.SaveContract();
       return contactId.ToString();
     }
  }

Global.asax

   protected void Application_Start()
    {

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);           
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        ContainerBuilder builder = new ContainerBuilder();
        builder.RegisterType<EntityDetails>().As<IEntityDetails1>();
        builder.RegisterType<ContractEntity>().As<IContractEntity>();
         builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => 
          t.Name.EndsWith("Controller"));
        IContainer container = builder.Build();
        var resolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM