How can I know which Runtime Host is currently run my code?
Asked Answered
S

2

7

As Microsoft Documentation declare Runtime Hosts that .NET have more than one Runtime Hosts to support and execute the code of our application, my question is How Can I know which Runtime Hosts of the Microsoft Runtime hosts, is hosting my code.

I am using C# language to develop dll class library which may be used and/or hosted by various Runtime hosts, so I need to know which Runtime host is now hosting my code to satisfy specify conditions.

Sharonsharona answered 11/6, 2013 at 9:1 Comment(1)
Are you implying that the DLL Runtime "shared" code might be using a newer version of .net Runtime than the code that's calling it? Is reading/writing to the *.config file your only concern?Morehouse
B
1

There's actually quite an easy way to determine the current runtime version of the CLR. As it happens, Environment.Version will return a different version if your code is currently run in a different CLR due to SxS (Side-by-Side) execution.

To see how that works in practice in an application that can have two runtimes at the same time, check out this article on Demonstrating Side by Side execution.

if(Environment.Version.StartsWith("2.0"))
    System.Console.WriteLine("Inside .NET CLR 2.0");
else if(Environment.Version.StartsWith("4.0"))
    System.Console.WriteLine("Inside .NET CLR 4.0");
else
    System.Console.WriteLine("Unknown .NET version");

Note that the .NET 2.0 loader will load the most recent CLR of .NET 2.0 available, which will be .NET 3.5 in most cases. It is not possible to run different versions of .NET 2.0 side by side in one process. Neither is it possible to run .NET 1.0 or .NET 1.1 side by side with .NET 2.0 in .NET 4.0 (it is possible though to run just 1.0 or 1.1 side by side in .NET 4.0).

Byers answered 11/12, 2013 at 13:54 Comment(0)
T
0

The .NET Framework 4 hosting API provides the CLRCreateInstance function, which can return the ICLRMetaHost interface. You can then call the GetRuntime method on this interface to get a specific ICLRRuntimeInfo interface, given a particular CLR version. This procedure supersedes the CorBindToRuntimeEx method that is used by the .NET Framework 2.0 hosting API.

The .NET Framework version 2.0 hosting API provides the CorBindToRuntimeEx function to initialize the runtime. You can choose which version of the runtime to load, but a process can host only one version. If version 2.0, 3.0, or 3.5 is loaded, the function returns the ICLRRuntimeHost interface, which is used to start the runtime and execute managed code.

Source: http://msdn.microsoft.com/en-us/library/dd380850.aspx

Tamica answered 12/6, 2013 at 11:28 Comment(7)
How can I call these functions, and Is there any other simpler approach ?Sharonsharona
After digging deep in MSDN and google i came to the fact that the interfaces provided by MSCorEE.dll do not include a method to define host name or version to CLR, the interfaces provide hosting and management functionality of the CLR only, One peace of information you can know by using these interfaces is the version of the CLR running your code NOT the host running CLR itselfTamica
Please refer to msdn.microsoft.com/en-us/library/a51xd4ze.aspx to double checkTamica
BTW why you think the CLR would benefit of knowing a name or a version of its host?,Also the main idea of writing a different CLR hosts is controlling the CLR behavior and i think the CLR should be host unaware anyway!Tamica
You right, but consider yourself developing DLL Library which need to deal with Configuration file for example, now If your dll will be hosted in ASP.NET then you need to deal with the Configuration file through WebConfigurationManager class and if your dll will be hosted in Shell Command Prompt then you need to deal with the Configuration file through ConfigurationManager, So my question how can I determine when I have to use either one of those classes?Sharonsharona
Can you explain what exactly you want to read from the configuration file please?, if you are reading connection string for example your library will take the connection string configuration from the host automatically, In fact I'm now developing such a functionality :), It would be helpful to both of us!Tamica
I need to modify the configuration file of the application and update a specific key/value of it depending on special conditionsSharonsharona

© 2022 - 2024 — McMap. All rights reserved.