小编Cod*_*-47的帖子

IHeadersDictionary 在模拟后返回 null

我试图模拟 IHeadersDictionary,每当我尝试访问它时,我都会返回 Null。

public interface IRequestScopeContext
{
    IHeaderDictionary Headers { get; set; }
    ISessionInfo SessionInfo { get; set; }
    HttpContext HttpContextInfo { get; set; }
}

[SetUp]
public void Setup()
{
    var headers = new Dictionary<string, string>
    {
        { "Key", "Value"}
    } as IHeaderDictionary;

    var sessionInfo = new SessionInfo
    {
        AccountId = "AccountId",
        UserId = "UserId",
    };

    requestScopeContext = new Mock<IRequestScopeContext>();
    requestScopeContext.Setup(x => x.Headers).Returns(headers);
    requestScopeContext.Setup(x => x.SessionInfo).Returns(sessionInfo);

    serviceProvider = new Mock<IServiceProvider>();
    serviceProvider.Setup(sp => sp.GetService(It.Is<Type>((Type t) => t.Name.Equals("IRequestScopeContext")))).Returns(requestScopeContext.Object);

    httpContextAccessor = new …
Run Code Online (Sandbox Code Playgroud)

c# nunit moq asp.net-core

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

Parallel.Foreach() 没有结果

我正在尝试使用并行查询 mongo-db,Parallel.Foreach()但没有得到任何结果。但是当我尝试在常规 foreach 循环中运行相同的事情时,我能够执行预期的任务。

var exceptions = new ConcurrentQueue<Exception>();
var secondaryObjectsDictionaryCollection = new Dictionary<string, List<JObject>>();

// This works
foreach(var info in infos)
{
    try
    {
        name = await commonValidator.ValidateAsync(name);
        await commonValidator.ValidateIdAsync(name, id);
        var list = await helper.ListRelatedObjectsAsync(name, id, info, false);

        secondaryObjectsDictionaryCollection.Add(info.PrimaryId, secondaryObjectsList.ToList());
    }
    catch (Exception ex)
    {
        exceptions.Enqueue(ex);
    }
}

//This does not
Parallel.ForEach(infos, async info =>
{
    try
    {
        name = await commonValidator.ValidateAsync(name);
        await commonValidator.ValidateIdAsync(name, id);
        var list = await helper.ListRelatedObjectsAsync(name, id, info, false);

        secondaryObjectsDictionaryCollection.Add(info.PrimaryId, secondaryObjectsList.ToList());
    } …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous async-await parallel.foreach

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