我想要实现dependency injection在Asp.Net Core.因此,在将此代码添加到ConfigureServices方法之后,两种方式都有效.
services.AddTransient和service.AddScoped方法有Asp.Net Core什么区别?
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddScoped<IEmailSender, AuthMessageSender>();
}
Run Code Online (Sandbox Code Playgroud) 在全新安装Visual Studio 2017之后,我尝试运行.NET Core Web项目,当我尝试在Chrome上运行它时,我收到此错误:
无法启动程序,在当前状态下操作不合法
我正在尝试在ASP.NET Core中创建自定义授权属性.在以前的版本中,可以覆盖bool AuthorizeCore(HttpContextBase httpContext).但这不再存在于AuthorizeAttribute.
制作自定义AuthorizeAttribute的当前方法是什么?
我想要完成的任务:我在Header Authorization中收到一个会话ID.从该ID我将知道特定动作是否有效.
在早期版本中,我们有 Startup.cs 类,并在Startup文件中获取配置对象,如下所示。
public class Startup
{
private readonly IHostEnvironment environment;
private readonly IConfiguration config;
public Startup(IConfiguration configuration, IHostEnvironment environment)
{
this.config = configuration;
this.environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
// Add Services
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Add Middlewares
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在 .NET 6 及更高版本(使用 Visual Studio 2022)中,我们看不到Startup.cs类。看来它的日子屈指可数了。那么我们如何获取配置(IConfiguration)和托管环境(IHostEnvironment)等对象
我们如何获取这些对象,也就是说从 appsettings 读取配置?目前,Program.cs 文件如下所示。
using Festify.Database;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDbContext<FestifyContext>();
//////////////////////////////////////////////// …Run Code Online (Sandbox Code Playgroud) 我知道对于旧版本的.NET,您可以通过以下方式确定是否安装了给定版本
https://support.microsoft.com/en-us/kb/318785
Run Code Online (Sandbox Code Playgroud)
是否有官方方法确定是否安装了.NET Core?
(我不是指SDK,我想检查没有SDK的服务器,以确定它是否安装了DotNetCore.1.0.0-WindowsHosting.exe)
我可以看到
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NET Cross-Platform Runtime Environment\.NET Framework 4.6\Win\v1-rc1
Run Code Online (Sandbox Code Playgroud)
在我的Windows 7机器上版本号为1.0.11123.0,但我在Windows 10机器上看不到相同的东西.
如何使用ASP.NET Core MVC内置依赖注入框架手动解析类型?
设置容器很容易:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddTransient<ISomeService, SomeConcreteService>();
}
Run Code Online (Sandbox Code Playgroud)
但是如何在ISomeService不进行注射的情况下解决?例如,我想这样做:
ISomeService service = services.Resolve<ISomeService>();
Run Code Online (Sandbox Code Playgroud)
没有这样的方法IServiceCollection.
在使用常规ASP.NET Web API多年后,我开始使用ASP.NET Core作为我的新REST API项目.我没有看到在ASP.NET Core Web API中处理异常的好方法.我试图实现异常处理过滤器/属性:
public class ErrorHandlingFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
HandleExceptionAsync(context);
context.ExceptionHandled = true;
}
private static void HandleExceptionAsync(ExceptionContext context)
{
var exception = context.Exception;
if (exception is MyNotFoundException)
SetExceptionResult(context, exception, HttpStatusCode.NotFound);
else if (exception is MyUnauthorizedException)
SetExceptionResult(context, exception, HttpStatusCode.Unauthorized);
else if (exception is MyException)
SetExceptionResult(context, exception, HttpStatusCode.BadRequest);
else
SetExceptionResult(context, exception, HttpStatusCode.InternalServerError);
}
private static void SetExceptionResult(
ExceptionContext context,
Exception exception,
HttpStatusCode code)
{
context.Result = new JsonResult(new ApiResponse(exception))
{ …Run Code Online (Sandbox Code Playgroud) 我是.NET的新手,我决定解决.NET Core而不是学习"老方法".我在这里找到了一篇关于为.NET Core设置AutoMapper的详细文章,但新手有更简单的演练吗?
我在appsettings/Config .json中设置了我的AppSettings数据,如下所示:
{
"AppSettings": {
"token": "1234"
}
}
Run Code Online (Sandbox Code Playgroud)
我在网上搜索了如何从.json文件中读取AppSettings值,但我无法获得任何有用的信息.
我试过了:
var configuration = new Configuration();
var appSettings = configuration.Get("AppSettings"); // null
var token = configuration.Get("token"); // null
Run Code Online (Sandbox Code Playgroud)
我知道在ASP.NET 4.0中你可以这样做:
System.Configuration.ConfigurationManager.AppSettings["token"];
Run Code Online (Sandbox Code Playgroud)
但是我如何在ASP.NET Core中执行此操作?
我有来自相同接口的服务
public interface IService { }
public class ServiceA : IService { }
public class ServiceB : IService { }
public class ServiceC : IService { }
Run Code Online (Sandbox Code Playgroud)
通常,其他IOC容器Unity允许您通过某些Key区分它们来注册具体实现.
在Asp.Net Core中,我如何注册这些服务并在运行时根据某些键解决它?
我没有看到任何通常用于区分具体实现的AddService方法key或name参数.
public void ConfigureServices(IServiceCollection services)
{
// How do I register services here of the same interface
}
public MyController:Controller
{
public void DoSomeThing(string key)
{
// How do get service based on key
}
}
Run Code Online (Sandbox Code Playgroud)
工厂模式是唯一的选择吗?
Update1
我已经阅读了这里 …
asp.net-core ×10
c# ×9
.net-core ×3
asp.net ×2
.net ×1
.net-6.0 ×1
.net-7.0 ×1
automapper ×1
coreclr ×1
exception ×1