C# IOOptions未从appsettings.Development.json获取值

C# IOOptions未从appsettings.Development.json获取值,c#,asp.net-core,api-gateway,C#,Asp.net Core,Api Gateway,我正在尝试使用NetCore创建网关api。当我尝试使用app.route重定向呼叫时: app.Run(async (context) => { using (var serviceScope = app.ApplicationServices.CreateScope()) { var routing = serviceScope.ServiceProvider

我正在尝试使用NetCore创建网关api。当我尝试使用app.route重定向呼叫时:

app.Run(async (context) =>
            {
                using (var serviceScope = app.ApplicationServices.CreateScope())
                {
                    var routing = serviceScope.ServiceProvider.GetService<IRoutingService>();

                    var content = await routing.RouteRequest(context.Request);
                    await context.Response.WriteAsync(await content.Content.ReadAsStringAsync());
                    content.Dispose();

                    // Seed the database.
                }
            });
我做错了吗?下面是RouteManagement类

public class RouteManagement
{
    public List<Routes> Routes { get; set; }
}
public class Routes
{
    public string Endpoint { get; set; }
    public Routes.DestinationManagement Destination { get; set; }


    public class DestinationManagement
    {
        private DestinationManagement()
        {
            Uri = "/";
            RequiresAuthentication = false;
        }
        public string Uri { get; set; }
        public bool RequiresAuthentication { get; set; }
    }

}
公共类路由管理
{
公共列表路由{get;set;}
}
公务舱路线
{
公共字符串端点{get;set;}
public Routes.DestinationManagement目的地{get;set;}
公共类目标管理
{
私人目的地管理()
{
Uri=“/”;
requireAuthentication=false;
}
公共字符串Uri{get;set;}
公共布尔要求重新验证{get;set;}
}
}

您是否在
ConfigureServices
方法中注册了
RouteManagement
绑定的配置实例


services.Configure(配置)

您是否已在
ConfigureServices
方法中注册了
RouteManagement
绑定的配置实例


services.Configure(配置)

请按照以下步骤解决问题:

  • 注册
    RouteManagement

    services.Configure<RouteManagement>(Configuration.GetSection("routeManagement"));
    
    public class RouteManagement
    {
        public List<Routes> Routes { get; set; }
    }
    public class Routes
    {
        public string Endpoint { get; set; }
        public Routes.DestinationManagement Destination { get; set; }
    
    
        public class DestinationManagement
        {
            public DestinationManagement()
            {
                Uri = "/";
                RequiresAuthentication = false;
            }
            public string Uri { get; set; }
            public bool RequiresAuthentication { get; set; }
        }
    
    }
    

  • 请按照以下步骤解决您的问题:

  • 注册
    RouteManagement

    services.Configure<RouteManagement>(Configuration.GetSection("routeManagement"));
    
    public class RouteManagement
    {
        public List<Routes> Routes { get; set; }
    }
    public class Routes
    {
        public string Endpoint { get; set; }
        public Routes.DestinationManagement Destination { get; set; }
    
    
        public class DestinationManagement
        {
            public DestinationManagement()
            {
                Uri = "/";
                RequiresAuthentication = false;
            }
            public string Uri { get; set; }
            public bool RequiresAuthentication { get; set; }
        }
    
    }
    

  • 将这些选项添加到服务集合的代码在哪里?另请参见{Startup.ConfigureServices}方法=>services.AddSingleton();是的,但是增加选项呢?您添加了服务,而不是选项。显示您的配置服务方法和IConfiguration交互。使用这些详细信息更新问题。引用将这些选项添加到服务集合的代码在哪里?另请参见{Startup.ConfigureServices}方法=>services.AddSingleton();是的,但是增加选项呢?您添加了服务,而不是选项。显示您的配置服务方法和IConfiguration交互。用这些细节更新问题。参考