The 'Oracle.DataAccess.Client.Oracleconnection' type initializer triggered an exception with c#

Asked

Viewed 3,651 times

1

My wpf program worked very well. They moved it to make some changes. Well, they asked me to change some folders in the application. Before I make any changes I went to run the application and is giving me this error:

The 'Oracle.DataAccess.Client.Oracleconnection' type initializer triggered an exception

The fact occurs in this line:

var diferenca = _listaCommiter().Except(_listaFarm());

This is the complete code of the method:

private List<string> RetornaDiferenca()
{
    List<string> listaDiferenca = new List<string>();

    try
    {
        var diferenca = _listaCommiter().Except(_listaFarm());

        foreach (var _diferenca in diferenca)
        {
            listaDiferenca.Add(_diferenca);
        }

        diferenca = null;

        return listaDiferenca;
    }
    catch (Exception ex)
    {
        string r = ex.Message;
        return null;
    }
}

That is the method _listaFarm()

private List<string> _listaFarm()
{
    List<string> lista = new List<string>();

    ConexaoBanco cb = new ConexaoBanco();

    lista = cb.CriaConexao();

    return lista;
}

It is a select on a BD Tablea.

I realized that in the method where I take the connection, is giving the problem. This is the method:

public List<string> CriaConexao()
{
    string conexao = ConfigurationManager.ConnectionStrings["FarmExternaConnect"]
                .ConnectionString;
    List<string> listaArquivoFarmExterna = new List<string>();

    OracleConnection conn = new OracleConnection(conexao);
    conn.Open();

    OracleCommand cmd = new OracleCommand();
    cmd.Connection = conn;
    cmd.CommandText = "select path, arquivo from gh_arquivos_farm_externa";
    cmd.CommandType = CommandType.Text;

    try
    {
        OracleDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            listaArquivoFarmExterna.Add(dr.GetString(0) + dr.GetString(1));
        }

        return listaArquivoFarmExterna;
    }
    catch(Exception ex)
    {
        return null;
    }
    finally
    {
        conn.Close();
        cmd.Dispose();
    }

The mistake comes in this line: OracleConnection conn = new OracleConnection(conexao);

This is the Inner Exception:

The Provider is not compatible with the version of Oracle client

My connection string:

<add name="FarmExternaConnect" connectionString="Data Source=DESENV; Persist Security Info=True; User ID=Eu; Password=teste"/>

More lines in my app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework"
      type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx"
      requirePermission="false"/>
    <section name="oracle.manageddataaccess.client"
      type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <appSettings>
    <!-- Pastas de Projetos .NET que estão na Farm Externa, separados por vírgula -->
    <add key="ProjetosDotNet" value="teste"/>
  </appSettings>

   <connectionStrings>
     <add name="FarmExternaConnect" connectionString="Data Source=DESENV; Persist Security Info=True; User ID=user; Password=teste"/>
   </connectionStrings>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client"/>
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
        type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxx"/>
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <publisherPolicy apply="no"/>
        <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="xxxxxxxxxxxxxxx" culture="neutral"/>
        <bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
      </dataSources>
    </version>
  </oracle.manageddataaccess.client>
</configuration>

2 answers

2


I took out TNS and made the system search the server and that way I got the connection to the bank and it worked. This is the way I found: <add name="FarmExternaConnect" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=nome_do_servidor)(PORT=1521))(C‌​ONNECT_DATA=(SERVICE_NAME=XE)));User id=user;Password=teste;"/>

  • Have you managed to solve it this way? If yes, mark it as an answer so that others can take advantage of the solution :)

  • 1

    I can only do it after two days. Rules of the site.

1

@pnet, if you show the exact error being generated (if possible the Stacktrace) helps a lot in understanding the error and finding the solution. In this case, I update this answer.

For now, let’s work with what we have :)

There are some items that need to be checked and that may be the root of the error:

  • To ConnectionString with the key FarmExternaConnect does not exist or is invalid in App.config (you can find a ConnectionString valid on the site connectionstrings.with)

  • You need to check whether the DLL of Provider is in the folder Bin, referenced in the project or if it is installed via Nuget (More information on MSDN and you can still install a package via Nuget

I hope I helped the/

  • I made an edit on the original post, with the error Inner Exception.

  • Try to post your ConnectionString and any tag that is related to Provider in the App.config (Don’t forget to change the connection data so it doesn’t expose your connection to the database :D). Probably the version that is set in Provider (App.config) is different from DLL that is in your project (either direct or via Nuget).

  • I posted my app.config. My question is that the client works and not here in the company. In our company we use Express 11g. I don’t know if that would be any kind of problem.

  • I installed the Oraclemanaged driver and I managed to go through the above line with problems, but is giving this error: ORA-12154: TNS:não foi possível resolver o identificador de conexão especificado. I don’t know if this would generate a new thread or if it could stay in this current one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.