我一直在关注Steven Sanderson的名为Pro ASP.NET MVC Framework的书,我遇到了一个例外:
Could not load type 'DomainModel.Abstract.IProductsRepository' from assembly 'DomainModel'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.TypeLoadException: Could not load type 'DomainModel.Abstract.IProductsRepository' from assembly 'DomainModel'
Line 19: public WindsorControllerFactory()
Line 20: {
Line 21: container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
Line 22:
Line 23: var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes() where typeof(IController).IsAssignableFrom(t) select t;
这是我的WindsorControllerFactory代码:
public class WindsorControllerFactory : DefaultControllerFactory
{
WindsorContainer container;
public WindsorControllerFactory()
{
container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes() where typeof(IController).IsAssignableFrom(t) select t;
foreach (Type t in controllerTypes)
container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
}
我的Global.asax.cs代码:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
}
web.config值:
<configSections>
< section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
<component id="ProdsRepository" service="DomainModel.Abstract.IProductsRepository, DomainModel"
type="DomainModel.Concrete.SqlProductsRepository, DomainModel">
<parameters>
< connectionString>data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|SportsStore.mdf;User Instance=true< /connectionString>
</parameters>
</component>
</components>
</castle>
我的问题是为什么无法从程序集“DomainModel”加载类型“DomainModel.Abstract.IProductsRepository”。 ?
答案 0 :(得分:1)
此错误的可能解释是IProductsRepository
接口不是public
和/或未置于DomainModel.Abstract
程序集中的DomainModel
命名空间内。确保定义看起来像这样,并且它是DomainModel
项目的一部分:
namespace DomainModel.Abstract
{
public interface IProductsRepository
{
...
}
}
对于您定义的DomainModel.Concrete.SqlProductsRepository, DomainModel
类,情况也是如此。