Home > Software engineering >  is there a way to tell at run time which type of app c# code is running in?
is there a way to tell at run time which type of app c# code is running in?

Time:05-16

In a Visual Studio 2022 .sln there are 2 projects. One is Windows Service, another one is ASP.NET 6.0. Both use a shared code to retrieve xml data such as SQL server instance name, database name, etc. from app.config file.

For logging I use log4net package which is implemented somewhat differently in .NET Framework 4.6.1 which is my Windows.Service project targets and .NET Core which my ASP.NET targets.

To save the effort I'd like to write a common log method and differentiate platform inside.

So, is there a way to get the type of app the code is running?

CodePudding user response:

You cannot get the Application Type, but you can get the targeted framework, which I believe would solve your problem.

        Assembly currentAssembly = Assembly.GetExecutingAssembly();

        var attribute = currentAssembly.GetCustomAttribute<System.Runtime.Versioning.TargetFrameworkAttribute>();

        if (attribute.FrameworkName == ".NETCoreApp,Version=v3.1")
        {
            //Do Stuff
        }

Let me know if I need to clarify anything.

Happy coding :)

  • Related