this._appDomain = AppDomain.CreateDomain(this._appname,AppDomain.CurrentDomain.EvIDence,appDomainSetup);this._startStopControllerToRun = (IStartStop)this._appDomain.CreateInstanceFromAndUnwrap(assemblyname,this._fullyQualifIEdClassname);this._startStopControllerToRun.Start();
这已经很长时间了.问题是在此应用程序域内启动的控制器调用框架日志记录类.记录器获取条目程序集名称,并将其记录为事件日志中的源.这是记录器获取源(调用者)名称的方式:
private static string GetSource(){ try { var assembly = Assembly.GetEntryAssembly(); // GetEntryAssembly() can return null when called in the context of a unit test project. // That can also happen when called from an app hosted in IIS,or even a windows service. if (assembly == null) { // From https://stackoverflow.com/a/14165787/279516: assembly = new StackTrace().GetFrames().Last().getmethod().Module.Assembly; } return assembly.Getname().name; } catch { return "UnkNown"; }}
在添加之前,如果检查,记录器将为源记录“未知”.经过一些研究,我在if块中添加了尝试.现在,记录器将“mscorlib”记录为源(条目程序集名称).
这是概述:
主持人 – >控制器(在app域内运行)
如何获取在域中运行的程序集(具有控制器)的名称?
注意:我也尝试了这个(下面),但是它给了我存在日志记录类的框架的名称(不是控制器在app域中运行的程序集的名称):
assembly = Assembly.GetExecutingAssembly();解决方法 这可能是你想做的事情的一种方式.我在这里演示的是通过SetData和GetData方法向创建的AppDomain传递和接收元数据,因此忽略了我创建实际远程类型的方式.
using System;using System.Reflection;namespace ConsoleApplication13{ class Program { static voID Main(string[] args) { AppDomain appDomain = AppDomain.CreateDomain("foo"); appDomain.SetData(FooUtility.sourceKey,FooUtility.sourceValue); IFoo foo = (IFoo)appDomain.CreateInstanceFromAndUnwrap(Assembly.GetEntryAssembly().Location,typeof(Foo).Fullname); foo.DoSomething(); } } public static class FooUtility { public const string SourceKey = "Source"; public const string SourceValue = "Foo Host"; } public interface IFoo { voID DoSomething(); } public class Foo : MarshalByRefObject,IFoo { public voID DoSomething() { string source = AppDomain.CurrentDomain.GetData(FooUtility.sourceKey) as string; if (String.IsNullOrWhiteSpace(source)) source = "some default"; Console.Writeline(source); } }}
哪个输出:
Foo Host
Press any key to continue …
因此,在您的情况下,您可以将任何源元数据传递给AppDomain:
this._appDomain = AppDomain.CreateDomain(this._appname,appDomainSetup);this._appDomain.SetData("Source","MyController");this._startStopControllerToRun = (IStartStop)this._appDomain.CreateInstanceFromAndUnwrap(assemblyname,this._fullyQualifIEdClassname);this._startStopControllerToRun.Start();
并在您的GetSource方法中检查它的存在.
private static string GetSource(){ try { string source = AppDomain.CurrentDomain.GetData("Source") as string; if (!String.IsNullOrWhiteSpace(source)) return source; var assembly = Assembly.GetEntryAssembly(); // GetEntryAssembly() can return null when called in the context of a unit test project. // That can also happen when called from an app hosted in IIS,or even a windows service. if (assembly == null) { // From https://stackoverflow.com/a/14165787/279516: assembly = new StackTrace().GetFrames().Last().getmethod().Module.Assembly; } return assembly.Getname().name; } catch { return "UnkNown"; }}
更新替代方案
您还可以声明一个公共接口方法,用于在目标域中的静态位置设置源.
using System;using System.Reflection;namespace ConsoleApplication13{ class Program { static voID Main(string[] args) { AppDomain appDomain = AppDomain.CreateDomain("foo"); IFoo foo = (IFoo)appDomain.CreateInstanceFromAndUnwrap(Assembly.GetEntryAssembly().Location,typeof(Foo).Fullname); foo.SetSource("Foo Host"); foo.DoSomething(); } } public interface IFoo { voID DoSomething(); voID SetSource(string source); } public class Foo : MarshalByRefObject,IFoo { public voID DoSomething() { string source = Foo.source; if (String.IsNullOrWhiteSpace(source)) source = "some default"; Console.Writeline(source); } public static string Source{get; private set;} public voID SetSource(string source) { Foo.source = source; } }}总结
以上是内存溢出为你收集整理的c# – 获取在应用程序域内启动的进程的程序集名称全部内容,希望文章能够帮你解决c# – 获取在应用程序域内启动的进程的程序集名称所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)