fir3pho3nixx
fir3pho3nixx

Reputation: 174

netstandard 2.0 does not have AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

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?

Upvotes: 12

Views: 4494

Answers (2)

Michael Blake
Michael Blake

Reputation: 2168

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);

Upvotes: 6

quetzalcoatl
quetzalcoatl

Reputation: 33556

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.

Upvotes: 7

Related Questions