加载资源失败:服务器响应状态为 500 () | Oracle & ASP.NET 核心 MVC 3.1
Failed to load resource: the server responded with a status of 500 () | Oracle & ASP.NET Core MVC 3.1
我有一个无法解决的问题。
我已经将我的 ASP.NET Framework 项目迁移到 ASP.NET Core,当我启动该项目时,我遇到了这些类型的错误:
This page isn't working
localhost is currently unable to handle this request.
HTTP ERROR 500
Failed to load resource: the server responded with a status of 500 ()
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Startup.cs :
public void ConfigureServices(IServiceCollection services)
{
[...]
services.AddDbContext<ApplicationDbContext>(options =>
options.UseOracle(Configuration.GetConnectionString("Web:ConnectionString")));
[...]
}
appsettings.json :
[...]
"ConnectionStrings": {
"Web": {
"ConnectionString": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=XX.XX.X.XXX)(PORT=XXXX))(CONNECT_DATA=(SERVICE_NAME=XXX)));User Id=XXXXX;Password=XXXXX;",
"ProviderName": "Oracle.ManagedDataAccess.Client"
}
}
[...]
DataAccessLayer.cs :
public class DataAccessLayer : IDisposable
{
private IDatabase _core = null;
public void Dispose() { _core?.Dispose(); }
public DataAccessLayer() { _core = Load(); }
private static Database Load()
{
// Get Instance of Configuration with appsettings.json
IConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
cfgBuilder.AddJsonFile("appsettings.json");
IConfiguration cfg = cfgBuilder.Build();
var connectionString = cfg["ConnectionStrings:Web:ConnectionString"];
var provider = cfg["ConnectionStrings:Web:ProviderName"];
if (provider != "Oracle.ManagedDataAccess.Client") {
return null;
}
return new Database(connectionString, DatabaseType.OracleManaged, SqlClientFactory.Instance);
}
public Client GetClient(string idWorkflow)
{
if (!string.IsNullOrEmpty(idWorkflow)) {
try {
return _core.FirstOrDefault<Client>(Sql.Builder.Where("ID_WORKFLOW=@0", idWorkflow));
}
catch (Exception) {
return new Client();
}
}
else {
return new Client();
}
}
[...]
}
你有什么想法吗?
尝试用这个替换 Load() 的 return:
return new Database(connectionString, DatabaseType.OracleManaged, new OracleClientFactory());
一定会更好^^
我有一个无法解决的问题。
我已经将我的 ASP.NET Framework 项目迁移到 ASP.NET Core,当我启动该项目时,我遇到了这些类型的错误:
This page isn't working
localhost is currently unable to handle this request.
HTTP ERROR 500
Failed to load resource: the server responded with a status of 500 ()
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Startup.cs :
public void ConfigureServices(IServiceCollection services)
{
[...]
services.AddDbContext<ApplicationDbContext>(options =>
options.UseOracle(Configuration.GetConnectionString("Web:ConnectionString")));
[...]
}
appsettings.json :
[...]
"ConnectionStrings": {
"Web": {
"ConnectionString": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=XX.XX.X.XXX)(PORT=XXXX))(CONNECT_DATA=(SERVICE_NAME=XXX)));User Id=XXXXX;Password=XXXXX;",
"ProviderName": "Oracle.ManagedDataAccess.Client"
}
}
[...]
DataAccessLayer.cs :
public class DataAccessLayer : IDisposable
{
private IDatabase _core = null;
public void Dispose() { _core?.Dispose(); }
public DataAccessLayer() { _core = Load(); }
private static Database Load()
{
// Get Instance of Configuration with appsettings.json
IConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
cfgBuilder.AddJsonFile("appsettings.json");
IConfiguration cfg = cfgBuilder.Build();
var connectionString = cfg["ConnectionStrings:Web:ConnectionString"];
var provider = cfg["ConnectionStrings:Web:ProviderName"];
if (provider != "Oracle.ManagedDataAccess.Client") {
return null;
}
return new Database(connectionString, DatabaseType.OracleManaged, SqlClientFactory.Instance);
}
public Client GetClient(string idWorkflow)
{
if (!string.IsNullOrEmpty(idWorkflow)) {
try {
return _core.FirstOrDefault<Client>(Sql.Builder.Where("ID_WORKFLOW=@0", idWorkflow));
}
catch (Exception) {
return new Client();
}
}
else {
return new Client();
}
}
[...]
}
你有什么想法吗?
尝试用这个替换 Load() 的 return:
return new Database(connectionString, DatabaseType.OracleManaged, new OracleClientFactory());
一定会更好^^