9

We are receiving this error when calling a WCF .net 4.0 service using entity framework.

The 'DbProviderFactories' section can only appear once per config file

It is the first app on the server using EF and other .net 4.0 WCF services are not receiving this error.

Is there any way to correct this error with out editing the machine config file on the server?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Chad
  • 1,512
  • 1
  • 16
  • 40
  • The reason is our server team wants me to prove that this is the problem before they are willing to make a change to the machine config. So I have to fix the problem with out editing the config so that they will fix the machine config... yes that problem – Chad Dec 11 '12 at 14:54
  • 1
    Can you post the bits of the config file around where it defines the DbProviderFactories section? This page says you could delete the 2nd DbProviderFactories tag and it fixes the problem. Is this the problem you have?: http://forums.asp.net/t/1693277.aspx/1 – Eric Leschinski Dec 11 '12 at 15:05
  • @EricLeschinski - No I can can not have access to the config. I am aware of that fix... it is what I am trying to prove is needed by working around it. – Chad Dec 11 '12 at 16:12
  • 1
    They won't even let you SEE the config file?? Wow, I haven't played "Turf wars" in a while. Sounds like a game theory/politics problem. Put on your politician hat. Send the link above and the error message to your manager and say: "Please forward these notes to the server team, as it is not physically possible for me to fix their buggy software since they won't let me even see it. Then it's out of your hands. Don't use a hammer to strike at fire, use the effective response: water. You'll have those posturing politicians wriggling for a new vantage point in no time. – Eric Leschinski Dec 11 '12 at 17:14

4 Answers4

10

The installation for IBM DB2 .NET provider, causes an empty DbProviderFactories, see below. Just delete the second empty entry DbProviderFactories

<system.data>
    <DbProviderFactories>
        <add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26" />
    </DbProviderFactories>
    <DbProviderFactories />
</system.data>
yonsk
  • 163
  • 1
  • 8
  • Is there any way to correct this error with out editing the machine config file on the server? - I do not have access to the machine config to remove it. – Chad Feb 07 '14 at 15:18
  • Hi Chad, not that I know of, I had to touch and amend this file. Ask you sysadmin to action this for you. – yonsk Feb 21 '14 at 11:13
4

Maybe you could create web.config entries which override any machine-wide settings you want changed.

Described here:

Override machine.config by web.config

Putting the <clear /> instruction inside of the DbProviderFactories tags in the web config to clear out and then override the duplicate entries made in the machine config. Thus doing a hack-work around of the error in the machine.config.

Community
  • 1
  • 1
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 2
    Thank you I was able to use this to override the machine config and prove that they needed to fix the machine config... I <3 politics – Chad Dec 21 '12 at 15:10
4

You have to update Machine.config file located in the below paths.

  • C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\Machine.Config
  • C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\Machine.Config

For 64-bit machines, Machine.config will be located in ...\Framework64\...

The block to pay attention to is this:

<system.data>
    <DbProviderFactories>
        <add name="IBM DB2 for i5/OS .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for i5/OS" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26"/>
        <add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
        </DbProviderFactories>
<!-- This is the line to remove - empty element --><DbProviderFactories/>
</system.data>
Hellion
  • 1,740
  • 28
  • 36
Sundeep
  • 2,035
  • 2
  • 23
  • 38
  • from the question `Is there any way to correct this error with out editing the machine config file on the server?` That is the key part... – Chad Mar 11 '15 at 18:40
  • Sorry Chad. I missed that line. I just focused on the title. I was looking for a way to fix the error & I found the solution else where. I thought to share it here so it will be helpful for someone. – Sundeep Mar 12 '15 at 15:15
  • 1
    Important to note : You have to be admin to be able to save the file after you delete the line. – Alexandre Aug 03 '16 at 18:11
1

As @yonsk already mentioned why this problem occurs (duplicate entry of ), you can create a console application which can fix the machine.config file and then, invoke that console application from your Application's Installer or from your Application whenever you get the Exception. The following code can be used for the console application that will fix the machine.config file.

class Program
    {
        static void Main()
        {
            string machineConfigFilePath = RuntimeEnvironment.SystemConfigurationFile;

            XDocument xdoc = XDocument.Load(machineConfigFilePath);

            XElement[] elements = xdoc.XPathSelectElements("//configuration/system.data/DbProviderFactories").ToArray();

            if (elements.Any())
            {
                foreach (XElement anElement in elements)
                {
                    if (!anElement.HasElements)
                        anElement.Remove();
                }
            }

            xdoc.Save(machineConfigFilePath);
        }
    }

If you want to call the console application, from your Application, you would need to invoke that as Administrator. So, the following snippet may help to invoke that console application as Administrator (The user will be prompted with a dialog to accept..)

 try
            {
                Process process = Process.Start(new ProcessStartInfo
                {
                    Verb = "runas",
                    FileName = "/Path/to/the/console/application",
                    UseShellExecute = true,
                    CreateNoWindow = true,

                });
                process.WaitForExit();
                int exitCode = process.ExitCode;
            }
            catch (Exception ex)
            {

            }
Emran Hussain
  • 11,551
  • 5
  • 41
  • 48
  • 2
    I am pretty sure if I tried running that I would have been fired... But cool solution. – Chad Apr 01 '14 at 23:44