最近用三层架构和工厂模式做了一个项目,借鉴了PetShop的框架。
出了个问题,搞了一天也没调试出来。想请高手指点下。
错误提示如题。我的MenuControl类如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using PDMS.Model;
using PDMS.IDAL;
using PDMS.DALFactory;
namespace PDMS.BLL
{
public class MenuControl
{
private static readonly IMenu dal = DataAccess.CreateMenu();//感觉问题出在这里 /// <summary>
/// 插入菜单实体
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public void Insert(MenuInfo model)
{
dal.Insert(model);
}
}
}//这个是DALFactory的DataAccess文件,主要是通过反射获得对应的程序集
using System.Reflection;
using System.Configuration;
using PDMS.IDAL;namespace PDMS.DALFactory
{
/// <summary>
/// 此类通过工厂模式创建DAL接口的实例
/// </summary>
public sealed class DataAccess
{
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"]; public static IMenu CreateMenu()
{
string className = path + ".MenuInfo";
return (IMenu)Assembly.Load(path).CreateInstance(className);
}
}
}//在Web.config里
<appSettings>
<add key="WebDAL" value="PDMS.SQLServerDAL"/>
</appSettings>
网上也搜了好多,有的说Web.config配置问题,但我的应该没错吧。
有的说程序集和命名空间不正确,但是我在各个程序集右键看属性时,程序集和默认命名空间都是PDMS.BLL,PDMS.SQLServerDAL之类的,应该没错吧
我解决方案的名称是PDMS。实在找不出哪里错了,请教了,谢谢。
出了个问题,搞了一天也没调试出来。想请高手指点下。
错误提示如题。我的MenuControl类如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using PDMS.Model;
using PDMS.IDAL;
using PDMS.DALFactory;
namespace PDMS.BLL
{
public class MenuControl
{
private static readonly IMenu dal = DataAccess.CreateMenu();//感觉问题出在这里 /// <summary>
/// 插入菜单实体
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public void Insert(MenuInfo model)
{
dal.Insert(model);
}
}
}//这个是DALFactory的DataAccess文件,主要是通过反射获得对应的程序集
using System.Reflection;
using System.Configuration;
using PDMS.IDAL;namespace PDMS.DALFactory
{
/// <summary>
/// 此类通过工厂模式创建DAL接口的实例
/// </summary>
public sealed class DataAccess
{
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"]; public static IMenu CreateMenu()
{
string className = path + ".MenuInfo";
return (IMenu)Assembly.Load(path).CreateInstance(className);
}
}
}//在Web.config里
<appSettings>
<add key="WebDAL" value="PDMS.SQLServerDAL"/>
</appSettings>
网上也搜了好多,有的说Web.config配置问题,但我的应该没错吧。
有的说程序集和命名空间不正确,但是我在各个程序集右键看属性时,程序集和默认命名空间都是PDMS.BLL,PDMS.SQLServerDAL之类的,应该没错吧
我解决方案的名称是PDMS。实在找不出哪里错了,请教了,谢谢。
调试一下,确保className正确 , 和创建的类型能否转为imenu
/// <summary>
/// 添加菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Page.IsValid == true)
{
MenuInfo mi = new MenuInfo();
mi.menuName = txtMenuName.Text.Trim();
mi.URL = txtMenuURL.Text;
mi.parentMenuID = Int32.Parse(txtParentID.Text.Trim());
mi.orderID = Int32.Parse(txtOrderID.Text.Trim());
mi.description = txtdescription.Text.Trim();
mi.menuLevel = Int32.Parse(ddlLevel.Text); MenuControl mc = new MenuControl();
mc.Insert(mi);
MessageBox.Show("添加成功");
}
}
我调试了一下,className正确。
未能加载文件或程序集“PDMS.SQLServerDAL”或它的某一个依赖项。系统找不到指定的文件。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IO.FileNotFoundException: 未能加载文件或程序集“PDMS.SQLServerDAL”或它的某一个依赖项。系统找不到指定的文件。源错误:
行 77: {
行 78: string className = path + ".MenuInfo";
行 79: return (IMenu)Assembly.Load(path).CreateInstance(className);
行 80: }
行 81:
源文件: G:\PDMSystem\DALFactory\DataAccess.cs 行: 79 程序集加载跟踪: 下列信息有助于确定程序集“PDMS.SQLServerDAL”无法加载的原因。
=== 预绑定状态信息 ===
日志: 用户 = BLUE\bluecountry
日志: DisplayName = PDMS.SQLServerDAL
(Partial)
日志: Appbase = file:///G:/PDMSystem/PDMS.Web/
日志: 初始 PrivatePath = G:\PDMSystem\PDMS.Web\bin
调用程序集: PDMS.DALFactory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null。
===
日志: 此绑定从 default 加载上下文开始。
日志: 正在使用应用程序配置文件: G:\PDMSystem\PDMS.Web\web.config
日志: 使用 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config 的计算机配置文件。
日志: 此时没有为引用应用策略(私有、自定义、分部或基于位置的程序集绑定)。
日志: 试图下载新的 URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/88af818b/2329883c/PDMS.SQLServerDAL.DLL。
日志: 试图下载新的 URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/88af818b/2329883c/PDMS.SQLServerDAL/PDMS.SQLServerDAL.DLL。
日志: 试图下载新的 URL file:///G:/PDMSystem/PDMS.Web/bin/PDMS.SQLServerDAL.DLL。
日志: 试图下载新的 URL file:///G:/PDMSystem/PDMS.Web/bin/PDMS.SQLServerDAL/PDMS.SQLServerDAL.DLL。
日志: 试图下载新的 URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/88af818b/2329883c/PDMS.SQLServerDAL.EXE。
日志: 试图下载新的 URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/88af818b/2329883c/PDMS.SQLServerDAL/PDMS.SQLServerDAL.EXE。
日志: 试图下载新的 URL file:///G:/PDMSystem/PDMS.Web/bin/PDMS.SQLServerDAL.EXE。
日志: 试图下载新的 URL file:///G:/PDMSystem/PDMS.Web/bin/PDMS.SQLServerDAL/PDMS.SQLServerDAL.EXE。 堆栈跟踪:
[FileNotFoundException: 未能加载文件或程序集“PDMS.SQLServerDAL”或它的某一个依赖项。系统找不到指定的文件。]
System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0
System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +54
System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +211
System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +141
System.Reflection.Assembly.Load(String assemblyString) +25
PDMS.DALFactory.DataAccess.CreateMenu() in G:\PDMSystem\DALFactory\DataAccess.cs:79
PDMS.BLL.MenuControl..cctor() in G:\PDMSystem\PDMS.BLL\MenuControl.cs:14[TypeInitializationException: “PDMS.BLL.MenuControl”的类型初始值设定项引发异常。]
PDMS.BLL.MenuControl..ctor() in G:\PDMSystem\PDMS.BLL\MenuControl.cs:15
PDMWeb.Manage.sys.menumanage.btnAdd_Click(Object sender, EventArgs e) in G:\PDMSystem\PDMS.Web\Manage\sys\menumanage.aspx.cs:44
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
这里面load的时候没找到该程序集
而不管在哪一层写这段代码其中的("程序集")读取的实际是web层bin文件夹下的dll,也就是说你反射的类的程序集dll在web层的bin下必须有
看来对反射理解得还是不够到位啊,呵呵