4

I have an Event System which is using Entity Framework. I now get the error that the connection string with name 'X' cannot be found in the application config file.

In which config file do I have to place the connectionstring?

5
  • 1
    Assuming this is a connection string used by YOUR extension, have you tried including it in your app.config? If this doesn't work (I remember vaguely something failing here since the app is loaded by Tridion) you may have to use something as explained here: stackoverflow.com/a/1838960/866454
    – Nuno Linhares
    May 18, 2017 at 17:15
  • Hi Nuno, I use the dll.config solution as Marko Milic describes in his answer. That solution only works for app keys and not for connectionstrings. Maybe your solution is helping me out, I will let you know! May 19, 2017 at 9:14
  • Does this mean you are connecting to your own DB from a CM event handler? I would try to avoid that in general. For example, it causes transaction promotion, meaning you won't be able to run on a Cloud Database (after you have upgraded to SDL Web 8 :-) May 19, 2017 at 9:35
  • Hi @RickPannekoek, could you explain that? Why is this not possible at SDL Web 8? May 19, 2017 at 14:35
  • It is possible with SDL Web 8, but not if you want to run on a Cloud Database (SQL Azure or AWS RDS, which are supported as of SDL Web 8). Cloud Databases don't support Distributed Transactions. May 19, 2017 at 17:52

3 Answers 3

4

You can read data from app config with something like this:

  private static string GetFromAppSettings(string fieldName)
        {
            Configuration myDllConfig = ConfigurationManager.OpenExeConfiguration(typeof(PublishLogging).Assembly.Location);
            AppSettingsSection myDllConfigAppSettings = (AppSettingsSection)myDllConfig.GetSection("appSettings");
            String value = myDllConfigAppSettings.Settings[fieldName].Value;
            if (value != null)
            {
                return value;
            }
            else
            {
                return String.Empty;
            }
        }

Also, please make note that config file should be located in the same folder in which your event system dll is. That being said, please make note also that it should have same name as your built dll with .config extension.

If you still have problems, I suggest you remote debug event system. You can use this article for reference how:

1
  • Hi Marko, thank you for your answer! I already use a solution like that. That only works for appsettings keys and not for connectionstrings. May 19, 2017 at 9:22
1
/// <summary>
    /// Returns the Configuration object next to the current executing DLL
    /// </summary>
    private static Configuration _dllConfiguration = null;
    private static Configuration DllConfiguration
    {
        get
        {
            if (_dllConfiguration == null)
            {
                try
                {
                    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap()
                    {
                        ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config"
                    };
                    _dllConfiguration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                }
                catch (Exception ex)
                {
                    // do nothing
                }
            }
            return _dllConfiguration;
        }
    }

Then when using it,

string connectionString = DllConfiguration.ConnectionStrings.ConnectionStrings["MyConnection"];

Your app.config (or YourEvent.dll.config) will be like this

<configuration>
  <connectionStrings>
    <add name="MyConnectionString" connectionString="Data Source=server,port;Initial Catalog=CustomDB;Persist Security Info=True;User ID=user;Password=xxx"/>
  </connectionStrings>
</configuration>
0

The solution was a combination of the above. I already used the '.dll.config' file for configuration (via a custom class ConfigurationHelper). But .NET wasn't able to recognize the connectionstrings in that file.

I solved the issue by setting the connectionstring as appsetting:

<appSettings>
    <add key="configConnectionString" value="myconnectionString" />
</appSettings>

And then create a DbContext class as follows:

    public MyEntities(string configConnectionString) : base(ConfigurationHelper.GetAppSetting(configConnectionString))
    {
    }

So you can create the context like this:

MyEntities context = new MyEntities("configConnectionString");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.