【发布时间】:2021-09-30 13:57:47
【问题描述】:
我想访问 swagger.json 文件并将其反序列化并存储到缓存中。
我正在IHostedService 中进行异步调用。
public class InitializeCacheService : IHostedService
{
private IMemoryCache _cache;
public static readonly string swaggerJson = "swagger.json";
private readonly HttpClient _httpClient;
private readonly IServiceProvider _serviceProvider;
public InitializeCacheService(IMemoryCache cache, HttpClient client, IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_httpClient = client;
_cache = cache;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
if (!_cache.TryGetValue(swaggerJson, out SwaggerDocumentCache cacheEntry))
{
_httpClient.BaseAddress = new Uri("https://localhost:44397");
var response = await _httpClient.GetAsync("swagger/v1/swagger.json");
response.EnsureSuccessStatusCode();
using var responseStream = await response.Content.ReadAsStreamAsync();
cacheEntry = await JsonSerializer.DeserializeAsync
<SwaggerDocumentCache>(responseStream);
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(3));
_cache.Set(swaggerJson, cacheEntry, cacheEntryOptions);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
但是应用程序一直在旋转,并且正在超时
var response = await _httpClient.GetAsync("swagger/v1/swagger.json");
它给出的错误是:
IOException: Unable to read data from the transport connection: I/O 操作由于线程退出或应用程序请求而中止。
我在这里缺少什么?访问swagger.json 以构建自定义 API 游乐场应用程序的正确方法是什么?
【问题讨论】:
-
写一个api来读取swagger.json的内容,然后向这个api发送请求获取数据?
-
我被困在以编程方式阅读 swagger.json 的内容
-
感谢您的回复,根据您的 sn-p,您似乎正在发送一个 http 请求以从另一台服务器获取 json 文件内容。我的意思是你可以在那个服务器程序中写reading content code。
-
我尝试了您提供的方式,但这也不起作用。它仍然让我遇到超时错误。因为 swagger.json 在任何文件夹中实际上都不可用。
-
我在下面展示了我的想法,这里你说
is not physically available in any folder,那么为什么你尝试通过https://localhost:44397/swagger/v1/swagger.json访问文件呢?先生,我很困惑。
标签: c# asp.net-core swagger asp.net-core-5.0 swashbuckle.aspnetcore