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

时间:2010-11-08 16:46:22

标签: c# asp.net asp.net-mvc-2 custom-membershipprovider

我正在创建一个自定义成员资格提供程序类库,它定义了我要实现的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'.

我采取了正确的方法吗?如果我想要通用的自定义成员资格库,我不应该在我的网络/数据项目中实现适当的数据提供者(在这个例子中,我在网上制作一个用于测试)?

1 个答案:

答案 0 :(得分:0)

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

厂:

    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>