C# 三层结构的 工厂模式的报错的 小问题
大侠们,这个总是报: 调用的目标发生了异常。 相信很多都遇到过这个问题,
我在网上也找了很久没看到什么解决的办法。
其报错的部分为: return (IYYMLoginDao)Assembly.Load(path).CreateInstance(className);
在方法里面:
public static IYYMLoginDao CreateYYMLoginDao()
{
string className = path + ".YYMLoginDao";
return (IYYMLoginDao)Assembly.Load(path).CreateInstance(className);
}
using 这些都导到里面去了的。
这个也写好了的。
private static readonly string path = ConfigurationManager.AppSettings["ServiceDAL"];
web.config 里面也配了的
<appSettings>
<add key="ServiceDAL" value="YYMAspNet_DAL"/>
</appSettings>
就是 不明白怎么还有这个错。。。
求大侠解决。、
在网页里面的错误可以看到:
未能加载文件或程序集“YYMAspNet_DB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
==============================================================================================
具体如下:
源错误:
行 13: public class YYMLoginDao:IYYMLoginDao
行 14: {
行 15: private IDBHelper helper = DBHelper.GetInstance();
行 16:
行 17:
源文件: C:\Users\yongmao\Documents\Visual Studio 2010\Projects\YYMAspNetWeb\YYMAspNet_DAL\YYMLoginDao.cs 行: 15
程序集加载跟踪: 下列信息有助于确定程序集“YYMAspNet_DB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”未能加载的原因。
警告: 程序集绑定日志记录被关闭。
要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1。
注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。
堆栈跟踪:
[FileNotFoundException: 未能加载文件或程序集“YYMAspNet_DB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。]
YYMAspNet_DAL.YYMLoginDao..ctor() in C:\Users\yongmao\Documents\Visual Studio 2010\Projects\YYMAspNetWeb\YYMAspNet_DAL\YYMLoginDao.cs:15
[TargetInvocationException: 调用的目标发生了异常。]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +1136
System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +111
System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +62
System.Reflection.Assembly.CreateInstance(String typeName) +22
YYMAspNet_DALFactory.DataAccess.CreateYYMLoginDao() in C:\Users\yongmao\Documents\Visual Studio 2010\Projects\YYMAspNetWeb\YYMAspNet_DALFactory\YYMLoginDaoFactory.cs:15
YYMAspNet_BLL.YYMLoginBll..ctor() in C:\Users\yongmao\Documents\Visual Studio 2010\Projects\YYMAspNetWeb\YYMAspNet_BLL\YYMLoginBll.cs:12
YYMAspNetWeb.Login..ctor() in C:\Users\yongmao\Documents\Visual Studio 2010\Projects\YYMAspNetWeb\YYMAspNetWeb\Login.aspx.cs:13
ASP.login_aspx..ctor() in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\8bfdeb29\c1c9e087\App_Web_thk0iqz2.0.cs:0
__ASP.FastObjectFactory_app_web_thk0iqz2.Create_ASP_login_aspx() in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\8bfdeb29\c1c9e087\App_Web_thk0iqz2.1.cs:0
System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) +109
System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +31
System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +40
System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +167
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +128
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
[解决办法]
你如果硬性添加了DAL项目的引用,那使用工厂模式就没多大意思了。不如直接new一个DAL对象出来。
如果你没有添加引用,那要确保你DAL项目的编译输出是指向的网站的bin目录而不是它自己的bin目录。
你的配置文件里的DAL名称是YYMAspNet_DAL, 但报错找不到的程序集是YYMAspNet_DB?
你确信这个配置是正确的吗?
[解决办法]
没什么引用了,就这几个就可以了,不过如果你还有个common 公共的项 的话也可以引用进去。