如何在中间件中获取UrlHelper。我尝试如下但actionContextAccessor.ActionContext返回 null。
public void ConfigureServices(IServiceCollection services)
{
services.AddSession();
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.AddMvc();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.Use(async (context, next) =>
{
var urlHelperFactory = context.RequestServices.GetService<IUrlHelperFactory>();
var actionContextAccessor = context.RequestServices.GetService<IActionContextAccessor>();
var urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
await next();
// Do logging or other work that …Run Code Online (Sandbox Code Playgroud) c# dependency-injection middleware asp.net-core-mvc asp.net-core-mvc-2.0
我怎样才能得到如下图所示的包装板?两个按钮 < > 和文本块向左对齐,文本框向右对齐,当我调整窗口宽度时,文本框自动换行。


我读过很多文章说 async/await 不会创建额外的线程。但 Visual Studio 调试模式下的输出和线程窗口的消息却相反。
我用一些代码创建了一个非常简单的示例窗口窗体;
private void button2_Click(object sender, EventArgs e)
{
Task t = methodAsync();
//t.Wait();
}
async Task methodAsync()
{
Console.WriteLine($"==before DownloadStringTaskAsync");
using (var wc = new System.Net.WebClient())
{
string content = await wc.DownloadStringTaskAsync("https://stackoverflow.com");
}
Console.WriteLine($"==after DownloadStringTaskAsync");
}
Run Code Online (Sandbox Code Playgroud)
我在调试模式下启动应用程序,通过单击调试工具栏上的暂停按钮来暂停它。线程窗口显示只有一个主线程,到目前为止这是正常的。
然后我单击按钮来执行methodAsync。当它完成 DownloadString 时,我再次暂停应用程序,然后我在线程窗口中看到几个附加线程。
大约 10 秒后,输出窗口显示消息"The thread xxx has exited with code 0 (0x0)"。
WebClient.DownloadStringTaskAsync当我替换为时,结果相同await Task.Delay(xxx)
,我想知道 async/await 是否真的创建了新线程。
有什么解释吗?
我尝试通过避免删除重复的素数倍数来改进基本的埃拉托斯特尼筛算法,但结果比我的预期更糟糕
我已经实现了两种返回范围 [2..max) 内的素数的方法
public static List<int> Sieve22Max_Basic(int n) {
var primes = new List<int>();
var sieve = new BitArray(n, true); // default all number are prime
//int crossTotal = 0;
int sqrt_n = (int)Math.Sqrt(n) + 1;
for (int p = 2; p < sqrt_n; ++p) {
if (sieve[p]) {
primes.Add(p);
//var cross = new List<int>();
int inc = p == 2 ? p : 2 * p;
for (int mul = p * p; mul < n; mul …Run Code Online (Sandbox Code Playgroud) c# ×4
algorithm ×1
asynchronous ×1
layout ×1
middleware ×1
primes ×1
threadpool ×1
wpf ×1
wrappanel ×1
xaml ×1