netstandard 2.0 does not have AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
Asked Answered
T

2

11

I have just upgraded a net451 classic dotnet project to multi-target project using net461/netstandard2.0 with reasonable success.

Did however come across this compiler error for net461 when it comes to gaining access legacy configuration files via:

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

This appears to not be a thing in netstandard2.0 :) Anyone know of any work arounds or good solutions?

Toting answered 14/9, 2017 at 18:46 Comment(2)
Aaargh! Title is meant to be: netstandard 2.0 does NOT have AppDomain.CurrentDomain.SetupInformation.ConfigurationFileToting
Remember you can edit the question yourself to fix any mistakes or add additional information.Piccaninny
G
7

According to this article, which I found when looking for replacement for PlatformServices.Default.Application.ApplicationBasePath, it should be either of:

  • AppDomain.CurrentDomain.SetupInformation.ApplicationName
  • Assembly.GetEntryAssembly().GetName().Name

Just like you, I've just found out that the first one does not exist in recent 2.0 so I'll be using the GetEntryAssembly name..

..however keep in mind that GetEntryAssembly may be null when used i.e. from within a test runner like xUnit, or probably will be wrong if your appcode is ran from another not-yours hosting/startup module, etc - so that is not a perfect replacement.

Gleam answered 2/1, 2018 at 16:55 Comment(0)
V
5

Add the ConfigurationManager as a NuGet

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
  <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
</ItemGroup>

Use ConfigurationManager in C#

var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Log4NetLogger.Configure(configFile.FilePath);
Valedictorian answered 11/6, 2019 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.