wixcustom-actiondtf

WIX CustomAction loading CLR 4.0 instead of 2.0


I'm trying to write a c# custom action and using .Net 2, however I can't seem to get things to work. I believe the MSI is using the wrong CLR version. I see this in the log file a few lines above where the MSI is calling my CA (line line does not appear if I comment out my CA):

SFXCA: Binding to CLR version v4.0.30319. 

My custom action does not appears to load (assuming since I do not see any of the logging messages). I am using Wiz 3.6.3014.0. Is this supported or does the Windows Installer just use the latest runtime available on the machine?

Thanks


Solution

  • When you use WiX to create a C# custom action project, it by default creates an XML file called CustomAction.config. The purpose of this file is to instruct sfxca.dll what version of the CLR to use when running your code. The default implementation of this file looks like:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
    
            <!--
              Use supportedRuntime tags to explicitly specify the version(s) of the .NET Framework runtime that
              the custom action should run on. If no versions are specified, the chosen version of the runtime
              will be the "best" match to what Microsoft.Deployment.WindowsInstaller.dll was built against.
    
              WARNING: leaving the version unspecified is dangerous as it introduces a risk of compatibility
              problems with future versions of the .NET Framework runtime. It is highly recommended that you specify
              only the version(s) of the .NET Framework runtime that you have tested against.
    
              Note for .NET Framework v3.0 and v3.5, the runtime version is still v2.0.
    
              In order to enable .NET Framework version 2.0 runtime activation policy, which is to load all assemblies 
              by using the latest supported runtime, @useLegacyV2RuntimeActivationPolicy="true".
    
              For more information, see http://msdn.microsoft.com/en-us/library/bbx34a2h.aspx
            -->
    
            <supportedRuntime version="v4.0" />
            <supportedRuntime version="v2.0.50727"/>
    
        </startup>
    
        <!--
          Add additional configuration settings here. For more information on application config files,
          see http://msdn.microsoft.com/en-us/library/kza1yk3a.aspx
        -->
    
    </configuration>
    

    Basically it's a good practice for CA's written in .NET 2.0/3.0/3.5 ( all CLR 2.0 ) to be allowed to run against 4.0 because you could come across a machine that has only 4.0 installed. Generally your code should work against 4.0 and if it doesn't, I'd fix that.

    Alternatively you could update the config file to only allow running on 2.0 and then put the needed checks into the installer to make sure that this version of the CLR is infact installed.