我在DALFactory中的一个类
public static OnlineServer.IDAL.IIndex1 CreateIndex()
{
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
string className = path + ".Index1";
return (OnlineServer.IDAL.IIndex1)Assembly.Load(path).CreateInstance(className);
}
WebDAL是在Web工程下web.config节点
<appSettings>
<add key="WebDAL" value="OnlineServer.SQLServerDAL" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="OnlineServer.SQLServerDAL" fullName="OnlineServer.SQLSererDAL,version=1.0.0.0,publicKeyToken=null,culture=neutral"/>
</assemblyBinding>
</runtime>
然后在BLL中调用的类是
public DataTable GetIndexTable(string dbfname)
{
IIndex1 dal=OnlineServer.DALFactory.Index1Create.CreateIndex();
return dal.GetIndexTable(dbfname);
}
但我在web工程下调用BLL类时,非要在web工程的引用中添加OnlineServer.SQLServerDAL.dll这个类,否则它说找不到OnlineServer.SQLServerDAL.dll
但我看到Petshop3.0下没有引用这个类库也可以实现呀
请大家帮我看看
谢谢
public static OnlineServer.IDAL.IIndex1 CreateIndex()
{
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
string className = path + ".Index1";
return (OnlineServer.IDAL.IIndex1)Assembly.Load(path).CreateInstance(className);
}
WebDAL是在Web工程下web.config节点
<appSettings>
<add key="WebDAL" value="OnlineServer.SQLServerDAL" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="OnlineServer.SQLServerDAL" fullName="OnlineServer.SQLSererDAL,version=1.0.0.0,publicKeyToken=null,culture=neutral"/>
</assemblyBinding>
</runtime>
然后在BLL中调用的类是
public DataTable GetIndexTable(string dbfname)
{
IIndex1 dal=OnlineServer.DALFactory.Index1Create.CreateIndex();
return dal.GetIndexTable(dbfname);
}
但我在web工程下调用BLL类时,非要在web工程的引用中添加OnlineServer.SQLServerDAL.dll这个类,否则它说找不到OnlineServer.SQLServerDAL.dll
但我看到Petshop3.0下没有引用这个类库也可以实现呀
请大家帮我看看
谢谢
希望MVP大大们都进来看看
解释一下我这个问题出在什么地方
(OnlineServer.IDAL.IIndex1)Assembly.Load(path).CreateInstance(className);
但我改成用Assembly.LoadForm传入Dll的绝对地址,问题还是解决不了
>>>但我看到Petshop3.0下没有引用这个类库也可以实现呀is it a compile error or runtime error?where is OnlineServer.IDAL.IIndex1 defined? check if you are using any interface defined in OnlineServer.SQLServerDAL.dll
public interface IIndex1
{
DataTable GetIndexTable(string dbfname);//获取index表
}SQLServerDAL:包括Index1.cs
//引用IDAL.dll
public class Index1:IIndex1
{
//获得某一大型的全部模架(整行)
public DataTable GetIndexTable(string dbfname)
{
DataTable dt;
Database db=new Database();//处理数据库操作的类
SqlParameter[] prams = { db.MakeInParam("@dbfname", SqlDbType.VarChar, 10, dbfname),
};dt=db.RunProcedure("getindex",prams,"Index1").Tables["Index1"];
return dt;
}
}WebDAL是在Web工程下web.config节点
<appSettings>
<add key="WebDAL" value="OnlineServer.SQLServerDAL" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="OnlineServer.SQLServerDAL" fullName="OnlineServer.SQLSererDAL,version=1.0.0.0,publicKeyToken=null,culture=neutral"/>
</assemblyBinding>
</runtime>IDALFactory :包括Index1Create.cs
//引用IDAL.dll
public class Index1Create
{
public static OnlineServer.IDAL.IIndex1 CreateIndex()
{
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
string className = path + ".Index1";
return (OnlineServer.IDAL.IIndex1)Assembly.Load(path).CreateInstance(className);
}
}BLL类中:Index1.cs
//引用IDAL.dll和DALFactory.dll
public class Index1
{
//获取Index表
public DataTable GetIndexTable(string dbfname)
{
IIndex1 dal=OnlineServer.DALFactory.Index1Create.CreateIndex();
return dal.GetIndexTable(dbfname);
}
}
我的整个解决方案中工程的编译顺序是:
IDAL=>SQLServerDAL=>DALFactory=>BLL=>WEB然后在我的web工程中
如果只引用BLL.dll的话,它说找不到OnlineServer.SQLServerDAL.dll麻烦再看一下啦
谢谢
如果这个问题不解决的话
我觉得我用工厂模式根本就没有意义
我的web工程直接跟SQLServerDAL打交道就可以解决问题了
>>>但运行就出错
前面跟后面好象不符合啊,因为前者是编译时的问题如果运行时出错的话,你的OnlineServer.SQLServerDAL.dll在bin目录里么?不管怎么样,Assembly.Load需要在它查访的路径上能找到OnlineServer.SQLServerDAL.dll啊
Assembly.Load时有问题
但我但是用Assembly.LoadForm传入OnlineServer.SQLServerDAL.dll的绝对地址,问题还是解决不了
IDALFactory :包括Index1Create.cs
//引用IDAL.dll
public class Index1Create
{
public static OnlineServer.IDAL.IIndex1 CreateIndex()
{Assembly asm = Assembly.LoadFrom(@"E:\CSharp\OnlineServer\SQLServerDAL\bin\Debug\OnlineServer.SQLServerDAL.dll");
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
string className = path + ".Index1";
Type t = asm.GetType(className);
return (OnlineServer.IDAL.IIndex1)Activator.CreateInstance(t);
}
}
出错的原因是一摸一样的,即提示找不到程序集OnlineServer.SQLServerDAL.dll
using System.Text;namespace IDAL
{
public interface IMyObject
{
void GetValue(StringBuilder sb);
}
}//compile to IDAL.dll
csc /t:library IDAL.cs2. DALFac.csusing System;
using System.Reflection;
using IDAL;namespace DALFac
{ public class MyObjectFactory
{
public static IMyObject CreateObject()
{
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
string className = "DAL.MyImp";
return (IMyObject)Assembly.Load(path).CreateInstance(className);
}
}
}//compile to DALFac.dll, note, it only references IDAL.dll
csc /t:library /r:IDAL.dll DALFac.cs3. DAL.cs
using System;
using System.Text;
using IDAL;namespace DAL
{
public class MyImp : IMyObject
{
public void GetValue(StringBuilder sb)
{
sb.Append("DAL.MyImp.GetValue().......");
}
}
}//compile to DAL.dll, note, it only references IDAL.dll
csc /t:library /r:IDAL.dll DAL.cs
4. BLL.cs
using System;
using System.Text;
using IDAL;namespace BLL
{
public class MyObject
{
public string GetValue()
{
IMyObject o = DALFac.MyObjectFactory.CreateObject();
StringBuilder sb = new StringBuilder();
sb.Append("BLL.MyObject.GetValue()....");
o.GetValue(sb);
return sb.ToString();
}
}
}
//compile to BLL.dll, note, it only references IDAL.dll, DALFac.dll
csc /t:library /r:IDAL.dll,DALFac.dll BLL.cs
F:\html>move *.dll bin
F:\html\BLL.dll
F:\html\DAL.dll
F:\html\DALFac.dll
F:\html\IDAL.dll5. web.config
<configuration>
<appSettings>
<add key="WebDAL" value="DAL" />
</appSettings>
</configuration>6. TestBLL.aspx
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
BLL.MyObject o = new BLL.MyObject();
Response.Write(o.GetValue());
}
</script>7. outputBLL.MyObject.GetValue()....DAL.MyImp.GetValue().......
而且实现的方法跟你的demo基本上没什么两样
但还是出现这个问题
不知道是怎么回事
assembly.load这个函数load哪里的dll?不理解.....
那个petshop3.0,安装完毕后,我搜索了整个硬盘,都没搜到那个sqlserver的dll,但却能运行,
assembly.load这个函数load哪里的dll?不理解.....--------------------------------------------------------
可能安装到GAC里了。 不清楚猜的
系统盘:\WINNT\assembly中通过PetShop.IDAL.IAccount Create()等的Create创建他们的实例;
如果装好后先把PetShop源程序备份,然后卸载,之后再直接去运行源程序的话,就会提示找不到PetShop.SQLServerDAL等,因为这些控件从全局程序集文件夹中卸载了
怎么装进去的啊