小编Eze*_*yen的帖子

OutputCache 属性不适用于 .NET 7 API 端点

我有一个 API REST .NET 7,即使在未经授权的端点中,属性 [OutputCache] 也不会缓存

我添加了这个:

services.AddOutputCache(options => 
{    
   options.AddBasePolicy(builder => builder.Cache()); //removing this line doesn't work either            
});
Run Code Online (Sandbox Code Playgroud)

然后在Configure()中:

app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
   endpoints.MapControllers();
});

app.UseOutputCache(); //putting it higher doesn't work either
Run Code Online (Sandbox Code Playgroud)

我的端点如下所示:

[AllowAnonymous] 
[OutputCache(Duration = 99999)] 
[HttpGet] 
public async Task<IActionResult> GetAsync([FromQuery] Dto dto) => ReturnDataFromDataBase();
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

我遵循文档https://learn.microsoft.com/en-us/aspnet/core/performance/caching/output?view=aspnetcore-7.0

编辑: 我添加更多信息,这个项目是.net 5,最近更新到.net 7([OutputCache]是在.net 7中引入的,这就是我的理解)。它不起作用,因为每次我(使用 Postman)向此端点发出请求时,它都会进入 ReturnDataFromDataBase 方法(我放置了一个断点)。我无法分享我的项目,因为它不是我的,但这是启动的配置方法(请随时纠正我):

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider)
    {
        //app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
        app.UseCors();
        app.UseAuthentication();
        app.UseExceptionHandler("/Error");
        app.UseIpRateLimiting();

        app.UseMvc(routes => …
Run Code Online (Sandbox Code Playgroud)

c# caching outputcache asp.net-web-api .net-7.0

10
推荐指数
2
解决办法
2957
查看次数

如何使用 SQL Server 数据库设置 dotnet Testcontainers 进行集成测试?

我有一个 API REST .NET 7,我想使用 sql server 容器数据库构建集成测试。我尝试遵循文档中的示例(https://dotnet.testcontainers.org/examples/aspnet/):

const string weatherForecastStorage = "weatherForecastStorage";

var mssqlConfiguration = new MsSqlTestcontainerConfiguration();
mssqlConfiguration.Password = Guid.NewGuid().ToString("D");
mssqlConfiguration.Database = Guid.NewGuid().ToString("D");

var connectionString = $"server={weatherForecastStorage};user id=sa;password={mssqlConfiguration.Password};database={mssqlConfiguration.Database}";

_weatherForecastNetwork = new NetworkBuilder()
 .WithName(Guid.NewGuid().ToString("D"))
 .Build();

_mssqlContainer = new ContainerBuilder<MsSqlTestcontainer>()
 .WithDatabase(mssqlConfiguration)
 .WithNetwork(_weatherForecastNetwork)
 .WithNetworkAliases(weatherForecastStorage)
 .Build();

Run Code Online (Sandbox Code Playgroud)

但 MsSqlTestcontainer 和 MsSqlTestcontainerConfiguration 类不存在。

nugget 包版本是 3.0.0,但官方文档似乎已经过时,因为构造函数 ContainerBuilder<> 也已过时。

我正在寻找的是创建一个带有 SQL SERVER 映像的容器,然后创建一个数据库/表并将其与实体框架核心一起使用(我不知道如何创建它们,甚至不知道这是否有必要)。

另一方面,我尝试了这样的事情:


       private readonly IContainer _dbContainer = new ContainerBuilder()
               .WithImage("mcr.microsoft.com/mssql/server")
               .WithEnvironment("MSSQL_SA_PASSWORD", "MyStrongPassword")
               .WithEnvironment("ACCEPT_EULA", "Y")
               .WithEnvironment("MSSQL_DATA_DIR", "/var/opt/sqlserver/data")
               .WithEnvironment("MSSQL_LOG_DIR", "/var/opt/sqlserver/log")
               .WithEnvironment("MSSQL_BACKUP_DIR", "/var/opt/sqlserver/backup")
               .WithPortBinding(49401, 1433) …
Run Code Online (Sandbox Code Playgroud)

c# asp.net integration-testing testcontainers .net-7.0

7
推荐指数
1
解决办法
7370
查看次数