2014-01-14 28 views
2

我將創建簡單的程序Console App C#.NET 4.0以便遠程加載DLL文件。 我的代碼正常工作負載DLL上的本地計算機,但我得到的問題解決DLL試圖遠程加載時。 我不知道要處理這個,因爲我找不到任何遠程加載dll的代碼示例。如何在新的AppDomain中遠程加載.NET DLL

我得到的錯誤如下。

Could not load file or assembly 'http://codesanook.cloudapp.net/dll/ZupZip.Lib.I 
nterfaces.dll' or one of its dependencies. Operation is not supported. (Exceptio 
n from HRESULT: 0x80131515) 
    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String cod 
eBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& 
stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntro 
spection, Boolean suppressSecurityChecks) 
    at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String code 
Base, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& s 
tackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntros 
pection, Boolean suppressSecurityChecks) 
    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName as 
semblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMar 
k& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIn 
trospection, Boolean suppressSecurityChecks) 
    at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Ev 
idence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, 
Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackM 
ark) 
    at System.Reflection.Assembly.LoadFrom(String assemblyFile) 
    at LoadDllRemotely.Proxy.GetObject[T](String assemblyPath, String fullTypeNam 
e, String[] referencedAssembliesPath) in C:\Projects\LoadDllRemotely\LoadDllRemo 
tely\Program.cs:line 102 

Unhandled Exception: System.NullReferenceException: Object reference not set to 
an instance of an object. 
    at LoadDllRemotely.Program.LoadRemotely() in C:\Projects\LoadDllRemotely\Load 
DllRemotely\Program.cs:line 74 
    at LoadDllRemotely.Program.Main(String[] args) in C:\Projects\LoadDllRemotely 
\LoadDllRemotely\Program.cs:line 28 
Press any key to continue . . . 

這是我的實現,它的遠程加載DLL的新的AppDomain

using System; 
using System.IO; 
using System.Reflection; 
using ZupZip.Lib.Interfaces; 

namespace LoadDllRemotely 
{ 
    public class Program 
    { 
     private const string ASSEMBLY_PATH = 
         @"C:\Projects\LoadDllRemotely\ZupZip.Lib\bin\Debug\ZupZip.Lib.dll"; 
     private const string ASSEMBLY_URL = 
         @"http://codesanook.cloudapp.net/dll/ZupZip.Lib.dll"; 

     private const string REFERENCED_ASSEMBLY3_URL = 
       @"http://codesanook.cloudapp.net/dll/ZupZip.Lib.Interfaces.dll"; 

     private const string REFERENCED_ASSEMBLY2_URL = 
       @"http://codesanook.cloudapp.net/dll/system.core.dll"; 


     private const string REFERENCED_ASSEMBLY1_URL = 
       @"http://codesanook.cloudapp.net/dll/mscorlib.dll"; 

     public static void Main(string[] args) 
     { 
      //LoadLocally(); 
      LoadRemotely(); 
     } 

     public static void appDomain_DomainUnload(object sender, EventArgs e) 
     { 
      Console.WriteLine("domain loaded sender: {0}", 
       sender.GetType().FullName); 
     } 


     public static void LoadLocally() 
     { 
      var appDomain = AppDomain.CreateDomain("dynamicDll"); 
      appDomain.DomainUnload += new EventHandler(appDomain_DomainUnload); 
      var proxy = (Proxy)appDomain.CreateInstanceAndUnwrap(
       typeof(Proxy).Assembly.FullName, 
       typeof(Proxy).FullName); 

      var calculator = proxy.GetObject<IGradeCalculator>(
       ASSEMBLY_PATH, 
       "ZupZip.Lib.GradeCalculator"); 
      var score = 80; 
      Console.WriteLine("you got grad: {0} from score: {1}", calculator.GetGradeForScore(score), score); 

      AppDomain.Unload(appDomain); 

      File.Delete(ASSEMBLY_PATH);//you can delete referece dll after remove 
     } 


     public static void LoadRemotely() 
     { 
      //app domain setup 
      //load as byte array 
      var appDomain = AppDomain.CreateDomain("dynamicDll"); 
      appDomain.DomainUnload += new EventHandler(appDomain_DomainUnload); 

      var proxy = (Proxy)appDomain.CreateInstanceAndUnwrap(
       typeof(Proxy).Assembly.FullName, 
       typeof(Proxy).FullName); 

      var calculator = proxy.GetObject<IGradeCalculator>(
       ASSEMBLY_URL, 
       "ZupZip.Lib.GradeCalculator", 
       REFERENCED_ASSEMBLY3_URL); 
      var score = 80; 
      Console.WriteLine("you got grad: {0} from score: {1}", calculator.GetGradeForScore(score), score); 

      AppDomain.Unload(appDomain); 

     } 

    } 

    public class Proxy : MarshalByRefObject 
    { 
     public T GetObject<T>(
      string assemblyPath, 
      string fullTypeName, 
      params string[] referencedAssembliesPath) where T : class 
     { 
      try 
      { 
       //find reference 
       var assemblyToLoad = Assembly.ReflectionOnlyLoadFrom(assemblyPath); 
       AssemblyName[] referencedAssemblies = assemblyToLoad.GetReferencedAssemblies(); 
       //foreach (var referencedAssembly in referencedAssemblies) 
       //{ 
       // Console.WriteLine("referenceAssemblyName: {0}", referencedAssembly.Name); 
       // Assembly.Load(referencedAssembly.Name); 
       //} 
       //solve reference assembly 
       foreach (var referencedAssemblyPath in referencedAssembliesPath) 
       { 
        Assembly.LoadFrom(referencedAssemblyPath); 
       } 

       //this dll will load in new domain 
       var assembly = Assembly.LoadFrom(assemblyPath); 
       var type = assembly.GetType(fullTypeName); 
       var obj = (T)Activator.CreateInstance(type); 
       return obj; 
      } 
      catch (Exception ex) 
      { 
       // throw new InvalidOperationException(ex); 
       Console.WriteLine(ex.Message); 
       Console.WriteLine(ex.StackTrace); 
       return null; 
      } 
     } 

    } 

} 

我也是這個源代碼添加到我的開源上Bitbuckget

enter link description here

+1

這個代碼「我嘗試遠程加載時有問題解決DLL。」那個問題是?請包含您收到的任何錯誤消息的全文或解釋意外行爲。還有,你爲什麼遠程引用'system.core.dll'和'mscorlib.dll'? –

+0

@ScottChamberlain非常感謝您的評論我添加了更多您建議我的信息我希望如果您有時間,請下載我的項目並幫助我解決此問題。 再次感謝。 – theeranitp

+0

請注意,.NET Remoting在.NET Core中已停用(https://www.infoq.com/news/2016/02/Core-Discontinued)。 – orad

回答

相關問題