2012-06-10 8 views
11

Questo è il mio Global.asax.csAutofac - errore di registrazione automatica: No costruttori possono essere trovati con 'bandiere vincolanti pubblici'

public void RegisterContainersUsingAutofac() 
    { 
     //http://elegantcode.com/2009/01/07/ioc-libraries-compared/ 
     //http://www.codeproject.com/Articles/25380/Dependency-Injection-with-Autofac 
     //https://code.google.com/p/autofac/ 
     //http://api.autofac.org/ 
     var builder = new ContainerBuilder(); 
     builder.RegisterModelBinders(Assembly.GetExecutingAssembly()); 
     builder.RegisterModelBinderProvider(); 
     //builder.RegisterType<AuthenticationService>().As<IAuthenticationService>(); 
     builder.RegisterControllers(typeof(MvcApplication).Assembly); 

     //builder.RegisterAssemblyTypes(typeof(SecurityService).Assembly). 
     //  Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerHttpRequest(); 
     builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerLifetimeScope() 
      .OnRelease(x =>{((IUnitOfWork)x).Commit();}); 

     builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces(); 
     builder.RegisterAssemblyTypes(typeof(SecurityService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces(); 

     IContainer container = builder.Build(); 
     DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 
    } 

e sto avendo il seguente errore: Nessun costruttori sul tipo di ' AppBase.Service.UserService 'può essere trovato con' Public binding flags '.

Ho registrato le interfacce implementate di repository e servizi, ma dove ho sbagliato? Si rompe al seguente.

Questo è il mio userService

public interface IUserService 
{ 
    int Login(string userNameOrEmail, string password, out int errorcode, out int timeout); 
    bool Register(string userName, string password, string email, string country, string language, out int errorCode); 

    bool Register(User user, out int errorCode); 
    bool ResetPassword(string username, string email, string oldPassword, string newPassword, out int errorCode); 

    void DeleteUser(string userName); 
} 
public class UserService : IUserService 
{ 
     private readonly IUserRepository userRepository; 
    private UserService(IUserRepository _userRepository) 
    { 
     userRepository = _userRepository; 

    } 
    //other methods ... 

} 

Questo è il mio errore:

[DependencyResolutionException: No constructors on type 'AppBase.Service.UserService' can be found with 'Public binding flags'.] 
Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +261 
Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) +91 
Autofac.Core.Resolving.InstanceLookup.Execute() +135 
Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) +434 
Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) +61 
Autofac.Core.Activators.Reflection.<>c__DisplayClass2.<CanSupplyValue>b__0() +68 
Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() +201 
      Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +499 
Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) +91 
Autofac.Core.Resolving.InstanceLookup.Execute() +135 
Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) +434 
Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) +52 
Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) +115 
Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) +300 
Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) +103 
Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters) +218 
Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable`1 parameters) +64 
Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType) +43 
Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(Type serviceType) +49 
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +51 

[InvalidOperationException: An error occurred when trying to create a controller of type 'AppBase.Web.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor.] 
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182 
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80 
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74 
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +232 
System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49 
System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13 
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7 
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 
    System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124 
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98 
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50 
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8968180 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 

risposta

32

io non sono sicuro perché UserService ha un costruttore privato. C'è una ragione per questo?

Definendo un costruttore, il costruttore predefinito public UserService() non viene più generato.

Autofac verrà iniettato solo in costruttori pubblici, di cui ora non ne avete. Dovrebbe funzionare se lo si cambia in pubblico.

+0

Dannazione, ho trascurato quello ... Seriamente, grazie! – NicoJuicy

+0

Durr - questo mi ha fatto sentire un po 'meno intelligente :). – slashp

+0

Gah! Così ovvio una volta che lo vedi. – craftworkgames

2

@HackedByChinese la risposta è corretta.

Re-sharper ha suggerito di creare una classe nel mio abstract del livello di servizio. Ciò impedisce anche la generazione di un costruttore pubblico predefinito. Questo mi ha infastidito un po ', quindi ho pensato che potesse aiutare qualcun altro.

Problemi correlati