带有数据提供程序接口问题的 ASP.NET MVC2 自定义成员资格提供程序
我正在创建一个自定义成员资格提供程序类库,它定义了一个供我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终使用了 Assembly.CreateInstance,如下所示:
Factory:
RoleProvider:
在 web.config 中进行以下设置:
I ended up using Assembly.CreateInstance like so:
Factory:
RoleProvider:
With the following settings in web.config: