我可以以编程方式控制 .NET Core 程序集重定向吗? [英] Can I control .NET Core assembly redirects programmatically?

查看:22
本文介绍了我可以以编程方式控制 .NET Core 程序集重定向吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:在 .NET Core 2.0 (netstandard 2.0) 中有什么方法可以在运行时应用给定 application.exe.config 文件中未定义的程序集重定向可执行?也许一些程序化的方式?或者甚至通过运行具有一些特殊设置/标志的可执行文件?

Question: Is there any way in .NET Core 2.0 (netstandard 2.0) how I could apply assembly redirections NOT defined in given application.exe.config file when running executable? Maybe some programmatic way? Or even by running executable with some special settings/flags?

场景:这是我试图在 .NET Core 2.0 中解决的简化场景.

Scenario: Here is simplified scenario that I'm trying to solve in .NET Core 2.0.

  1. 有一个服务托管"可执行文件,我们称之为ServiceHost.exe
  2. 有一个服务.dll,我们称之为Service.dll,它实现了一些入口接口IService,方法Start().
  3. 当我运行 ServiceHost.exe --service-assembly Service.dll 时,它会加载服务并调用它的 IService.Start() 实现.
  4. 服务的作者可以是任何人,它独立于服务宿主.
  1. There is a "service hosting" executable, let's call it ServiceHost.exe
  2. There is a service .dll, let's call it Service.dll, which implements some entry interface IService with method Start().
  3. When I run ServiceHost.exe --service-assembly Service.dll it loads service and calls its IService.Start() implementation.
  4. Author of the service can be anyone, it's independent on service host.

假设服务需要一些程序集重定向.在 .NET Core 世界中,当您引用的包比旧的 .NET Framework 多得多,并且并非所有包都具有最新 .NET Core 的确切版本时,这很常见.使用程序集重定向效果很好,并且允许您使用针对旧版本的库就好了.

Let's say that the service needs some assembly redirects. It's quite common in .NET Core world when you reference much more packages than in old .NET Framework and not all of them have exact versions for latest .NET Core. Using assembly redirects works quite well and allows you to use library targeting older version just fine.

我的设置出现了问题.由于 Service.dll 是一个由独立可执行文件加载的库,因此不使用带有程序集重定向的 app.config 文件.运行时使用 ServiceHost.exe.config 而不是 Service.dll.config,这是预期的.

And here comes the problem with my setup. Since Service.dll is a library which is loaded by independent executable its app.config file with assembly redirects is not used. Runtime uses ServiceHost.exe.config and not Service.dll.config, which is expected.

提醒:我正在使用 .NET Core,因此创建新的应用程序域并将其设置为加载不同的配置文件的解决方案不是一个选项.

Reminder: I'm using .NET Core so solution with creating new Application Domain and setting it to load different config file is not an option.

推荐答案

我们使用的是 NetCore 针对 NetFramework 的 servicehost,您可以使用自动绑定重定向:

We use a servicehost that is NetCore targeting NetFramework, you can use Automatic binding redirects:

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>

这会在 Servicehost.exe.config 中自动生成绑定重定向

This then generates the binding redirects automatically in the Servicehost.exe.config

这篇关于我可以以编程方式控制 .NET Core 程序集重定向吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆