开发者

Ninject MVC3 ASP.NET CustomMembershipService with Sqlite Setup

开发者 https://www.devze.com 2023-03-17 03:43 出处:网络
I ran into a problem configuring a custom asp.net membership service. I\'ve got an annoying message on application startup when binding modules, here it is :

I ran into a problem configuring a custom asp.net membership service.

I've got an annoying message on application startup when binding modules, here it is :

this.Bind<RoleProvider>()
    .ToConstant(Roles开发者_如何学C.Providers["SQLiteRoleProvider"]);
this.Bind<MembershipProvider>()
    .ToConstant(Membership.Providers["SQLiteMembershipProvider"]);

This method cannot be called during the application's pre-start initialization stage.
At 
>System.Web.dll!System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() 
in {System.Web.Compilation.BuildManager}   System.Web.Compilation.BuildManager

I've check a bunch of blogs and SO question like

ASP.NET: This method cannot be called during the application's pre-start initialization stage

.net console app lifecycle - working around a pre-start initialization error from BuildManager.GetReferencedAssemblies

http://weblogs.asp.net/leftslipper/archive/2010/07/28/migrating-asp-net-mvc-2-applications-to-asp-net-mvc-3-preview-1.aspx#7635403

also but haven't any success.

Has anyone encountered this error message before?


Changing the binding code will fix the problem.

this.Bind<RoleProvider>().ToProvider<SQLiteRoleProvider>();
this.Bind<MembershipProvider>().ToProvider<SQLiteMembershipProvider>();

Just make the SQLiteMembershipProvider and SQLiteRoleProvider implementing the IProvider.

If you postpone the initialization it's gonna get right.


I had huge problems with that too. Something about providers that doesn't work well with Ninject. I never figured it out. I decided to make it fixed rather than injected. I abstracted everything into a Service and made the Ninject bindings against this service class. I ended up with a Service that has hardcode use of my Entity Framework membership provider and if I need another provider I will have to implement another service that has that provider hardcoded.

Injection with a simple service class works but it does not with the ToConstant() direct binding to the provider.

    public class AccountMembershipService : IMembershipService
    {
        private readonly MembershipProvider _provider;
        private readonly IAccountRepository _accountRepository;
        private readonly IFirmsRepository _firmsRepository;
        private readonly IRepository<Client> _clientsRepository;

        public AccountMembershipService(IAccountRepository accountRepository, IFirmsRepository firmRepository,
                                        IRepository<Client> clientsRepository)
        {
            _provider = System.Web.Security.Membership.Providers["EfMembershipProvider"];
            _accountRepository = accountRepository;
            _firmsRepository = firmRepository;
            _clientsRepository = clientsRepository;
        }
...

global.asax.cs

Bind<IFormsAuthenticationService>().To<FormsAuthenticationService>();
Bind<IMembershipService>().To<AccountMembershipService>();
Bind<IAccountRepository>().To<EntityFrameworkAccountProvider>();
// never worked
//Bind<MembershipProvider>().ToConstant(System.Web.Security.Membership.Providers["EfMembershipProvider"]);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号