小编Esp*_*dbø的帖子

使用内存IdentityServer进行集成测试

我有一个使用IdentityServer4进行令牌验证的API.我想用内存中的TestServer对这个API进行单元测试.我想将IdentityServer托管在内存中的TestServer中.

我已经设法从IdentityServer创建一个令牌.

这是我到底有多远,但是我收到错误"无法从http:// localhost:54100/.well-known/openid-configuration获取配置 "

Api使用[授权] - 属性与不同的策略.这是我想测试的.

可以这样做,我做错了什么?我试图查看IdentityServer4的源代码,但没有遇到过类似的集成测试场景.

protected IntegrationTestBase()
{
    var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;

    _contentRoot = SolutionPathUtility.GetProjectPath(@"<my project path>", startupAssembly);
    Configure(_contentRoot);
    var orderApiServerBuilder = new WebHostBuilder()
        .UseContentRoot(_contentRoot)
        .ConfigureServices(InitializeServices)
        .UseStartup<Startup>();
    orderApiServerBuilder.Configure(ConfigureApp);
    OrderApiTestServer = new TestServer(orderApiServerBuilder);

    HttpClient = OrderApiTestServer.CreateClient();
}

private void InitializeServices(IServiceCollection services)
{
    var cert = new X509Certificate2(Path.Combine(_contentRoot, "idsvr3test.pfx"), "idsrv3test");
    services.AddIdentityServer(options =>
        {
            options.IssuerUri = "http://localhost:54100";
        })
        .AddInMemoryClients(Clients.Get())
        .AddInMemoryScopes(Scopes.Get())
        .AddInMemoryUsers(Users.Get())
        .SetSigningCredential(cert);

    services.AddAuthorization(options =>
    {
        options.AddPolicy(OrderApiConstants.StoreIdPolicyName, policy => policy.Requirements.Add(new StoreIdRequirement("storeId")));
    });
    services.AddSingleton<IPersistedGrantStore, InMemoryPersistedGrantStore>();
    services.AddSingleton(_orderManagerMock.Object);
    services.AddMvc();
}

private void …
Run Code Online (Sandbox Code Playgroud)

identityserver4

21
推荐指数
3
解决办法
6750
查看次数

确保异步方法可以更新非线程安全集合的最佳方法是什么?

我正在执行异步调用,最终将更新 GUI 中的集合。异步调用是通过这样的委托命令完成的:

StartDoingUsefulStuffCommand = new DelegateCommand(() => Task.Run(() => StartDoingUsefulStuff()));

public async Task StartDoingUsefulStuff()
{
    try
    {
        await some method
        do something else
        MyCollection.Clear();
        ...
        ...
    }
    catch (Exception e)
    {
       // handle exception
    }
}
Run Code Online (Sandbox Code Playgroud)

最终 StartDoingUsefulStuff 方法想要更新一个集合,导致这个异常,因为另一个线程试图更新一个集合:

PresentationFramework.dll 中发生了“System.NotSupportedException”类型的第一次机会异常

附加信息:这种类型的 CollectionView 不支持从不同于 Dispatcher 线程的线程更改其 SourceCollection。

我找到了一种解决方案:在当前 SynchronizationContext 中运行任务:

Task.Factory.StartNew(() => StartDoingUsefulStuff(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
Run Code Online (Sandbox Code Playgroud)

这会正常工作,但对我来说,每次从 XAML 调用命令时都执行所有这些操作似乎很乏味。您对我目前的解决方案有何看法/最佳实践是什么?

c# wpf async-await

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

标签 统计

async-await ×1

c# ×1

identityserver4 ×1

wpf ×1