SassKit.Multitenancy与AspNetCore 2.2不兼容吗?

时间:2019-04-22 16:53:51

标签: c# asp.net-core

我已经创建了一个.NET Core API控制器,并且在POST方法中,我尝试使用CreatedAtActionResult方法在响应中包括该路由作为位置标头。

[ApiController, Route("v1/[controller]")]
public class WidgetController: ControllerBase
{
    public WidgetController(IWidgetService service)
    {
        _service = service;
    }

    private readonly IWidgetService _service;

    [HttpGet("{id}", Name = "GetSingle")]
    public IActionResult GetSingle(Guid id)
    {
        var result = _service.Get(id);

        return Ok(result);
    }

    [HttpPost]
    public IActionResult Post(WidgetModel model)
    {
        var result = _service.Post(model);

        return result == Guid.Empty
                    ? (IActionResult) BadRequest("No changes saved.")
                    : CreatedAtAction(nameof(GetSingle),
                                      new {id = result},
                                      model);
    }
}

当我启动应用程序时,从POSTman进行的对POST的第一个调用运行没有问题。对象和位置标题URL均按预期生成。但是,如果在代码仍在运行时尝试再次访问该端点,则会收到以下错误:

  

System.InvalidOperationException:类型'Microsoft.AspNetCore.Routing.IEndpointAddressScheme 1[Microsoft.AspNetCore.Routing.RouteValuesAddress]' has been registered. at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.AspNetCore.Routing.DefaultLinkGenerator.GetEndpoints[TAddress](TAddress address) at Microsoft.AspNetCore.Routing.DefaultLinkGenerator.GetPathByAddress[TAddress](HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues, Nullable 1 pathBase,FragmentString片段,LinkOptions选项没有服务)      在Microsoft.AspNetCore.Routing.LinkGeneratorRouteValuesAddressExtensions.GetPathByRouteValues(LinkGenerator生成器,HttpContext httpContext,字符串routeName,对象值,可空1 pathBase, FragmentString fragment, LinkOptions options) at Microsoft.AspNetCore.Mvc.Routing.EndpointRoutingUrlHelper.Action(UrlActionContext urlActionContext) at Microsoft.AspNetCore.Mvc.UrlHelperExtensions.Action(IUrlHelper helper, String action, String controller, Object values, String protocol, String host, String fragment) at Microsoft.AspNetCore.Mvc.UrlHelperExtensions.Action(IUrlHelper helper, String action, String controller, Object values, String protocol, String host) at Microsoft.AspNetCore.Mvc.CreatedAtActionResult.OnFormatting(ActionContext context) at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsync(ActionContext context, ObjectResult result) at Microsoft.AspNetCore.Mvc.ObjectResult.ExecuteResultAsync(ActionContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) at SaasKit.Multitenancy.Internal.TenantResolutionMiddleware 1.Invoke(HttpContext上下文,ITenantResolver`1 tenantResolver)处      在Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext上下文)

我尝试将CreatedAtAction换成Ok,这200个结果将返回与单击POSTman中的按钮一样多的次数。

Post方法内的代码已成功处理,并且将移至我创建的Filter的OnActionExecuted方法上。我尝试查看ActionExecutedContext对象的所有属性,但看不到任何异常。一旦我退出了空的OnActionExecuted方法,该调用将在错误页面内返回500状态代码和上述堆栈跟踪。我有一个异常过滤器,但是没有被触及。

以前有人对此有疑问吗?

作为参考,这是我的启动配置:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMultitenancy<AppTenant, CachingAppTenantResolver>();

    services.AddAutoMapper()
            .AddSingleton(ConfigureAutoMapper())
            .AddMvc(options =>
            {
                options.Filters.Add<ValidatorActionFilter>();
                options.Filters.Add<ErrorHandlingFilter>();
            })
            .AddFluentValidation(validation => {
                validation.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
                validation.ImplicitlyValidateChildProperties = true;
                validation.RegisterValidatorsFromAssemblyContaining<WidgetModelValidator>();
                })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .ConfigureApiBehaviorOptions(options =>
            {
                options.SuppressModelStateInvalidFilter = true;
                options.SuppressMapClientErrors = true;
            });

    services.AddEntityFrameworkSqlServer()
            .AddDbContext<MyContext>();

    services.Configure<MultitenancyOptions>(configuration.GetSection("Multitenancy"));

    services.AddCors(options =>
    {
        options.AddDefaultPolicy(
            builder =>
            {
                builder.WithOrigins("http://localhost:4200").AllowAnyHeader().AllowAnyMethod();
            });
    });

    ConfigureContainer(container);

    return provider;
}

1 个答案:

答案 0 :(得分:1)

尝试设置CompatibilityVersion.Version_2_02.1而不是2.2吗?

在2.2的基础上,EndpointRouting发生了很大的变化,您的第三方插件可能不兼容。

将兼容性设置回2.1,通常不需要在控制器中进行任何代码更改,因此成本很低。