小编Kev*_*Bui的帖子

使用 ASP.NET Core MVC 2.0 在中间件中获取 UrlHelper

如何在中间件中获取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

6
推荐指数
1
解决办法
2986
查看次数

如何在wrappanel中设置两个对齐方式

我怎样才能得到如下图所示的包装板?两个按钮 < > 和文本块向左对齐,文本框向右对齐,当我调整窗口宽度时,文本框自动换行。

c# wpf layout xaml wrappanel

5
推荐指数
1
解决办法
4196
查看次数

async/await 创建新线程,输出窗口显示

我读过很多文章说 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 是否真的创建了新线程。

有什么解释吗?

c# asynchronous threadpool

5
推荐指数
1
解决办法
1330
查看次数

埃拉托斯特尼筛改进后运行速度变慢

我尝试通过避免删除重复的素数倍数来改进基本的埃拉托斯特尼筛算法,但结果比我的预期更糟糕

我已经实现了两种返回范围 [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# algorithm primes sieve-of-eratosthenes

3
推荐指数
1
解决办法
129
查看次数