none
winforms useLegacyV2RuntimeActivationPolicy issues RRS feed

  • Question

  • This is a visual studio 2010 .net 4.0 winforms project with a setup project.

    I went off of this tutorial http://techbrij.com/145/install-sql-server-database-with-visual-studio-setup  on how to run sql during a setup project. I changed the sql dlls to reference 10.0 instead of 9.0.

    I read post after post on how to fix this issue.
    “{System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.”

    I have tried multiple combinations in my project’s app.config but I still get the error.

    I decided to make sure that it was reading the correct app.config file.  When I checked the configuration manager it was showing that it was reading from the C:\WINDOWS\system32\MsiExec.exe.config???

    How do I get my setup project to reference the app.config from my real project? The setup project already has the primary output set in the custom actions section.

    What I tried already:

    <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    
    <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0"/>
      <requiredRuntime version="v4.0.20506"/>
    </startup>
    
     <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v2.0.50727"/>
      <supportedRuntime version="v4.0"/>
      <requiredRuntime version="v4.0.20506"/>
     </startup>
    
     <runtime>
      <loadFromRemoteSources enabled="true" />
      <generatePublisherEvidence enabled="false" />
     </runtime>
    
    Thursday, March 17, 2011 4:35 PM

Answers

  • After needing to create a custom bootstrapper for Microsoft SQL Server System CLR Types, Microsoft SQL Server 2008 Management Objects, and Microsoft SQL Server 2008 Native Client it was getting out of hand.

    Now I needed .net framework 2.0 sp2 as a prerequisite as well!

    Instead of using Microsoft.SqlServer.Smo.dll, Microsoft.SqlServer.Management.Sdk.Sfc.dll, and Microsoft.SqlServer.ConnectionInfo.dll I took another approach. No need for startup or runtime tags in my app.config.


    A better solution. Code was a proof of concept so clean it up how you like.
    Generate a sql script from SSMS named it sql.txt and make it an Embedded Resource in project.

    Create a console app and drop code in. No need for extra references or additions to app.config.


    namespace ConsoleApplication1
    {
      using System;
      using System.Data;
      using System.Data.SqlClient;
      using System.IO;
      using System.Reflection;
      using System.Text.RegularExpressions;
    
      class Program
      {
        static void Main(string[] args)
        {
          string machineName = Environment.MachineName;
          string instance = "SQLEXPRESS";
          string dataSource = string.Concat(machineName, @"\", instance);
    
          using (SqlConnection connection = new SqlConnection(string.Concat("Data Source=", dataSource, ";Initial Catalog=master;Integrated Security=True")))
          {
            connection.Open();
    
            string strScript = GetSql("sql.txt");
            ExecuteSql(connection, strScript);
          }
        }
    
        private static string GetSql(string Name)
        {
          try
          {
            // Gets the current assembly.
            Assembly Asm = Assembly.GetExecutingAssembly();
    
            // Resources are named using a fully qualified name.
            Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);
    
            // Reads the contents of the embedded file.
            StreamReader reader = new StreamReader(strm);
    
            return reader.ReadToEnd();
          }
          catch (Exception ex)
          {
            throw ex;
          }
        }
    
        /// <remarks>
        /// Uncomment transaction lines if want to do transactions.
        /// Transactions do not work with CREATE TABLE or CREATE DATABASE.
        /// </remarks>
        public static void ExecuteSql(SqlConnection connection, string sql)
        {
          Regex regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
          string[] lines = regex.Split(sql);
    
          //SqlTransaction transaction = connection.BeginTransaction();
          using (SqlCommand cmd = connection.CreateCommand())
          {
            cmd.Connection = connection;
            //cmd.Transaction = transaction;
    
            foreach (string line in lines)
            {
              if (line.Length > 0)
              {
                cmd.CommandText = line;
                cmd.CommandType = CommandType.Text;
    
                try
                {
                  cmd.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                  //transaction.Rollback();
                  throw;
                }
              }
            }
          }
    
          //transaction.Commit();
        }
      }
    }
    
    
    • Marked as answer by Kevin O. _ Friday, March 18, 2011 12:15 AM
    Friday, March 18, 2011 12:15 AM

All replies

  • After needing to create a custom bootstrapper for Microsoft SQL Server System CLR Types, Microsoft SQL Server 2008 Management Objects, and Microsoft SQL Server 2008 Native Client it was getting out of hand.

    Now I needed .net framework 2.0 sp2 as a prerequisite as well!

    Instead of using Microsoft.SqlServer.Smo.dll, Microsoft.SqlServer.Management.Sdk.Sfc.dll, and Microsoft.SqlServer.ConnectionInfo.dll I took another approach. No need for startup or runtime tags in my app.config.


    A better solution. Code was a proof of concept so clean it up how you like.
    Generate a sql script from SSMS named it sql.txt and make it an Embedded Resource in project.

    Create a console app and drop code in. No need for extra references or additions to app.config.


    namespace ConsoleApplication1
    {
      using System;
      using System.Data;
      using System.Data.SqlClient;
      using System.IO;
      using System.Reflection;
      using System.Text.RegularExpressions;
    
      class Program
      {
        static void Main(string[] args)
        {
          string machineName = Environment.MachineName;
          string instance = "SQLEXPRESS";
          string dataSource = string.Concat(machineName, @"\", instance);
    
          using (SqlConnection connection = new SqlConnection(string.Concat("Data Source=", dataSource, ";Initial Catalog=master;Integrated Security=True")))
          {
            connection.Open();
    
            string strScript = GetSql("sql.txt");
            ExecuteSql(connection, strScript);
          }
        }
    
        private static string GetSql(string Name)
        {
          try
          {
            // Gets the current assembly.
            Assembly Asm = Assembly.GetExecutingAssembly();
    
            // Resources are named using a fully qualified name.
            Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);
    
            // Reads the contents of the embedded file.
            StreamReader reader = new StreamReader(strm);
    
            return reader.ReadToEnd();
          }
          catch (Exception ex)
          {
            throw ex;
          }
        }
    
        /// <remarks>
        /// Uncomment transaction lines if want to do transactions.
        /// Transactions do not work with CREATE TABLE or CREATE DATABASE.
        /// </remarks>
        public static void ExecuteSql(SqlConnection connection, string sql)
        {
          Regex regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
          string[] lines = regex.Split(sql);
    
          //SqlTransaction transaction = connection.BeginTransaction();
          using (SqlCommand cmd = connection.CreateCommand())
          {
            cmd.Connection = connection;
            //cmd.Transaction = transaction;
    
            foreach (string line in lines)
            {
              if (line.Length > 0)
              {
                cmd.CommandText = line;
                cmd.CommandType = CommandType.Text;
    
                try
                {
                  cmd.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                  //transaction.Rollback();
                  throw;
                }
              }
            }
          }
    
          //transaction.Commit();
        }
      }
    }
    
    
    • Marked as answer by Kevin O. _ Friday, March 18, 2011 12:15 AM
    Friday, March 18, 2011 12:15 AM
  • Thank you for your sharing.
    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 18, 2011 1:57 AM