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.

Niklaus Wirth
  • 820
  • 1
  • 7
  • 21
  • 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? – Jamie Clayton Jan 04 '14 at 16:55

2 Answers2

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

Abel
  • 56,041
  • 24
  • 146
  • 247
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

  • How can I call these functions, and Is there any other simpler approach ? – Niklaus Wirth Jun 12 '13 at 12:45
  • 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 itself – Ahmad Sabbagh Jun 13 '13 at 06:57
  • Please refer to http://msdn.microsoft.com/en-us/library/a51xd4ze.aspx to double check – Ahmad Sabbagh Jun 13 '13 at 06:57
  • 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! – Ahmad Sabbagh Jun 13 '13 at 07:04
  • 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? – Niklaus Wirth Jun 17 '13 at 09:34
  • 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! – Ahmad Sabbagh Jun 22 '13 at 09:24
  • I need to modify the configuration file of the application and update a specific key/value of it depending on special conditions – Niklaus Wirth Jul 02 '13 at 15:51