Ninject MVC3 ASP.NET CustomMembershipService 与 Sqlite 设置

发布于 2024-11-18 07:10:47 字数 1415 浏览 2 评论 0原文

我在配置自定义 asp.net 会员服务时遇到问题。

绑定模块时,我在应用程序启动时收到一条烦人的消息,如下所示:

this.Bind<RoleProvider>()
    .ToConstant(Roles.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

我检查了很多博客和诸如

ASP.NET:在应用程序的预启动初始化阶段无法调用此方法

.net 控制台应用程序生命周期 - 解决 pre -从 BuildManager.GetReferencedAssemblies 开始初始化错误

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

也但是没有任何成功。

以前有人遇到过这个错误消息吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-11-25 07:10:47

更改绑定代码将解决该问题。

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

只需让 SQLiteMembershipProviderSQLiteRoleProvider 实现 IProvider 即可。

如果你推迟初始化它就会恢复正常。

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.

红颜悴 2024-11-25 07:10:47

我也遇到了很大的问题。提供者的一些问题与 Ninject 不能很好地配合。我一直没弄清楚。我决定将其修复而不是注入。我将所有内容抽象为服务,并针对该服务类进行 Ninject 绑定。我最终得到了一个对我的实体框架成员资格提供程序进行硬编码使用的服务,如果我需要另一个提供程序,我将必须实现另一个对该提供程序进行硬编码的服务。

使用简单服务类进行注入是可行的,但不能通过 ToConstant() 直接绑定到提供程序。

    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;
        }
...

全局.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"]);

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"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文