How to run odp.net application in both .net 3.5 and .net 4 - oracle

I am working on an internal tool for our product. Our product uses oracle database and have evolved over time from .net framework 2.0 to 4.5 and Oracle 10 to 12.2.
The aim of the tool is to write a single application which works across different versions of the product.
I have solved the problem of multiple .net framework versions by using the following entries in app.config
<supportedRuntime version="v2.0.50727"/>
<supportedRuntime version="v4.0"/>
As the oracle managed .net driver is supported for framework >=4.0, I can not use this as I have to support .net framework 3.5 also.
As I have to use unmanaged odp.net driver, I was thinking about the following scenario
My tool would use the lowest version of oracle.dataaccess.dll and target .net 3.5.
Following #1 above makes me refer to 2.xx.... version of the oracle.dataaccess.dll.
When I run this application on a machine with only .net framework 4 installed, what would be the behavior? Would it load 4.xx... version of oracle.dataaccess dll when running under the context of .net framework 4?
The best solution for this would have been availability of oracle managed driver for .net 3.5 version but I found that it is not available.
Please provide your valuable inputs.
Satish
UPDATE :
I have written a sample application targeting .net framework 3.5. In this sample app, I will build a connection string and just open a connection and close it.
This application runs successfully when there are no <supportedRuntime> tags in the app.config.
When we add any <supportedRuntime> tags in the app.config, I am getting a type initializer exception for oracle related types. I have tried this with the supported run time tags
<supportedRuntime version="v2.0.50727"/>
<supportedRuntime version="v4.0"/>
individually and both combined. But I am still getting the issue. Can anyone suggest how to resolve this issue?

ODP.NET unmanaged driver exist in following versions:
1.x (.NET Framework 1.0.3705/1.1.4322), available up to Oracle Client 11.1.
2.0 (.NET Framework 2.0.50727), introduced with Oracle Client 10.2
4.0 (.NET Framework 4.0.30319), introduced with Oracle Client 11.2
If your compile target is .NET version 3 or 3.5 then the application will try to load ODP.NET version 2.0 (and will raise an exception if it is not found on the machine). Actually I am not sure if it would also accept ODP.NET version 4.0.
If your compile target is .NET version 4 or higher then the application will try to load ODP.NET version 4.0 (and will raise an exception if it is not found on the machine).
You can do several solutions:
Provide a copy of Oracle.DataAccess.dll which matches your version and put it in your application directory.
Use late binding, i.e. instead of
var con = new Oracle.DataAccess.Client.OracleConnection();
use
var DLL = Assembly.Load(String.Format("Oracle.DataAccess, Version={0}.{1}.*.*, Culture=neutral, PublicKeyToken=89b483f429c47342", frameworkVersion, oracleVersion));
var type = DLL.GetType("Oracle.DataAccess.Client.OracleConnection", true, false);
dynamic con = Activator.CreateInstance(type)
However, this syntax is only available from .NET Framework version 4.0 on, I do not know how to write this in version 3.0/3.5.
Note, use con.GetType().Assembly.FullName and con.GetType().Assembly.Location in order to see which DLL was really loaded.

Related

how to resolve target .NET Framework with Xamarin

i am new to development with Xamarian platform.Now i just want to run application to targetFramework 4.5. but there diff targetframework listed as below. how i resolve that please help me out.
See https://learn.microsoft.com/en-us/dotnet/standard/net-standard - in the table, note that .Net Framework 4.5 supports the newer net-standard up to 1.1. So choose .NET Standard 1.1. Or if you can require .Net Framework 4.5.1 and newer, choose .NET Standard 1.2.
Or better, whatever dependency is forcing you to use Framework 4.5, talk to the vendor, get them to rebuild for .NET Core 3.1 or the new .NET 6.
If you really must support .Net "Framework" APIs, see https://learn.microsoft.com/en-us/archive/msdn-magazine/2017/connect/net-introducing-the-windows-compatibility-pack-for-net-core, for a (no longer being updated) solution that allows your newer code to be "Net Core", yet work with legacy .net APIs.
Be aware all this is "legacy" - Microsoft's goal is to move everyone to .NET 6.

What Non-4.* .NET App Versions play well with Standard?

I am building an MVC Web API (Service) with Views returned in specific cases. As an architectural decision, I've been directed to NOT build the service project in .NET Framework 4.*. Rather, I am to attempt .NET 5.0 first, and then Core 3.1 if 5.0 doesn't work.
This service project in my solution will depend on a few class library projects, call them DataLibrary, ComplexLibrary, and DocLibrary. DataLibrary will depend on a Nuget package of Oracle, be it ODP or Oracle Managed Data, in order to query an Oracle database via an Oracle Package on that database. DocLibrary will depend on a Nuget package of Aspose Word & Aspose PDF. ComplexLibrary will depend on Oracle AND Aspose.
Here's my dilemma:
Aspose Word's latest stable release (21.6) will report that it is compatible with 5.0 and Standard 2.0, but not .NET Core.
Oracle Managed Data reports that it is compatible with Standard 2.1 or Standard 2.0, but not 5.0 or .NET Core.
My own libraries have reported that they are not compatible with my API .csproj if...
3a. The API is 5.0 and the libraries are .NET Core or .NET Standard
3b. The API is Core 3.1 and the libraries are .NET Standard.
Since my compile script naturally requires a run of Nuget to retrieve all the necessary dependencies, I cannot get a clean compile because I seemingly have no combination of versions for my WebAPI and libraries that satisfy each others' compatibility needs. Since Standard libraries are the only common .NET version that satisfy the needs of both Aspose & Oracle, What available version for my WebAPI (i.e. I don't believe that Standard is an option for anything other than a class library) is compatible with .NET Standard libraries?
Thanks.
Please see the following article https://learn.microsoft.com/en-us/dotnet/standard/net-standard.
.NET Standard is not a framework it is kind f specification and .NET Core, .NET 5, Mono framework etc are .NET Standard implementations.
So for library projects I would select .NET Standard and for the service .NET Core or .NET 5 (which is actually the next version of .NET Core)
Well, don't I feel silly.
Turns out, the issue wasn't incompatibilities within Aspose, Office, .NET Core, 5.0, and Standard, but a failure of a prior version of NuGet to handle the different versions.
While my VS 2019 install was able to compile the whole solution effectively, my local install of NuGet was not. This was due to VS2019 likely using the most up-to-date version as of this post (5.9.#), while my locally installed version was 4.9.#. Thus, VS was able to sail through while my compile script kept failing at the NuGet stage (which I have included prior to the actual compile.) Once I ran a NuGet update, everything was good to go.
Long story short: KEEP YOUR NUGET VERSION UP-TO-DATE!!!

How to connect with SQL Server database using .NET Core Class Library (.NET Standard)?

I have just started to create an ASP.NET Core Web API Project. I am not much aware of "ASP.NET Core .NET Standard Library".
I am creating this application using Visual Studio 2017 RC and in the application, I have taken a project of type Class Library (.NET Standard) at repository layer.
Following is the screenshot for the same:
Now from repository Layer I want to connect to the database. I have created a variable
IDbConnection con;
Now I am trying to add reference of System.Data but I am unable to add any reference because when I am opening the add reference window then I am getting the following message:
No Framework assemblies were found on the machine.
How can I connect to database using .NET Core Class Library(.NET Standard)?
.NET Standard Class libraries don't work by directly referencing a DLL, because with .NET Core there is no guarantee the framework will be installed on the system and .NET Core applications can also run as self-contained applications which ship the framework libraries with the application and do not require a runtime to be installed before.
You have to use the NuGet package manager (or project.json or *.csproj in VS2017) to add dependencies. For SQLServer you need to add the System.Data.SqlClient package (link) if you want to directly communicate with the Database (i.e. w/o an ORM).
Above answer (Tseng) may have been valid in 2016 and .NET Standard 1.4, but in the meantime, Microsoft did remove some showstoppers, allowing access to SQL Server from a .NET Standard 2.1 library. Mind the old System.Data.SqlClient will not link, so you (really!) have to refer EF6 via NuGet and change some using.
Create a .NET Standard Class library and put version on 2.1
Install Entity framework (this contains the lower level libs)
In using, refer to System.Data and to Microsoft.Data
Using are:
using System.Data
using Microsoft.Data.SqlClient
Now, "legacy" classes like DataSet, SqlConnection and SqlClient will become available.
It is not completely compatible (yet) There are some things that are not available in .NET standard 2.1, such as enumerating available SQL servers on the LAN. This was done with SqlDataSourceEnumerator which is a class in System.Data I cannot locate in Microsoft.Data.
NOTE: I tested successfully with a .Net Core 3.1 console application. A Standard lib configured as above can be called from .NET Core and connect to the database without issues. You cannot use a .NET Framework caller.

How to validate prerequisite for ODP and Oracle Client connection

I have a very simple application on .Net that connect to Oracle using ODP (was compiled with Oracle.DataAccess dll 10.2).
I want to check "connection prerequisite" (application will be able to connect to DB) before installing this aplication on other computers?
How can I check that ODP.NET version 10.2 or higher is installed on target host (registry check is not enopugh)?
I know that I can check ODP entries in registry - but this will not check Oracle Client and compatibility between client and ODP.
Also, if ODP was installed by ODAC XCopy installation - registry may be not updated by new ODP entry.
And - if I am installing on the computer, DB was installed on, Oracle Client can be not installed separately.
And one more issue: When I tried to run "test connection" method from installation, that use referenced dll on machine, that has only ODAC (with ODP) 11, - "Oracle.DataAccess.Client.OracleException The provider is not compatible with the version of Oracle client at Oracle.DataAccess.Client.OracleInit.Initialize()
at Oracle.DataAccess.Client.OracleConnection..cctor()" error message was thrown (policies for both ODAC 10.2 and 11 are exists in GAC (assembly), pointing to Oracle.DataAccess 11 from ODP 2x bin).
So, How and which components can I check to ensure appropriate versions of ODP and Oracle Client were installed and application will be connected?
Thank you in advance!
You can use the DataProvider Factory Classes from .Net. This will show all data providers that are available to your current .Net installation / process. I have a blog post here on how to do it. I put it in place to prevent our users from using versions of Oracle that were not supported with .Net 4.0.
http://blog.tsells.com/2011/05/12/oracle-11g-release-2-and-net-framework-4-0-and-version-checking/
I recently found out that Oracle is not supporting the .net 4.0 framework with any version of the Oracle Provider for .Net prior to 11.2.0.2. (See Oracle Data Provider for .NET (ODP) Supported Configurations [ID 726240.1] from oracle support for more details).
Since the prior versions work (10.2, 11.1, 11.2.0.1) then I needed a way to prevent end users from using the wrong version. I searched the net and could not find any one who was able to do this so I did some digging. After some testing I came up with the following code.
System.Data.Common.DbProviderFactory factory =
System.Data.Common.DbProviderFactories.GetFactory("Oracle.DataAccess.Client");
if (factory == null)
return false;
Type t = factory.GetType();
int majorversion = t.Assembly.GetName().Version.Major;
// Do not allow any major versions less than 4
if (majorversion < 4)
return false;
This code will use the same data provider that is registered with the .net framework version you are using. This ensures you are not out of sink in your environment and allows you to inspect the version of the dll installed. For this example – I am just checking that ODP is 4.0 or higher (first release of odp for .net 4).

Which Oracle Client and ODP.NET version should I install (using .NET 3.5)

After quite some time developing with Oracle Client 9.2.0.7, and the ODP.NET to go with it, targeting .NET 1.1, we are upgrading our code line to .NET 3.5 and we are also considering upgrading the Oracle Client version.
I wonder if there are any "gotchas" as to whether specific versions should be used/not used?
For example Oracle Client 9.2.0.4 was known to be buggy with .NET and the upgrade to 9.2.0.7 was non optional.
Apparently the current Oracle Client version is 11.1.0.7.0 (from here). Has anyobody had specific issues with this version and would recommend other version? Sometimes installing the latest release isn't the best choice...
Take a look at the following document from Metalink, it might help. It contains a matrix of ODP Driver Version, Supported Operating System, SQL*Net, RDBMS, .NET Framework, and Visual Studio.
Subject: Oracle Data Provider for .NET (ODP) Supported Configurations
Doc ID: 726240.1
If all you need to do is talk to Oracle, then you can use my instructions found here for the Oracle 11 Client XCopy deploy.
The latest version is the only version that you can use if you have visual studio 2008 unless you don't want any of the snazzy add ins that Oracle provides with it (such as SQLPlus and such). I use the latest version to connect to a 10g database just fine.

Resources