-2

サービスライブラリを備えたWCFサービスがあります。新しいAppDomainにクラスライブラリのインスタンスを作成したいのですが、これにより例外がスローされます。

ファイルまたはアセンブリ'ClassLibrary1.dll'またはその依存関係の1つを読み込めませんでした。

 //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();
    }
}

ソリューションへのリンク

4

2 に答える 2

0

//セルフホスティングで問題を解決

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();
}
于 2012-10-31T02:28:39.197 に答える