[英]how to change property value of scoped service at configure method of startup.cs file in asp.net core
我將在 startup.cs 文件中為我的范圍服務更改或插入一些值。
這是我的代碼。
我正在為 MyService.SomeData 屬性插入一個值。 但是,在查看頁面中,打印空值。
這是為什么?
啟動文件
namespace MyProject
{
public class Startup
{
.
.
.
public void ConfigureServices(IServiceCollection services)
{
.
.
.
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<MyService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var scope = app.ApplicationServices.CreateScope();
var MyService = scope.ServiceProvider.GetService<MyService>();
app.Use(async (context, next) =>
{
if (context.Request.Query.ContainKey("conditionKey") && context.Request.Query["conditonKey"] == "something")
{
MyService.SomeData = "foo";
}
});
}
}
}
我的服務.cs
namespace MyProject.Service
{
public class MyService
{
public string SomeData { get; set; } = "";
{
}
我的視圖.cshtml
@inject MyProject.Service.MyService MyService
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div>@MyService.SomeData</div>
</body>
ASP.NET Core 管道為每個 http 請求創建作用域,因此您需要在操作方法中設置此屬性
public class HomeController : Controller
{
private readonly MyService myService;
public HomeController(MyService myService)
{
this.myService = myService;
}
public IActionResult Index()
{
myService.MyData = "MyData";
return View();
}
}
或者將您的服務注冊為單身人士
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<MyService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var scope = app.ApplicationServices.CreateScope();
var MyService = scope.ServiceProvider.GetService<MyService>();
MyService.MyData = "SomeData";
}
或注冊自定義過濾器並將其集成到管道中
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews(x => x.Filters.Add(typeof(MyServiceFilter)));
services.AddScoped<MyService>();
}
public class MyServiceFilter : IActionFilter
{
private readonly MyService myService;
public MyServiceFilter(MyService myService)
{
this.myService = myService;
}
public void OnActionExecuted(ActionExecutedContext context)
{
}
public void OnActionExecuting(ActionExecutingContext context)
{
myService.MyData = "MyData";
}
}
最后,使用app.Use
app.Use(async (context, next) =>
{
if (context.Request.Query.ContainsKey("conditionKey") && context.Request.Query["conditionKey"] == "something")
{
var myService = context.RequestServices.GetService<MyService>();
myService.MyData = "foo";
}
await next();
});
感謝@YegorAndrosov,我解決了它。 在 app.Use() 中間件中創建請求范圍的服務。 並且,通過 context.RequestServices.GetService(); 創建它
namespace MyProject
{
public class Startup
{
.
.
.
public void ConfigureServices(IServiceCollection services)
{
.
.
.
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<MyService>();
}
.
.
.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
.
.
.
app.Use(async (context, next) =>
{
var RequestData = context.RequestServices.GetService<RequestData>();
var HompageSetting = context.RequestServices.GetService<HomepageSetting>();
if (context.Request.Query.ContainKey("conditionKey") && context.Request.Query["conditonKey"] == "something")
{
MyService.SomeData = "foo";
}
await next();
});
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.