希望使用.Net核心来创建一套微服务,我正在考虑为Startup类创建一个基类,该类将负责配置常用功能,例如日志记录,身份验证,端点健康检查,从而标准化我们的所有服务.
然而,我很惊讶这样的模式似乎没有被提及.是使用自定义中间件来实现常用功能的首选模式吗?任何有关这种困境的想法或经验都将受到赞赏.
有谁知道为什么在创建新的.NET完整框架项目时,Visual Studio(版本15.9.2)仍使用旧样式的.csproj文件?
为了解决这个问题,在创建新的完整框架项目时,我创建了一个.NET核心项目,然后只需将.csproj中的TargetFramework标记编辑为目标net472(例如),从而利用了新的简化结构。
有人看到这种方法有问题吗?
在 Windows 上运行的 ASP.NET 核心 (2.1) 中,我使用配置了以下身份验证方案的 HttpSys:
builder.UseHttpSys(options =>
{
options.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM;
options.Authentication.AllowAnonymous = true;
})
Run Code Online (Sandbox Code Playgroud)
然后在我的 Startup.Configure() 方法中,我试图访问调用 uri "/sensitiveOperation" 的客户端的用户凭据,如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
app.MapWhen(context => context.Request.Path.Equals("/sensitiveOperation") && context.Request.Method.Equals(HttpMethods.Put), subApp =>
{
subApp.Run(async (context) =>
{
if (context.User.Identity.Name == "admin")
{
await context.Response.WriteAsync("Performing sensitive operation.");
// .. Do Sensitive operation....
}
});
});
Run Code Online (Sandbox Code Playgroud)
这个例子有点粗俗,但重点是 context.User.Identity.Name 总是空的,我希望看到正在进行调用的 AD 帐户的名称。请注意,调用是在 powershell 中完成的,如下所示:
Invoke-WebRequest -Uri http://localhost:5555/sensitiveOperation -Method Put -UseDefaultCredentials
Run Code Online (Sandbox Code Playgroud)
我可以将此代码放在控制器中并使用 [Authorize] 属性来获取凭据,但我更愿意在点击 Mvc …
我有一个相当冗长的构造函数,它正在执行各种初始化工作,因此我想将一些工作分解为一些函数.这让我想知道是否应该制作上述函数实例或静态方法.我理解从构造函数调用虚函数的风险,但我也认为在一个没有100%实例化的对象上调用实例方法是不对的.当然,这是一个矛盾.
我对这个问题的人们的意见感兴趣.我还发现通过使用静态方法返回初始化变量,我可以使成员目标只读.这是我的场景的简化说明.
public class A
{
private readonly string _foo;
public A()
{
_foo = InitialiseFoo();
}
private static InitialiseFoo()
{
// Do stuff
return new string ("foo");
}
}
Run Code Online (Sandbox Code Playgroud)