我正在开发一个Azure辅助角色,需要根据从WCF服务中提取的工作定期执行操作。由于Azure部署的性质,决定在Azure中实现插件框架,以便快速更新我们的产品,同时通过消除对完全天蓝色部署的需求来最小化停机时间,以及支持各种程序集的多个版本。 / p>
从云存储下载插件并将其加载到包含加载程序集所需的所有相关信息的类中。 (参见下面的PluginAssemblyInfo)
在其他开发工作中,我开发了以下的ReflectionHelper类。这个类在其他应用程序中工作,我已经验证它在控制台应用程序中工作。
但是,当在Azure模拟器中运行时,我收到以下错误:(我在该行上添加了注释,引发了异常。)
Type is not resolved for member 'MyCompanyName.ReflectionHelper,MyCompanyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
ReflectionHelper类作为源文件添加到调用它的同一程序集中。 (出于匿名原因,我确实编辑了程序集/名称空间的名称)
鉴于知道这个课程在其他情况下有效,我唯一的猜测是它可能是一个信任问题,但我没有找到与此相关的很多信息。任何帮助将不胜感激。
这是ReflectionHelper类的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Serialization;
using System.Web;
namespace MyCompanyName
{
[Serializable]
public class ReflectionHelper
{
public AppDomain appDomain { get; set; }
public byte[] PluginBytes { get; set; }
public string PluginClassName { get; set; }
public string PluginAssemblyName { get; set; }
public Dictionary<string, byte[]> AssemblyArchive { get; set; }
public object GetInstance(AppDomain NewDomain, byte[] PluginBytes, string PluginClassName, string PluginAssemblyName, Dictionary<string, byte[]> AssemblyArchive)
{
try
{
this.appDomain = NewDomain;
this.PluginBytes = PluginBytes;
this.PluginClassName = PluginClassName;
this.AssemblyArchive = AssemblyArchive;
this.PluginAssemblyName = PluginAssemblyName;
//this is the line that throws the serializationexception
appDomain.AssemblyResolve += new ResolveEventHandler((sender, args) =>
{
AssemblyName x = new AssemblyName(args.Name);
string Name = x.Name;
if (System.IO.Path.GetExtension(x.Name) != ".dll")
Name += ".dll";
Assembly Ret = appDomain.Load(this.AssemblyArchive[Name]);
return Ret;
});
appDomain.DoCallBack(LoaderCallBack);
return appDomain.GetData("Plugin");
}
catch (Exception ex)
{
throw ex;
}
}
public void LoaderCallBack()
{
ObjectHandle PluginObject = appDomain.CreateInstance(string.Format(this.PluginAssemblyName), PluginClassName);
appDomain.SetData("Plugin", PluginObject.Unwrap());
}
}
}
这是调用它的代码片段
AppDomain currentDomain = AppDomain.CurrentDomain;
AppDomainSetup domainSetup = new AppDomainSetup() { };
AppDomain BrokerJobDomain = AppDomain.CreateDomain("WorkerPluginDomain" + Guid.NewGuid().ToString());//, null, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase });
ReflectionHelper helper = new ReflectionHelper();
object Instance = helper.GetInstance(BrokerJobDomain, pluginAssembly.PluginBytes, pluginAssembly.ClassName, pluginAssembly.AssemblyName, pluginAssembly.AssemblyArchive);
IWorkPlugin executor = (IWorkPlugin)Instance;
这是从云存储下载插件时填充的程序集信息类
public class PluginAssemblyInfo
{
public string ClassName { get; set; }
public byte[] PluginBytes { get; set; }
public Dictionary<string, byte[]> AssemblyArchive { get; set; }
public string AssemblyName { get; set; }
public DateTime LoadDate { get; set; }
}
答案 0 :(得分:4)
该问题似乎与未在子域上设置的RelativeSearchPath有关。确保子域具有相同的设置凭据似乎解决了这个问题。
AppDomain currentDomain = AppDomain.CurrentDomain;
AppDomain BrokerJobDomain = AppDomain.CreateDomain("WorkerPluginDomain" + Guid.NewGuid().ToString(), null, AppDomain.CurrentDomain.SetupInformation );