【问题标题】:Could not load file or assembly when loading WCF library in AppDomain在 AppDomain 中加载 WCF 库时无法加载文件或程序集
【发布时间】:2012-10-30 07:59:47
【问题描述】:

我有一个带有服务库的 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();
    }
}

Link to solution

【问题讨论】:

  • 您可能应该解决一些问题。首先,请不要只是在另一个站点上链接到您的解决方案,而是在问题中添加相关的代码 sn-ps。其次,告诉我们您尝试解决问题的方法,以及为什么没有成功。请注意,您可以随时修改您的问题。
  • 它告诉你哪些程序集不能被加载。请提供完整的异常信息
  • 您的参考文献之一在您期望的位置不存在。如果这是同一台机器 - 检查你没有引用某个地方的 obj/debug 文件夹中的机器 - 如果这是一台单独的机器(可能是构建服务器) - 检查你引用的是本地版本而不是 gac 版本。 (点击引用,在VS中查看其属性下的文件路径)
  • @Chris 为什么它有效? var _Factory = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(name, type_name) as Class1;
  • 这能给你带来什么价值?字符串名称 = typeof(Class1).Assembly.GetName().FullName;

标签: wcf appdomain


【解决方案1】:

//自托管解决了问题

Uri url = new Uri("http://localhost:7777/Service1");
using (ServiceHost host = new ServiceHost(typeof(Service1), url))
{
    host.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "");
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    host.Description.Behaviors.Add(smb);
    host.Open();
}

【讨论】:

    【解决方案2】:

    设置 AppDomains 基本目录和相关目录至少为我解决了这个问题

    string basePath = AppDomain.CurrentDomain.BaseDirectory;
    string relativePath = AppDomain.CurrentDomain.RelativeSearchPath;
    m_domain = AppDomain.CreateDomain("MyDomain", null, basePath, relativePath, false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      相关资源
      最近更新 更多