小编phi*_*-fx的帖子

如何通过其具体类型而不是其接口类型来获取注册服务?

请假设我有以下类和接口:

public class ConcreteRepository : IRepository<T>, IEntityOwner
{ }

public interface IRepository<T>
{ }

public interface IEntityOwner
{ }

public class SomeModel
{ }
Run Code Online (Sandbox Code Playgroud)

假设我已经在我的 Asp.Net Core 2.0 启动类中注册了以下服务:

services.AddScoped<IRepository<SomeModel>, ConcreteRepository>();
Run Code Online (Sandbox Code Playgroud)

我可以像这样获得 ConcreteRepository 的实例:

var concreteRepositoryInstance = 
         httpContext.RequestServices.GetService(typeof(IRepository<SomeModel>))
              as IRepository<SomeModel>;
Run Code Online (Sandbox Code Playgroud)

如何通过 ConcreteRepository 的类型获取 ConcreteRepository 的实例?

我想象这样的事情:

var concreteRepositoryInstance =  
         httpContext.RequestServices.GetService(typeof(ConcreteRepository))
              as IEntityOwner;
Run Code Online (Sandbox Code Playgroud)

请注意,我不能使用任何泛型来回答我的问题,因为我的用例引用了一个自定义属性,该属性将creteRepository 的类型作为输入参数(并且属性在设计上不能是泛型)。

到目前为止我尝试过的:

(a) 我检查并尝试了所有httpContext.RequestServices.GetService...方法,但它们都需要注册具体类型的接口类型。

(b) 我正在考虑从依赖容器获取所有服务,但 IServiceProvider 不提供获取所有服务。我只能通过接口类型来获取它们。

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

5
推荐指数
1
解决办法
3069
查看次数

ASP.Net Core 2.0-如何从中间件返回自定义json或xml响应?

在ASP.Net Core 2.0中,我试图返回带有状态代码的格式化为json或xml的消息。我从控制器返回自定义消息没有问题,但是我不知道如何在中间件中处理它。

到目前为止,我的中间件类如下所示:

public class HeaderValidation
{
    private readonly RequestDelegate _next;
    public HeaderValidation(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext httpContext)
    {
        // How to return a json or xml formatted custom message with a http status code?

        await _next.Invoke(httpContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

request-pipeline asp.net-core asp.net-core-middleware asp.net-core-2.0

3
推荐指数
1
解决办法
4464
查看次数

ASP.Net Core 2.0如何在中间件中获取所有请求标头?

在ASP.Net Core 2.0中,我试图在自定义中间件中验证传入的请求标头。

问题是我不怎么提取所有键值对标头。我需要的标头存储在受保护的属性中

protected Dictionary<string, stringValues> MaybeUnknown
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的中间件类如下所示:

public class HeaderValidation
{
    private readonly RequestDelegate _next;
    public HeaderValidation(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext httpContext)
    {
        IHeaderDictionary headers = httpContext.Request.Headers; // at runtime headers are of type FrameRequestHeaders

        // How to get the key-value-pair headers?
        // "protected Dictionary<string, stringValues> MaybeUnknown" from headers is inaccessbile due to its protection level
        // Casting headers as Dictionary<string, StringValues> results in null

        await _next.Invoke(httpContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的目标是提取所有请求标头,而不仅仅是我必须知道特定键的几个选定标头。

c# request-pipeline asp.net-core asp.net-core-middleware asp.net-core-2.0

3
推荐指数
1
解决办法
9692
查看次数

如何预热托管在 IIS 中的 asp.net 核心应用程序(a)在网站重启和(b)重新部署时?

我在 IIS 中托管了一个 asp.net core 2.0 应用程序。该应用程序仅在至少一个网络请求被发送到网站时启动。但是,我需要在应用程序 (a) 在 IIS 中重新启动和 (b) 在重新部署(发布到文件夹)时启动该应用程序。通过“开始”我的意思Startup是构造了类并调用了它的 ConfigureServices(...) 和 Configure(...) 方法,因为我已经配置了一些需要在 Web 主机启动后立即实例化的单例服务开始而不是在第一个 Web 请求进入时立即启动。

这是我的应用程序的入口点:

public class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.File("log.txt", rollOnFileSizeLimit: true, fileSizeLimitBytes: 10485760, retainedFileCountLimit: 10) // 10485760 Bytes = 10Megabytes
            .CreateLogger();

        try
        {
            Log.Information("Starting web host");
            BuildWebHost(args).Run();
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Host terminated unexpectedly");
        }
        finally
        {
            Log.CloseAndFlush();
        }

    }

    public static IWebHost BuildWebHost(string[] …
Run Code Online (Sandbox Code Playgroud)

c# iis asp.net-core

3
推荐指数
1
解决办法
2419
查看次数