/// <summary> /// 开始处理 /// </summary> /// <param name="remoteTask"></param> public static void Process(IRemoteTaskRequest remoteTask) { //将请求加入队列 remoteTaskRequests.Enqueue(remoteTask); if (!remoteTaskRequests.IsEmpty) { //从RemoteTaskPool中取得对象 var remoteTaskApplication = remoteApplicationPool.OutPool(); //从请求队列中将请出列,交由remoteTaskAppliction处理 IRemoteTaskRequest remoteTaskRequest; remoteTaskRequests.TryDequeue(out remoteTaskRequest); //将请求交由RemoteTaskApplication处理 remoteTaskApplication.Process(remoteTaskRequest); //请求处理完之后,我们要将RemoteTaskApplication对象释放到对象池中 remoteApplicationPool.InPool(remoteTaskApplication); } }
public string CloneFilesAndAssembles(IRemoteTaskRequest remoteTask) { var remoteTaskDirectoryPath = Path.Combine(this.REMOTETASKSBASEPATH, remoteTask.RemoteTaskIdentity.ToString()); if (!Directory.Exists(remoteTaskDirectoryPath)) { Directory.CreateDirectory(remoteTaskDirectoryPath); } this.SaveFile(remoteTask.CodeFile.CodeFileBytes, remoteTaskDirectoryPath, remoteTask.CodeFile.CodeFileName); if (remoteTask.ReferencedAssembles == null) { return(remoteTaskDirectoryPath); } foreach (var referencedAssemble in remoteTask.ReferencedAssembles) { this.SaveFile(referencedAssemble.ReferencedAssembleBytes, remoteTaskDirectoryPath, referencedAssemble.ReferencedAssembleName); } return(remoteTaskDirectoryPath); }
public void BuildDynamicAssemble(IRemoteTaskRequest remoteTask) { var filesAndAssemblesDirectoryPath = this.CloneFilesAndAssembles.CloneFilesAndAssembles(remoteTask); var code = string.Empty; //读取代码文件中的源代码,并动态构造一个程序集 using (var fileStream = new FileStream(Path.Combine(filesAndAssemblesDirectoryPath, remoteTask.CodeFile.CodeFileName), FileMode.Open, FileAccess.Read)) { using (var streamReader = new StreamReader(fileStream)) { code = streamReader.ReadToEnd(); } } //var engine = new ScriptEngine(); //var session = engine.CreateSession(); //session.AddReference(typeof(IRemoteTaskClass).Assembly.Location); //session.ImportNamespace("System.Linq"); var syntaxTree = SyntaxFactory.ParseSyntaxTree(code); var compilation = CSharpCompilation.Create("RemoteTaskAutoBuildDll.dll", new[] { syntaxTree }, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), references: new[] { new MetadataFileReference(typeof(object).Assembly.Location), new MetadataFileReference(typeof(IRemoteTaskClass).Assembly.Location) }); var model = compilation.GetSemanticModel(syntaxTree); var root = (CompilationUnitSyntax)syntaxTree.GetRoot(); var nameInfo = model.GetSymbolInfo(root.Usings[0].Name); var systemSymbol = (INamespaceSymbol)nameInfo.Symbol; foreach (var ns in systemSymbol.GetNamespaceMembers()) { Trace.WriteLine(ns.Name); } using (var fileStream = new FileStream(Path.Combine(filesAndAssemblesDirectoryPath, "RemoteTaskAutoBuildDll.dll"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) { compilation.Emit(fileStream); } }
/// <summary> /// 开始处理请求 /// </summary> /// <param name="remoteTaskRequest">请求信息</param> public void Process(IRemoteTaskRequest remoteTaskRequest) { //加载所有模块和模块中绑定的事件处理程序 this.InitModules(this); //创建一个事件参数 var retmoeTaskEventArgs = new RemoteTaskEventArgs() { RemoteTaskRequest = remoteTaskRequest }; //调用开始请求事件 if (this.OnRequest != default(EventHandler <RemoteTaskEventArgs>)) { this.OnRequest(this, retmoeTaskEventArgs); } //TODO:实际处理逻辑,这里将要调用封住的Roslyn酷进行动态编译 //调用结束请求事件 if (this.EndRequest != default(EventHandler <RemoteTaskEventArgs>)) { this.EndRequest(this, retmoeTaskEventArgs); } }