Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I control .NET Core assembly redirects programmatically?

Tags:

c#

.net

.net-core

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?

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

  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.

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.

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.

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.

like image 744
Katulus Avatar asked Sep 04 '17 15:09

Katulus


1 Answers

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>

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

like image 61
Rebecca Avatar answered Nov 09 '22 02:11

Rebecca