拥有以下服务构造函数
public class Service : IService
{
public Service(IOtherService service1, IAnotherOne service2, string arg)
{
}
}
Run Code Online (Sandbox Code Playgroud)
使用.NET Core IOC机制传递参数有哪些选择?
_serviceCollection.AddSingleton<IOtherService , OtherService>();
_serviceCollection.AddSingleton<IAnotherOne , AnotherOne>();
_serviceCollection.AddSingleton<IService>(x=>new Service( _serviceCollection.BuildServiceProvider().GetService<IOtherService>(), _serviceCollection.BuildServiceProvider().GetService<IAnotherOne >(), "" ));
Run Code Online (Sandbox Code Playgroud)
还有其他方法吗?
c# dependency-injection ioc-container .net-core asp.net-core
我有一个策略想要在 C# 中测试
public class WorkflowCreatePolicy
{
public AuthorizationPolicy AuthorizationPolicy =>
new AuthorizationPolicyBuilder()
.RequireClaim("scope", "WorkflowAdmin")
.Build();
}
Run Code Online (Sandbox Code Playgroud)
有谁知道一种方法来测试 AuthorizationPolicy 来确认范围“WorkflowAdmin”成功而所有其他范围都失败?
这是我检查对象时看到的:
我设法找到这个网站:授权处理程序单元测试,但它谈论测试处理程序,并且具有将身份验证尝试标记为成功的代码。
我不确定这是否正在接近。目前还没有通过
[Test]
public void GivenPolicyName_WhenICallPolicyChecks_ThenItPasses()
{
ClaimsPrincipal user = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim> { new Claim(CustomClaims.Scope, "WorkflowAdmin") }));
WorkflowCreatePolicy workflowCreatePolicy = new WorkflowCreatePolicy();
AuthorizationHandlerContext authorizationHandlerContext = new AuthorizationHandlerContext(workflowCreatePolicy.AuthorizationPolicy.Requirements, user, null);
Assert.That(authorizationHandlerContext.HasSucceeded, Is.EqualTo(true));
}
Run Code Online (Sandbox Code Playgroud)