Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
.net 如何在没有DbOptions的情况下获取连接字符串?_.net_Connection String_Blobstorage - Fatal编程技术网

.net 如何在没有DbOptions的情况下获取连接字符串?

.net 如何在没有DbOptions的情况下获取连接字符串?,.net,connection-string,blobstorage,.net,Connection String,Blobstorage,我是一个不需要DbContext类的项目,我在appsettings.json上使用BlobStorage DB { "ConnectionStrings": { "StorageConnectionString": "Connection" } 这是我的appsettings.json { "ConnectionStrings": { "StorageConnectionStr

我是一个不需要DbContext类的项目,我在appsettings.json上使用BlobStorage DB

{
  "ConnectionStrings": {
    "StorageConnectionString": "Connection"
 }
这是我的appsettings.json

{
  "ConnectionStrings": {
    "StorageConnectionString": "Connection"
 }
我在Startup.cs类上配置,没有选项,因为我现在不需要DbContext

_configuration.GetConnectionString("StorageConnectionString");
但是我需要在BlobStorageService类上调用我的连接字符串,而不需要传递连接字符串名称(因为它已经在appsettings.json上)

问题是:如何在不传递连接字符串名称的情况下调用服务类上的connectionString?

如果使用

 Configuration.GetConnectionString("StorageConnectionString"); 

在我的BlobStorageService中获取IConfiguration并执行依赖项注入。

您如何注册您的
BlobStorageService
?如果您已经在
Startup.cs
中检索连接字符串,则始终可以使用字符串依赖项注册
BlobStorageService
,以便在运行时在服务中使用

在您的
BlobStorageService
中,您可以要求在请求服务时插入字符串:

public class BlobStorageService : IBlobStorageService
{
    private readonly string _connectionString;

    public BlobStorageService(string connectionString)
    {
        _connectionString = connectionString;

        // Do whatever storage client initialization is required by the SDK...
    }
}
然后,在
Startup.cs
中,使用应用程序启动时已检索到的连接字符串注册依赖项:

public void ConfigureServices(IServiceCollection services)
{
    var yourConnectionString = Configuration.GetConnectionString("StorageConnectionString");
            
    // Register with the appropiate service lifetime
    services.AddTransient<IBlobStorageService>(_ => new BlobStorageService(yourConnectionString));

    // ...other dependencies
}
public void配置服务(IServiceCollection服务)
{
var yourConnectionString=Configuration.GetConnectionString(“StorageConnectionString”);
//在适当的服务期限内注册
AddTransient(=>newblobstorageservice(yourConnectionString));
//…其他依赖项
}

返回空值。如果我使用Configuration.GetConnectionString(“StorageConnectionString”);但是我需要在BlobStorageService中进行IConfiguration和依赖注入,我已经在Startup.class上完成了这项工作。所以,我正在使用azure blobs和服务总线服务。我如何使用多个ConnectionString实现这一点?(每个服务提供一个特定的主连接)。