加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 获取在应用程序域内启动的进程的程序集名称

发布时间:2020-12-15 05:38:43 所属栏目:百科 来源:网络整理
导读:我有一个创建应用程序域并启动它的服务: this._appDomain = AppDomain.CreateDomain(this._appName,AppDomain.CurrentDomain.Evidence,appDomainSetup);this._startStopControllerToRun = (IStartStop)this._appDomain.CreateInstanceFromAndUnwrap(assembl
我有一个创建应用程序域并启动它的服务:
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;
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读