1

My code is in a process that might be a Console application, a desktop application, or an ASP.NET application. I'm trying to find out which kind of application I'm running in.

For .NET framework, I can use HostingEnvironment.IsHosted to see if I'm running in ASP.NET. But how do I determine this for a .NET Core application? Note that it can be both in IIS and self hosted.

Many Thanks.

Michael_S_
  • 488
  • 4
  • 11
  • 26
  • 4
    Why would you want to know that? How would that change the app's behavior? After all, all types of apps use the same Generic host – Panagiotis Kanavos Oct 22 '20 at 12:38
  • @PanagiotisKanavos I'm developing a profiler and I need to extract metadata about the process I'm profiling. – Michael_S_ Oct 22 '20 at 12:39
  • 4
    Which is something done from the *outside* of a process, using .NET Core's built-in APIs like EventCounters and EventPipe. Otherwise the profiler will end up profiling itself – Panagiotis Kanavos Oct 22 '20 at 12:41
  • 1
    You can chek the source of tools like [dotnet-counters](https://github.com/dotnet/diagnostics/tree/master/src/Tools/dotnet-counters) and [dotnet-trace](https://github.com/dotnet/diagnostics/tree/master/src/Tools/dotnet-trace) to see how they work. The [Diagnostics and Instrumentation](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/) section in the docs explains the APIs, tools, and available technologies – Panagiotis Kanavos Oct 22 '20 at 12:44
  • BTW I don't think having a built-in profiler in the application is bad. That's something Windows in general and .NET specifically always paid attention to. All versions had profiling hooks, performance counters and APIs that allowed diagnostics collection with minimal overhead. Developers were simply too lazy to use them - reporting a metric takes more work than just dumping a string to a file while using 100x-1000x more CPU cycles – Panagiotis Kanavos Oct 22 '20 at 12:48
  • So having your own tool *alongside* the app makes sense. There's even a [discussion on how to collect diagnostics from a side-car container](https://github.com/dotnet/diagnostics/issues/810) - not whether it's possible but documenting the process – Panagiotis Kanavos Oct 22 '20 at 12:50
  • @PanagiotisKanavos I guess I can use the profiling API (don't know) but that seems like an overkill. Since I run my own code in the same process, I was hoping for an in-process solution. – Michael_S_ Oct 22 '20 at 12:55
  • Overkill is what you're trying to do - and overhead. In fact, what are you trying to do? Why not use the built-in tools? How do you expect to profile the application if you don't use the built-in mechanisms? Remember, the application *already* publishes metrics about CPU, RAM, garbage collections etc. – Panagiotis Kanavos Oct 22 '20 at 12:56
  • @PanagiotisKanavos The biggest thing the profiling API allows me to do is to instrument the code and inject my own code. – Michael_S_ Oct 22 '20 at 12:58
  • When I want to know about the things that a website uses, I try this website: https://builtwith.com/ – Jamshaid K. Oct 22 '20 at 13:51

1 Answers1

0

Here is how I am doing it. Might not work in everyones use case but this works for me.

        var webType = Type.GetType("Microsoft.AspNetCore.Mvc.ControllerBase, Microsoft.AspNetCore.Mvc.Core");
        RunningAsWebApp = webType != null && Assembly.GetEntryAssembly()?.ExportedTypes.FirstOrDefault(x => webType.IsAssignableFrom(x)) != null;
Craig
  • 344
  • 4
  • 9