小编pla*_*sjh的帖子

在多个项目中使用Asp.Net Core 2注入Serilog

我为Asp.Net Core 2.0配置了Serilog,它在我的启动Web项目中通过.Net Core依赖注入工作得很好(如果我通过Microsoft.Extensions.Logging使用它),但我无法从任何其他项目访问它.

这就是我所拥有的:

Program.cs中

using System;
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Serilog;

namespace ObApp.Web.Mvc
{
    public class Program
    {
        public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
            .Build();

        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(Configuration)
                .CreateLogger();

            try
            {
                Log.Warning("Starting BuildWebHost");

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

        public …
Run Code Online (Sandbox Code Playgroud)

c# asp.net logging serilog asp.net-core-mvc

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

DDD:从域项目引用MediatR接口

我刚刚开始使用DDD.我将域事件放入CQRS应用程序中,我遇到了一项基本任务:如何在域项目中使用MediatR.INotification标记接口,而不会在基础结构上创建域依赖项.

我的解决方案分为以下四个项目:

MyApp.Domain
    - Domain events
    - Aggregates
    - Interfaces (IRepository, etc), etc.
MyApp.ApplicationServices
    - Commands
    - Command Handlers, etc.
MyApp.Infrastructure
    - Repository
    - Emailer, etc.
MyApp.Web
    - Startup
    - MediatR NuGet packages and DI here
    - UI, etc.
Run Code Online (Sandbox Code Playgroud)

我目前在UI项目中安装了MediatR和MediatR .net Core DI软件包,并使用.AddMediatR()将它们添加到DI中

services.AddMediatR(typeof(MyApp.AppServices.Commands.Command).Assembly);
Run Code Online (Sandbox Code Playgroud)

它从AppServices项目中扫描并注册命令处理程序.

当我想要定义一个事件时,就会出现问题.要使MediatR使用我的域事件,需要使用MediatR.INotification接口进行标记.

namespace ObApp.Domain.Events
{
    public class NewUserAdded : INotification
    {
        ...
    }
Run Code Online (Sandbox Code Playgroud)

在这种情况下标记我的事件的正确方法是什么,以便MediatR可以使用它们?我可以为事件创建自己的标记界面,但MediatR将无法识别那些没有某种方法自动将它们转换为MediatR.INotification的标记.

这只是使用多个项目的缺点吗?即使我使用的是单个项目,如果我在域部分中使用MediatR.INotification,我会在域中放置一个"外部"接口.

当我的用户实体从EF的IdentityUser继承时,我遇到了同样的问题.在这种情况下,网络共识似乎是务实的,并继续允许轻微的污染,以节省许多麻烦.这是另一个类似的案例吗?我不介意为了实用主义而牺牲纯洁,而不仅仅是为了懒惰.

这是我使用的其他软件包会出现的一个基本问题,所以我期待着解决这个问题.

谢谢!

.net c# domain-driven-design mediatr

8
推荐指数
2
解决办法
2489
查看次数

嵌入在另一个标签助手代码中的标签助手不呈现

我正在尝试使用 .net Core 2.0 中的以下自定义标签助手来简化长表单的创建:

@model ObApp.Web.Models.ViewComponents.FormFieldViewModel

<span>Debug - Paramater value: @Model.FieldFor</span>
<div class="form-group">
    <label asp-for="@Model.FieldFor"></label>
    <input asp-for="@Model.FieldFor" class="form-control" />
</div>
Run Code Online (Sandbox Code Playgroud)

这看起来很简单,但是当我使用时,我得到了一个意想不到的(对我来说)结果:

<vc:form-field field-for="PersonEmail"></vc:form-field>
Run Code Online (Sandbox Code Playgroud)

预期结果

<span>Debug - Paramater value: PersonEmail</span>
<div class="form-group">
    <label for="PersonEmail">Email</label>
    <input name="PersonEmail" class="form-control" id="PersonEmail" 
        type="text" value="PersonEmail">
</div>
Run Code Online (Sandbox Code Playgroud)

实际结果

<span>Debug - Paramater value: PersonEmail</span>
<div class="form-group">
    <label for="FieldFor">FieldFor</label>
    <input name="FieldFor" class="form-control" id="FieldFor" 
        type="text" value="PersonEmail">
</div>
Run Code Online (Sandbox Code Playgroud)

我尝试从 @Model.FieldFor 周围删除引号,以及其他一些语法更改。

有什么建议?

谢谢!

c# razor asp.net-core asp.net-core-tag-helpers

7
推荐指数
1
解决办法
2434
查看次数

仅针对特定接口类型(命令)执行 MediatR 预处理器

[注意:这是一个“替换”问题。第一个是基于我的主项目的代码,因此我使用来自单一用途项目的代码重新提出了这个问题,该项目更清楚地说明了原理。问题仍然相同,只是表述得更好。]

场景

我正在尝试使用 MediatR 管道行为和 Autofac 进行请求路由,在 CQRS 请求管道上设置命令预处理器。我的目标是预处理器仅针对命令 (ICommand<>) 运行,而不是针对所有请求 (IRequest<>) 运行,这将导致预处理器针对命令、查询和事件执行。

问题

我可以让我的 GenericPreProcessor 或任何其他预处理器对所有类型的请求都能正常运行,但是我用来尝试“过滤”注入的任何方法要么返回错误,要么根本不执行所需的预处理器。

我在 Autofac 中处理所有请求的管道配置如下所示:

// Pipeline pre/post processors
builder
    .RegisterGeneric(typeof(RequestPostProcessorBehavior<,>))
    .As(typeof(IPipelineBehavior<,>));

builder
    .RegisterGeneric(typeof(RequestPreProcessorBehavior<,>))
    .As(typeof(IPipelineBehavior<,>));

// Works as desired: Fires generic pre-processor for ALL requests, both cmd and query
builder
    .RegisterGeneric(typeof(GenericRequestPreProcessor<>))
    .As(typeof(IRequestPreProcessor<>));

// Works for all requests, but I need a way to limit it to commands
builder
    .RegisterGeneric(typeof(MyCommandPreProcessor<>))
    .As(typeof(IRequestPreProcessor<>));
Run Code Online (Sandbox Code Playgroud)

从概念上讲,我正在尝试做类似的事情,但失败了:

builder
    .RegisterGeneric(typeof(MyCommandPreProcessor<>)) // Note generic
    .As(typeof(IRequestPreProcessor<ICommand<>>));
    // Intellisense error "Unexpected use of …
Run Code Online (Sandbox Code Playgroud)

c# generics autofac cqrs mediatr

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