标签: asp.net-core

AddTransient,AddScoped和AddSingleton服务的差异?

我想要实现dependency injectionAsp.Net Core.因此,在将此代码添加到ConfigureServices方法之后,两种方式都有效.

services.AddTransientservice.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)

c# .net-core asp.net-core

746
推荐指数
13
解决办法
28万
查看次数

Visual Studio 2017错误:无法启动程序,在当前状态下操作不合法

在全新安装Visual Studio 2017之后,我尝试运行.NET Core Web项目,当我尝试在Chrome上运行它时,我收到此错误:

无法启动程序,在当前状态下操作不合法

asp.net visual-studio asp.net-core visual-studio-2017

388
推荐指数
6
解决办法
14万
查看次数

如何在ASP.NET Core中创建自定义AuthorizeAttribute?

我正在尝试在ASP.NET Core中创建自定义授权属性.在以前的版本中,可以覆盖bool AuthorizeCore(HttpContextBase httpContext).但这不再存在于AuthorizeAttribute.

制作自定义AuthorizeAttribute的当前方法是什么?

我想要完成的任务:我在Header Authorization中收到一个会话ID.从该ID我将知道特定动作是否有效.

c# asp.net authorization asp.net-core-mvc asp.net-core

362
推荐指数
13
解决办法
19万
查看次数

ASP.NET Core 6+ 如何在启动过程中访问配置

在早期版本中,我们有 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)

c# asp.net-core .net-6.0 .net-7.0

255
推荐指数
7
解决办法
25万
查看次数

如何确定是否已安装.NET Core

我知道对于旧版本的.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机器上看不到相同的东西.

c# .net-core asp.net-core

249
推荐指数
10
解决办法
19万
查看次数

使用ASP.NET Core DI解析实例

如何使用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.

c# dependency-injection asp.net-core-mvc asp.net-core

245
推荐指数
6
解决办法
17万
查看次数

ASP.NET Core Web API异常处理

在使用常规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)

c# exception asp.net-core

230
推荐指数
8
解决办法
16万
查看次数

如何在ASP.NET Core中设置Automapper

我是.NET的新手,我决定解决.NET Core而不是学习"老方法".我在这里找到了一篇关于为.NET Core设置AutoMapper的详细文章,但新手有更简单的演练吗?

c# automapper asp.net-core

219
推荐指数
13
解决办法
13万
查看次数

如何从ASP.NET Core中的.json文件中读取AppSettings值

我在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中执行此操作?

.net c# .net-core asp.net-core

199
推荐指数
21
解决办法
24万
查看次数

如何在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方法keyname参数.

    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
我已经阅读了这里 …

c# asp.net-core-mvc coreclr asp.net-core

183
推荐指数
21
解决办法
9万
查看次数