Click here to Skip to main content
15,606,012 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends

I could discover that we can redirect to a specific version of a .net assembly by using configuration file as below:

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="myAssembly"
          culture="en-us" />
        <!-- Assembly versions can be redirected in application, 
          publisher policy, or machine configuration files. -->
        <bindingRedirect oldVersion="" newVersion="" />

But I do not want to use that config file. In fact I can not do that. I am working on a C++ application which also have some .net modules. We explicitly load the CLR by using stuff as shown below:
ICLRMetaHost *m_pMetaHost = NULL;
	HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&m_pMetaHost);
// Get the runtime host 
	hr = m_pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID *)&m_pRuntimeInfo);
	if (!m_pRuntimeInfo) {
		*pHR = hr;
		return false;	
// Register our this object as host control
	hr = m_pClrHost->SetHostControl(static_cast<IHostControl *>(this));
	if (FAILED(hr)) {
		*pHR = hr;
		return false;

	// Get pointer to ICLRControl interface
	ICLRControl *pCLRControl = NULL;
	hr = m_pClrHost->GetCLRControl(&pCLRControl);

	if (FAILED(hr) || pCLRControl == NULL) {
		*pHR = hr;
		return false;
// String defines used to construct args to SetAppDomainType.  Note these are wide strings.
	#define ADM_ASSEMBLY_REF L"MyAssembly, Version=%s, Culture=neutral, PublicKeyToken=1c8d91d2de78b25e"
	#define ADM_CLASS_REF    L"My.Namespace.MyAppDomainManager"

	// Convert version string to wide char and into assembly ref string
	wchar_t versionInfo[256];
	MultiByteToWideChar(CP_ACP, 0, MI_VERSION_STRING, -1, versionInfo, sizeof(versionInfo) / sizeof(wchar_t));
	wchar_t assemblyRef[512];
	swprintf(assemblyRef, sizeof(assemblyRef), ADM_ASSEMBLY_REF, versionInfo);

	// Set the app domain manager type
	hr = pCLRControl->SetAppDomainManagerType(assemblyRef, ADM_CLASS_REF);

Can someone please tell how can I specify which version of a specific assembly is to be redirected to? Please note that I want to do this with respect to default application only. I am not creating any new application domain. I think while initializing/loading the CLR, we might have some way to specify the configuration information via code instead of using any physical configuration file.

Any help/direction is heartily appreciated.

Thanks in advance
Aseem Sharma
Sergey Alexandrovich Kryukov 30-Jan-13 12:44pm    
Redirect? How can it be redirect? Are you familiar with side-by-side execution?
Aseem Sharma 30-Jan-13 20:03pm    
Thanks Sergey for your reply,

I am not much aware of Side by Side execution. But I could resolve my issue. We just need to override below function in our class MyAppDomainManager (derived from AppDomainManager)

public override void InitializeNewDomain(AppDomainSetup appDomainInfo)

Since here we have access to AppDomainSetup , we can do the redirection stuff here only.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900