Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t从运行在GCP云上的.net应用程序连接到postgres实例_C#_Google Cloud Platform_Entity Framework Core_Npgsql_Google Cloud Run - Fatal编程技术网

C# Can';t从运行在GCP云上的.net应用程序连接到postgres实例

C# Can';t从运行在GCP云上的.net应用程序连接到postgres实例,c#,google-cloud-platform,entity-framework-core,npgsql,google-cloud-run,C#,Google Cloud Platform,Entity Framework Core,Npgsql,Google Cloud Run,我有一个应用程序在GCP云运行实例中运行,它使用映射在pgsql db上的EF核心,但由于某些原因,我无法连接到数据库 以下是用于连接数据库的代码: Startup.cs: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<postgresContext>(options => options.UseNpgsql(SecretManager

我有一个应用程序在GCP云运行实例中运行,它使用映射在pgsql db上的EF核心,但由于某些原因,我无法连接到数据库

以下是用于连接数据库的代码:

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<postgresContext>(options =>
        options.UseNpgsql(SecretManagerAccessor.GetConnectionString())
    );
}
最后是SourceRepository:

private readonly postgresContext _db;
private readonly DbSet<Source> _source;

public SourceRepository(postgresContext db)
{
    _db = db;
    _source = db.Set<Source>();
}

public IEnumerable<Source> GetAll()
{
    return _source;
}
我使用此代码简化了测试,但仍然存在相同的问题:

        //GET : api/MyController
[HttpGet]
public IActionResult Get()
{
    Console.WriteLine("Get route called");
    try
    {
        _context.Database.CanConnect();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    return Ok();
}
在创建实例时,可以使用flag
--addcloudsql实例
。Cloud Run将为您激活并配置云SQL代理

连接字符串应类似于此字符串
“服务器=/cloudsql/您的项目id:us-central1:实例名称;Uid=aspnetuser;Pwd=;数据库=投票”

你可以看一下


还有一个repo展示了如何在云上构建C#应用程序,并使用Postgres运行

使用sqlservermanagementstudio,看看是否可以连接到数据库?检查登录窗口以查看是否使用了windows凭据或用户名/密码。大多数情况下,您使用的是Windows凭据。因此,连接字符串没有用户名/密码,而是使用integratedsecurity=True。如果使用的是标准端口号1433,则不需要端口号。在登录窗口中,连接字符串应包括服务器IP和实例like。对于SSMS,这似乎只是一个连接错误。请包括更多细节。你的博士后跑哪?您如何连接它(通过公共IP或VPC)?我的Postgres运行在谷歌云SQL上(两者都在同一个项目中),它通过身份验证连接到公共IP。我不能在我的计算机上运行它,因为项目使用docker,我的公司阻止我使用它
A 2020-08-26T13:58:49.877242Z An error occurred using the connection to database 'MyDb' on server 'tcp://xx.xx.xxx.xxx:5432'. 
A 2020-08-26T13:58:49.877242Z System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. 
A 2020-08-26T13:58:49.877286Z  ---> Npgsql.NpgsqlException (0x80004005): Exception while connecting 
A 2020-08-26T13:58:49.877295Z  ---> System.TimeoutException: Timeout during connection attempt 
A 2020-08-26T13:58:49.877485Z    at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout) 
A 2020-08-26T13:58:49.877514Z    at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout) 
A 2020-08-26T13:58:49.877528Z    at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken) 
A 2020-08-26T13:58:49.877540Z    at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken) 
A 2020-08-26T13:58:49.877553Z    at Npgsql.ConnectorPool.AllocateLong(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken) 
A 2020-08-26T13:58:49.878103Z    at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext() 
A 2020-08-26T13:58:49.878128Z --- End of stack trace from previous location where exception was thrown --- 
A 2020-08-26T13:58:49.878136Z    at Npgsql.NpgsqlConnection.Open() 
A 2020-08-26T13:58:49.878145Z    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected) 
A 2020-08-26T13:58:49.878152Z    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) 
A 2020-08-26T13:58:49.878161Z    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) 
A 2020-08-26T13:58:49.878170Z    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result) 
A 2020-08-26T13:58:49.878180Z    at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) 
A 2020-08-26T13:58:49.878188Z    --- End of inner exception stack trace --- 
A 2020-08-26T13:58:49.878197Z    at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) 
A 2020-08-26T13:58:49.878203Z    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext() 
A 2020-08-26T13:58:49.878211Z    at MyProject.Controllers.MyController.Post(Body body) in /app/Controllers/MyController.cs:line 62 
        //GET : api/MyController
[HttpGet]
public IActionResult Get()
{
    Console.WriteLine("Get route called");
    try
    {
        _context.Database.CanConnect();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    return Ok();
}