Changing app.configuration file during installation process with visual studio installer fails: Could not load file or assembly 'EntityFramework'

馋奶兔 提交于 2019-12-23 05:24:02

问题


Project Info: We are creating an installer project that allows users to choose a servername and databasename. After installation of the main program a corresponding database will be created.

Installer project :

  • Has an additional UI screen with two textboxes, containing servername and databasename.
  • An custom action will be executed on installation and has the servername and databasename defined as custom action data.

Main application:

  • An installer class wires the after-installation event. This class will automatically be executed during installation. The custom action data will be passed as paramaters to this class.
  • After-installation event will trigger a method that:
    • Creates the database
    • Gets the app.config file and adjusts the connectionstring.

Everything works well expect saving the configuration file: we recieve this error:

System.Configuration.ConfigurationErrorsException: An error occurred creating >the configuration section handler for entityFramework: Could not load file or >assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, >PublicKeyToken=b77a5c561934e089'

Strange enough we don't get any error with entityframwork when running and debugging the main application. Also we were able to execute the code as unittest, so this assembly problem occurs only when changing the configuration file during installation with visual studio installer

Below you can find the code to change the configuration file.

void DeployInstaller_AfterInstall(object sender, InstallEventArgs e)
{
  try
  {
    Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

    string connectionsection = config.ConnectionStrings.ConnectionStrings
    ["XBSDbDataContext"].ConnectionString;

    ConnectionStringSettings connectionstring = null;
    if (connectionsection != null)
    {
          config.ConnectionStrings.ConnectionStrings.Remove("XBSDbDataContext");
    }

    connectionstring = new ConnectionStringSettings("XBSDbDataContext", connectionString);
    config.ConnectionStrings.ConnectionStrings.Add(connectionstring);

    config.Save(ConfigurationSaveMode.Minimal, true);
    }
    catch (Exception ex)
    {
          MessageBox.Show(ex.ToString());      
    }
}

Any idea what can be the cause and how to resolve it? Many thanks in advance.

-- UPDATE -- WE FOUND A WORK ARROUND.

By avoiding to use the ConfigurationManager class to edit the configuration file we no longer have this problem. This is now done with the system.xml namespace.

  //updating config file
  XmlDocument XmlDoc = new XmlDocument();
  MessageBox.Show(Assembly.GetExecutingAssembly().Location + ".config");
  XmlDoc.Load(Assembly.GetExecutingAssembly().Location+".config");
  foreach (XmlElement xElement in XmlDoc.DocumentElement)
  {
       if (xElement.Name == "connectionStrings")
       {
            xElement.LastChild.Attributes["connectionString"].Value = connectionString;
       }
  }
  XmlDoc.Save(Assembly.GetExecutingAssembly().Location + ".config");

回答1:


If that Dll (or a dependency of it) is being installed to the GAC by your MSI, the problem is that installed GAC assemblies are not actually accessible in the GAC until the Commit stage of the install. Depite the name "AfterInstall" that event is actually "near the end of the install". If you move that custom action to be a Commit custom action and it works then that was the issue.

In general it often works better to do these things at first run of the app - see that it is not configured and then do the creation, connection string stuff etc. It is much more straightforward to debug; in an installer class you are running with the local system account (in a per machine install) or a non-elevated user (in a per user install) and these can cause issues with security etc. Apart from that, if the user wants to move the database, or the server name changes etc you could run some of that configuration again.



来源:https://stackoverflow.com/questions/32891384/changing-app-configuration-file-during-installation-process-with-visual-studio-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!