'How do I setup tnsnames.ora in asp.net core web application?
I have a small asp.net core 2.2 app that should access an oracle db; I'm using NuGet Oracle.ManagedDataAccess.Core (2.18.6), and it just works on my machine.
When I deploy it to the windows server with IIS, I place a tnsnames.ora
file in app's directory and again it just works.
Now I want to use a shared tnsnames.ora
file. I have tried web.config
like following (recipy from StackOverflow/Google answers to similar questions).
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</configSections>
<!-- skipping app stuff -->
<oracle.manageddataaccess.client>
<version number="*">
<settings>
<setting name="tns_admin" value="F:\path\to\tnsadmin\folder" />
</settings>
</version>
</oracle.manageddataaccess.client>
</configuration>
Unfortunately, this doesn't work (and since the Core version of Oracle.ManagedDataAccess.dll
doesn't have an ODPMSectionHandler
class it isn't a big surprise).
So, is there a way to have a shared tnsnames.ora
file with odp.net core?
(PS imo we need the odp.net-core
tag)
UPDATE
One need to ensure the
w3wp.exe
can actually access the files.Setting system environment variable
TNS_ADMIN
is a possible solution. It'll suffice in this particular case, but I'm still curious about how to configure it viaweb.config
.
Solution 1:[1]
ODP.NET Core does not support config files like standard ODP.NET does. You will need to use the Configuration APIs:
Solution 2:[2]
With Oracle.ManagedDataAccess you don't need to use a tnsnames.ora file. In fact, you don't need an Oracle client installed on the web server. Use startup.cs.
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkOracle()
.AddDbContext<OracleDbContext>(builder => builder.UseOracle(Configuration["Data:OracleDbContext"]),ServiceLifetime.Scoped)
.AddDbContext<AppsDbContext>(option => option.UseOracle(Configuration["Data:AppsDbConnection:ConnectionString"]), ServiceLifetime.Scoped);
}
apsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"Data": {
"OracleDbContext": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=zzz)(PORT=1521))(CONNECT_DATA=(zzzz)));User Id=zzz;Password=zzz;" },
"AppsDbContext": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=yyyy)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=yyyy)));User Id=yyyy;Password=yyyy;" }
}
}
Solution 3:[3]
I just ran into the same issue, strangely enough setting env variables system wide didn't seem to work. I had to configure it in the web.config itself for it to find the TNS_ADMIN, like so:
<aspNetCore ...>
<environmentVariables>
<environmentVariable name="TNS_ADMIN" value="C:\Oracle\product\11.2.0\client_1\network\admin" />
</environmentVariables>
</aspNetCore>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Christian Shay |
Solution 2 | Charles Owen |
Solution 3 | Rik De Peuter |