我有一个带有服务库的 WCF 服务。我想在新的 AppDomain 中创建一个类库的实例,但这会引发异常:
无法加载文件或程序集“ClassLibrary1.dll”或其依赖项之一。
//Class library
public class Class1 : MarshalByRefObject
{
public string Method1(int i)
{
return "int=" + i;
}
}
//Class WCF Service
public class Service1 : IService1
{
public string GetData(int value)
{
string name = typeof(Class1).Assembly.GetName().FullName;
string type_name = typeof(Class1).FullName;
var _Dom = AppDomain.CreateDomain("SubDomain", null, info);
//why is it not working?
//exception - not found assembly file
var _Factory = _Dom.CreateInstanceAndUnwrap(name, type_name) as Class1;
//it's worked
//var _Factory = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(name, type_name) as Class1;
return _Factory.Method1(value);
}
}
//Client method to service
static void Main(string[] args)
{
using (Service1Client cc = new Service1Client())
{
Console.WriteLine("Client opened.");
Console.Write("Enter integer: ");
int i = 0;
int.TryParse(Console.ReadLine(), out i);
try
{
var r = cc.GetData(i);
Console.WriteLine(r);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
cc.Close();
Console.WriteLine("Client closed. Press Enter key to exit...");
Console.ReadLine();
}
}