IOptions 未从 appsettings.Development.json 获取值
IOptions not getting the values from appsettings.Development.json
我正在尝试使用 net core 创建网关 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.
}
});
... RoutingService 服务启动如下:
public class RoutingService : IRoutingService
{
private readonly RouteManagement _routeManagement;
static HttpClient _client = new HttpClient();
public RoutingService(IOptions<RouteManagement> routeManagement)
{
_routeManagement = routeManagement.Value;
}
...
.. 我无法从 json 文件中获取值。以下是 json 文件:
{
"tokenManagement": {
"secret": "Any String used to sign and verify JWT Tokens, Replace this string with your own Secret",
"issuer": "threenine.co.uk",
"audience": "SampleAudience",
"accessExpiration": 30,
"refreshExpiration": 60
},
"routeManagement": {
"Routes": [
{
"Endpoint": "/coupons",
"Destination": {
"Uri": "http://localhost:30561/coupons/",
"RequiresAuthentication": "true"
}
},
{
"Endpoint": "/songs",
"Destination": {
"Uri": "http://localhost:8091/songs/",
"RequiresAuthentication": "false"
}
}
]
}
}
我做错了吗?下面是classRouteManagement
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; }
}
}
您是否在ConfigureServices
方法中注册了RouteManagement
绑定的配置实例?
services.Configure<RouteManagement>(Configuration);
按照以下步骤解决您的问题:
注册RouteManagement
services.Configure<RouteManagement>(Configuration.GetSection("routeManagement"));
需要使DestinationManagement()
public,否则会初始化DestinationManagement
失败
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; }
}
}
我正在尝试使用 net core 创建网关 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.
}
});
... RoutingService 服务启动如下:
public class RoutingService : IRoutingService
{
private readonly RouteManagement _routeManagement;
static HttpClient _client = new HttpClient();
public RoutingService(IOptions<RouteManagement> routeManagement)
{
_routeManagement = routeManagement.Value;
}
...
.. 我无法从 json 文件中获取值。以下是 json 文件:
{
"tokenManagement": {
"secret": "Any String used to sign and verify JWT Tokens, Replace this string with your own Secret",
"issuer": "threenine.co.uk",
"audience": "SampleAudience",
"accessExpiration": 30,
"refreshExpiration": 60
},
"routeManagement": {
"Routes": [
{
"Endpoint": "/coupons",
"Destination": {
"Uri": "http://localhost:30561/coupons/",
"RequiresAuthentication": "true"
}
},
{
"Endpoint": "/songs",
"Destination": {
"Uri": "http://localhost:8091/songs/",
"RequiresAuthentication": "false"
}
}
]
}
}
我做错了吗?下面是classRouteManagement
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; }
}
}
您是否在ConfigureServices
方法中注册了RouteManagement
绑定的配置实例?
services.Configure<RouteManagement>(Configuration);
按照以下步骤解决您的问题:
注册
RouteManagement
services.Configure<RouteManagement>(Configuration.GetSection("routeManagement"));
需要使
失败DestinationManagement()
public,否则会初始化DestinationManagement
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; } } }