小编Spa*_*man的帖子

这些多余的花括号的目的是什么?

所以我最近开始在一个新的工作场所,我遇到了一种javascript格式,让我质疑它的目的.(特别是括号{})

var _occurrences = getOccurrences($('#ddlTours').val());
{
    var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID);
    {
        _occurrenceID = _occurrence.occurrenceID;
    }
}
Run Code Online (Sandbox Code Playgroud)

对我来说,它几乎看起来像一个尝试对象的构造.即

var _occurrences : // Ignoring = getOccurrences($('#ddlTours').val());
{
    _occurrence : // Ignoring getObjectByValue(_occurrences, 'tourID', booking.tourID);
    {
        _occurrenceID : _occurrence.occurrenceID;
    }
}
Run Code Online (Sandbox Code Playgroud)

但据我所知,它会执行它.

var _occurrences = getOccurrences($('#ddlTours').val());
var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID);
_occurrenceID = _occurrence.occurrenceID;
Run Code Online (Sandbox Code Playgroud)

或者它是如此_occurrence得到删除并且不会被封装,我们分配一个在封装之外的var.这实际上是否可以改善绩效?即

Global var a = 1
{
    b = someFunction()  // After execution because of encapsulation it poofs???
    for(var c in b)
    {
        a += c.somefunction() …
Run Code Online (Sandbox Code Playgroud)

javascript

46
推荐指数
3
解决办法
2836
查看次数

更改角色后刷新声明主要内容

我在改变dotnetcore身份中的角色时遇到了一些问题.

我有以下代码.

private async Task SetRoleToX(ClaimsPrincipal claimsPrincipal, string X)
{
    var currentUser = await UserManager.GetUserAsync(claimsPrincipal);
    var roles = await UserManager.GetRolesAsync(currentUser);

    await UserManager.RemoveFromRolesAsync(currentUser, roles);
    await UserManager.AddToRoleAsync(currentUser, X);
    await SignInManager.RefreshSignInAsync(currentUser);
}
Run Code Online (Sandbox Code Playgroud)

我无法更新ClaimsPrincipal.

我尝试过使用登录并注销.

如果我手动登录和退出,角色切换工作正常.

我一直在网上搜索,很多人说这应该工作:(

c# asp.net-identity .net-core asp.net-core-identity

11
推荐指数
1
解决办法
930
查看次数

使用异步和等待模式一步高效地将 IQueryable<TSource> 转换为 ImmutableArray<TSource>

我正在尝试以最有效的方式将集合选择不可变数组中。

我还想明确结果集的意图:只读/不可变/不跟踪

var someCollection = await _dataContext
    .Set<someType>()
    .AsNoTracking()
    .ToArrayAsync();
Run Code Online (Sandbox Code Playgroud)

我可以这样做,然后转换为不可变,但这看起来很疯狂。

var someImmutableCollection = someCollection
    .ToImmutableArray();
Run Code Online (Sandbox Code Playgroud)

我也可以这样做:

var someCollection = _dataContext
    .Set<someType>()
    .AsNoTracking()
    .ToImmutableArray();
Run Code Online (Sandbox Code Playgroud)

但这不是async

有没有理由ToImmutableArrayAsync()吗?

或者是否有更好的方法async从 efcore 获取不可变数组?

.net c# entity-framework entity-framework-core

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

Cors Html5 Jquery/SoundManager wierdness

所以我在使用soundcloud api和html 5音频播放器时遇到了一些问题.

主要目标是仅使用html5访问所有公开的soundcloud音乐.

我使用以下内容.

    //C# api call returning json object. Cut down version of the code as it isnt the issue.
    var method "http://api.soundcloud.com/users/{0}/favorites.json"
    var query = "?client_id=" + soundCloudClientId;

    WebClient client = new WebClient();

    return client.DownloadString(string.Format(method, artistId) + query);
Run Code Online (Sandbox Code Playgroud)

我从soundcloud回来了.对于一些相关的测试用例再次减少

    {
        "Songs": 
        [
            {
                "Title": "Rio",
                "Artist": "Netsky",
                "Album": "",
                "Duration": "03:49",
                "Artwork": "https://i1.sndcdn.com/artworks-000120037955-m8t78n-t500x500.jpg",
                "StreamUrl": "https://api.soundcloud.com/tracks/210032350/stream",
                "Libary": "SoundCloud",
                "TrackId": "210032350"
            },
            {
                "Title": "FreqAsia x Nasty Wizard - Episode 01",
                "Artist": "Frequency Asia",
                "Album": "",
                "Duration": "17:50",
                "Artwork": …
Run Code Online (Sandbox Code Playgroud)

jquery html5 soundmanager2 html5-audio soundcloud

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

Razor runco​​mpile不允许我调试

我正在使用最新版的剃须刀. https://github.com/Antaris/RazorEngine

我想将它附加到一些cshtml并调试它.

自述文件陈述如下

调试

您可能想要启用的一件事是调试功能:

config.Debug = true;
Run Code Online (Sandbox Code Playgroud)

当Debug为true时,您可以直接调试生成的代码.RazorEngine还支持直接调试模板文件(通常是.cshtml文件).正如您在上面的代码中看到的那样,没有要调试的文件.要为RazorEngine提供必要的信息,您需要告诉它可以找到文件的位置:

string template = "Hello @Model.Name, welcome to RazorEngine!";
string templateFile = "C:/mytemplate.cshtml"
var result = Engine.Razor.RunCompile(new LoadedTemplateSource(template, templateFile), "templateKey", null, new { 
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我设置了以下内容

var config = new TemplateServiceConfiguration();
// .. configure your instance
config.Debug = true;

var service = RazorEngineService.Create(config);

Engine.Razor = service;

//string template = "Hello @Model.Name, welcome to RazorEngine!";
string templateFile = "C:/mytemplate.cshtml";

var template = new LoadedTemplateSource("", templateFile);
var result = Engine.Razor.RunCompile(template, this.Name, null, model);
Run Code Online (Sandbox Code Playgroud)

现在我在该路径中创建了一个cshtml文件,其中包含以下内容.

@{ …
Run Code Online (Sandbox Code Playgroud)

c# razor razor-2 razorengine

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

有没有办法在 C# 中将插值字符串拆分为多行,同时在运行时执行相同的性能

我一直在与同事讨论格式化以下代码的最佳方法。

return $" this is a really long string.{a} this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string.{b} this is a really long …
Run Code Online (Sandbox Code Playgroud)

c# string string-interpolation

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

处理多个带有超时的CancellationToken

对于在以下情况下如何实现取消标记,我有些困惑。

说我有一个方法,它有一个取消令牌,没有指定超时,像这样。

public static async Task DoSomeAsyncThingAsync(CancellationToken cancellationToken = default)
{
    try
    {
        Task.Delay(1000, cancellationToken)
    }
    catch (OperationCanceledException canceledException)
    {
        // Do something with canceledException
        Console.WriteLine("DoSomeElseAsyncThingAsync {0}", canceledException);
        throw;
    }
    catch (Exception exception)
    {
        // Do something with exception
        Console.WriteLine("DoSomeElseAsyncThingAsync {0}", exception);
        throw;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在该方法中,我想调用另一个期望CancellationToken除此以外的方法,但这次我要对此设置超时。

public static async Task DoSomeAsyncThingAsync(CancellationToken cancellationToken = default)
{
    try
    {
        var innerCancellationTokenSource = new CancellationTokenSource();
        innerCancellationTokenSource.CancelAfter(1000);
        var innerCancellationToken = innerCancellationTokenSource.Token;

        await DoSomeElseAsyncThingAsync(innerCancellationToken);
    }
    catch (OperationCanceledException canceledException)
    {
        // Do something with …
Run Code Online (Sandbox Code Playgroud)

c# cancellation async-await cancellationtokensource cancellation-token

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