小编Fab*_*NET的帖子

在线创建和使用一次性物体是否安全?

我已经看到了使用一次性对象内联,所以很多时候开发商这里的实例.内联我的意思是:

var result = new DataTable().Compute("1 + 4 * 7", null);
Run Code Online (Sandbox Code Playgroud)

我知道该Dispose方法不会被调用,但由于没有对该对象进行引用,垃圾收集器将如何处理它?使用像这样的一次性物体是否安全?

DataTable在我的例子中使用了一个,因为它是我找到的唯一具体例子,但我的问题一般适用于一次性物品.我不是那样亲自使用它们,我只是想知道如果它们以这种方式使用它们是否由GC处理它们.

c#

17
推荐指数
1
解决办法
542
查看次数

枚举Dictionary.Values vs Dictionary本身

我正在GitHub上探索ASP.NET核心的来源,看看ASP.NET团队用来加速框架的那种技巧.我看到一些引起我兴趣的东西.在ServiceProvider的源代码中,在Dispose实现中,它们枚举一个字典,并且它们发出注释以指示性能技巧:

private readonly Dictionary<IService, object> _resolvedServices = new Dictionary<IService, object>();

// Code removed for brevity

public void Dispose()    
{        
    // Code removed for brevity

    // PERF: We've enumerating the dictionary so that we don't allocate to enumerate.
    // .Values allocates a KeyCollection on the heap, enumerating the dictionary allocates
    // a struct enumerator
    foreach (var entry in _resolvedServices)
    {
        (entry.Value as IDisposable)?.Dispose();
    }

    _resolvedServices.Clear();        
}
Run Code Online (Sandbox Code Playgroud)

如果字典是这样列举的,有什么区别?

foreach (var entry in _resolvedServices.Values)
{
    (entry as IDisposable)?.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

它有性能影响吗?或者是因为分配ValueCollection …

.net c# dictionary .net-core asp.net-core

14
推荐指数
1
解决办法
584
查看次数

如何使用RewriteMiddleware将www重定向到AspNetCore 1.1预览1中的非www规则?

使用AspNetCore 1.1位和新的RewriteMiddleware我写了这样的东西Startup.cs来处理www到非www重定向:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var options = new RewriteOptions()
        .AddRedirect("(www\\.)(.*)", "$1");

    app.UseRewriter(options);

    // Code removed for brevty
}
Run Code Online (Sandbox Code Playgroud)

由于RedirectRule仅适用于路径而不是整个请求uri,因此正则表达式不匹配.

如何使用相同的方法将www重定向到非www规则?我不想使用IISUrlRewriteRule.

c# .net-core asp.net-core

9
推荐指数
1
解决办法
1261
查看次数

如何在ASP.NET Core 1.0中配置身份验证

我正在尝试向我的应用程序添加身份验证,我有实体框架运行但现在我想验证用户,但我遇到很多问题在配置构造函数中配置它.

例如,在许多教程中,他们提供的代码不再像我那样工作

    // Configure ASP.NET Identity to use our Identity-based application context
    services.AddAuthentication()
        .AddIdentity()
        .AddEntityFrameworkStores()
        .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

它告诉我,我需要明确指定类型参数,但这是教程中的内容吗?

https://shellmonger.com/2015/05/29/asp-net-mvc5-identity-part-1-the-database/

我很难理解这样做的正确方法,我想要做的就是在用户登录时对用户进行身份验证.

这是我的project.json

  "dependencies": {
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
    "Microsoft.AspNet.Identity": "3.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.Authentication": "1.0.0-rc1-final",
    "Microsoft.AspNet.Authorization": "1.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta5",
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
    "Microsoft.Framework.Logging": "1.0.0-beta7",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta8",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final"
  },
Run Code Online (Sandbox Code Playgroud)

和我的配置:

public class Startup
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup()
    {

        var builder = new …
Run Code Online (Sandbox Code Playgroud)

c# authentication entity-framework asp.net-core

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