带有数据提供程序接口问题的 ASP.NET MVC2 自定义成员资格提供程序

发布于 2024-10-01 13:03:18 字数 1668 浏览 6 评论 0原文

我正在创建一个自定义成员资格提供程序类库,它定义了一个供我的 Web 项目实现的数据提供程序接口。

我在创建实现该接口的类的实例时遇到问题。

我在 web.config 中添加了一行来提取数据提供程序类型,但它正在成员资格库中查找该类型(但它在我的 Web 项目中定义)。

public class MvcRoleProvider : RoleProvider
{
    public string ConnectionString { get; set; }

    private IDataProvider _dataProvider;

    public override void Initialize(string name, NameValueCollection config)
    {
        var dataProviderType = config["dataProviderType"];
        ConnectionString = ConfigurationManager.AppSettings[config["connectionStringName"]];

        if (!string.IsNullOrEmpty(dataProviderType))
        {
            var type = Type.GetType(dataProviderType, true, true);

            if (type != null && !string.IsNullOrEmpty(ConnectionString))
                _dataProvider = (IDataProvider)Activator.CreateInstance(type, ConnectionString);
        }
    }

Web.config

    <roleManager enabled="true" defaultProvider="WebTestRoleProvider">
  <providers>
    <clear />
    <add name="WebTestRoleProvider"
         applicationName="WebTest"
         type="MvcMembership.MvcRoleProvider, MvcMembership"
         dataProviderType="MyWebNameSpace.MyWebProject.DataProviderClassName"
         connectionStringName="TheConnectionStringName" />
  </providers>
</roleManager>

错误:

Parser Error Message: Could not load type 'MyWebNameSpace.MyWebProject.DataProviderClassName' from assembly 'MvcMembership, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

我采取的方法是否正确?如果我想要通用的自定义会员资格库,我是否应该在我的网络/数据项目中实现适当的数据提供程序(在本示例中,我在网络中制作一个用于测试)?

I am creating a custom membership provider class library that defines a data provider interface for my web projects to implement.

I am having an issue creating an instance of the class that implements the interface.

I added a line in my web.config to pull the data provider type out, but it is looking for that type inside the membership library (but its defined in my web project).

public class MvcRoleProvider : RoleProvider
{
    public string ConnectionString { get; set; }

    private IDataProvider _dataProvider;

    public override void Initialize(string name, NameValueCollection config)
    {
        var dataProviderType = config["dataProviderType"];
        ConnectionString = ConfigurationManager.AppSettings[config["connectionStringName"]];

        if (!string.IsNullOrEmpty(dataProviderType))
        {
            var type = Type.GetType(dataProviderType, true, true);

            if (type != null && !string.IsNullOrEmpty(ConnectionString))
                _dataProvider = (IDataProvider)Activator.CreateInstance(type, ConnectionString);
        }
    }

Web.config

    <roleManager enabled="true" defaultProvider="WebTestRoleProvider">
  <providers>
    <clear />
    <add name="WebTestRoleProvider"
         applicationName="WebTest"
         type="MvcMembership.MvcRoleProvider, MvcMembership"
         dataProviderType="MyWebNameSpace.MyWebProject.DataProviderClassName"
         connectionStringName="TheConnectionStringName" />
  </providers>
</roleManager>

The error:

Parser Error Message: Could not load type 'MyWebNameSpace.MyWebProject.DataProviderClassName' from assembly 'MvcMembership, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Am I taking the right approach to this? If I want generic custom membership library, shouldn't I be implementing the appropriate data provider in my web/data projects (in this example I am making one in web for testing)?

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

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

发布评论

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

评论(1

救星 2024-10-08 13:03:18

我最终使用了 Assembly.CreateInstance,如下所示:

Factory:

    public class DataProviderFactory
{
    public IDataProvider GetProvider(string assemblyName, string type, string connectionString)
    {
        var assembly = System.Reflection.Assembly.Load(new AssemblyName(assemblyName));
        return (IDataProvider)assembly.CreateInstance(type, true, BindingFlags.CreateInstance, null, new object[]{ connectionString }, null, null);
    }
}

RoleProvider:

 public class MvcRoleProvider : RoleProvider
{
    public string ConnectionString { get; set; }

    private IDataProvider _dataProvider;

    public override void Initialize(string name, NameValueCollection config)
    {
        DataProviderFactory factory = new DataProviderFactory();
        _dataProvider = factory.GetProvider(config["dataProviderAssembly"], config["dataProviderType"], ConfigurationManager.AppSettings[config["connectionStringName"]]);

        base.Initialize(name, config);
    }

在 web.config 中进行以下设置:

<roleManager enabled="true" defaultProvider="MvcRoleProvider">
  <providers>
    <clear />
    <add name="MvcRoleProvider"
         applicationName="MyWebNameSpace"
         type="MvcMembership.MvcRoleProvider"
      dataProviderType="MyWebNameSpace.MyWebProject.DataProviderClassName"              dataProviderAssembly="EmailService.Web.EmailAdmin"
         connectionStringName="MyMembershipConnection" />
  </providers>
</roleManager>

I ended up using Assembly.CreateInstance like so:

Factory:

    public class DataProviderFactory
{
    public IDataProvider GetProvider(string assemblyName, string type, string connectionString)
    {
        var assembly = System.Reflection.Assembly.Load(new AssemblyName(assemblyName));
        return (IDataProvider)assembly.CreateInstance(type, true, BindingFlags.CreateInstance, null, new object[]{ connectionString }, null, null);
    }
}

RoleProvider:

 public class MvcRoleProvider : RoleProvider
{
    public string ConnectionString { get; set; }

    private IDataProvider _dataProvider;

    public override void Initialize(string name, NameValueCollection config)
    {
        DataProviderFactory factory = new DataProviderFactory();
        _dataProvider = factory.GetProvider(config["dataProviderAssembly"], config["dataProviderType"], ConfigurationManager.AppSettings[config["connectionStringName"]]);

        base.Initialize(name, config);
    }

With the following settings in web.config:

<roleManager enabled="true" defaultProvider="MvcRoleProvider">
  <providers>
    <clear />
    <add name="MvcRoleProvider"
         applicationName="MyWebNameSpace"
         type="MvcMembership.MvcRoleProvider"
      dataProviderType="MyWebNameSpace.MyWebProject.DataProviderClassName"              dataProviderAssembly="EmailService.Web.EmailAdmin"
         connectionStringName="MyMembershipConnection" />
  </providers>
</roleManager>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文