Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 237
fir3pho3nixx Avatar asked Sep 14 '17 18:09

fir3pho3nixx


2 Answers

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);
like image 128
Michael Blake Avatar answered Nov 09 '22 18:11

Michael Blake


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.

like image 35
quetzalcoatl Avatar answered Nov 09 '22 17:11

quetzalcoatl