c# - ServiceStack - 消息队列服务(验证和过滤)

标签 c# servicestack fluentvalidation

我是 ServiceStack 的新手。我使用的是 4.04 版。

我创建了两个程序,它们使用 Redis 队列相互通信。 一个是 Asp.Net 主机,另一个是 Windows 服务。

虽然基本的发送和接收消息运行良好,但我在 Windows 服务程序中为请求 DTO 配置验证时遇到了一些问题。

这些是我的请求、响应 DTO 和验证器:

public class RegisterCustomer : IReturn<RegisterCustomerResponse>
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string CompanyName { get; set; }
  public string EMailAddress { get; set; }
  public string Password { get; set; }
}

public class RegisterCustomerValidator : AbstractValidator<RegisterCustomer>
{
  public RegisterCustomerValidator()
  {
    RuleFor(m => m.FirstName).NotEmpty();
    RuleFor(m => m.LastName).NotEmpty();
    RuleFor(m => m.EMailAddress).NotEmpty().EmailAddress();
    RuleFor(m => m.Password).NotEmpty().Length(5, 100);
  }
}

public class RegisterCustomerResponse
{
  public ResponseStatus ResponseStatus { get; set; }
}

这是我配置验证的部分:

private void configureValidation(Container container)
{
  Plugins.Add(new ValidationFeature());
  // container.RegisterValidators(typeof(RegisterCustomerValidator).Assembly);
  container.Register<IValidator<RegisterCustomer>>(new RegisterCustomerValidator());
}

这是我的服务:

public class RegisterCustomerService : RavenService
{
  public RegisterCustomerService(IDocumentSession ravenSession)
    : base(ravenSession)
  {
  }

  public RegisterCustomerResponse Any(RegisterCustomer request)
  {
    Logger.Info("Start: Handel message '{0}' ({1})".Fmt(request.ToString(), request.GetType().Name));

    RegisterCustomerResponse result = new RegisterCustomerResponse();     

    Logger.Info("End: Handel message '{0}' ({1})".Fmt(request.ToString(), request.GetType().Name));
    return result;
  }
}
  1. 消息和验证器位于一个单独的程序集中。 通过以下方式在 IOC 自动注册验证器 container.RegisterValidators(typeof(RegisterCustomerValidator).Assembly); 不工作。在我通过以下方式注册验证器之后:

container.Register<IValidator<RegisterCustomer>>(new RegisterCustomerValidator());

我能够通过“TryResolve”解析服务中的验证器

  1. 我想只要我在 IOC 注册验证器,请求 DTO 就会得到验证。但看起来情况并非如此:调用方取回响应并且“ResponseStatus”属性为空。

  2. 我创建了 RequestFilter 来进行验证:

    公共(public)类 ValidationFilterAttribute : RequestFilterAttribute { public override void Execute(IRequest req, IResponse res, object requestDto) { 字符串虚拟=“HelloWorld”; }

并将其应用于请求dto:

[ValidationFilter]
public class RegisterCustomer : IReturn<RegisterCustomerResponse>
{
  ...
}

不调用“执行”方法。我在这里想念什么?

非常感谢任何帮助。

最佳答案

这是因为验证是作为全局 RequestFilter 执行的,而不是为 MQ Requests 执行的被认为是内部服务并跳过全局请求过滤器。

我们添加了新的 IAppHost.GlobalMessageRequestFiltersv4.05 中,ValidationFeature 现在也使用它来通过 MQ 验证请求。

注意:任何验证或服务错误都会发送到响应 DLQ(例如 QueueNames<RegisterCustomerResponse>.Dlq),而不是默认的 INQ(例如 QueueNames<RegisterCustomerResponse>.Inq),如本示例所示:

public class ValidateTestMq
{
    public int Id { get; set; }
}

public class ValidateTestMqResponse
{
    public int CorrelationId { get; set; }

    public ResponseStatus ResponseStatus { get; set; }
}

public class ValidateTestMqValidator : AbstractValidator<ValidateTestMq>
{
    public ValidateTestMqValidator()
    {
        RuleFor(x => x.Id)
            .GreaterThanOrEqualTo(0)
            .WithErrorCode("PositiveIntegersOnly");
    }
}

下面的测试先发布一个无效请求,然后再发布一个有效请求:

using (var mqFactory = appHost.TryResolve<IMessageFactory>())
{
    var request = new ValidateTestMq { Id = -10 };
    mqFactory.CreateMessageProducer().Publish(request);
    var msg = mqFactory.CreateMessageQueueClient()
        .Get(QueueNames<ValidateTestMqResponse>.Dlq, null)
        .ToMessage<ValidateTestMqResponse>();

    Assert.That(msg.GetBody().ResponseStatus.ErrorCode,
        Is.EqualTo("PositiveIntegersOnly"));

    request = new ValidateTestMq { Id = 10 };
    mqFactory.CreateMessageProducer().Publish(request);
    msg = mqFactory.CreateMessageQueueClient()
         .Get(QueueNames<ValidateTestMqResponse>.In, null)
        .ToMessage<ValidateTestMqResponse>();
    Assert.That(msg.GetBody().CorrelationId, Is.EqualTo(request.Id));
}

关于c# - ServiceStack - 消息队列服务(验证和过滤),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20546442/

相关文章:

asp.net-mvc - 使用 FluentValidation 时将 DataAnnotation 添加到类

c# - 为什么 C# 7 ValueTuples 实现了 Equals 方法而不是双等于运算符?

c# - 在 Winforms 窗体中滑动

c# - 隐式延迟加载与显式延迟加载

c# - Swagger 无法在自托管的 ServiceStack 服务上工作

使用 servicestack 搜索/报告

MassTransit 中间件 - 获取 IServiceProvider 的实例

c# - 失去网络连接时 ZMQ 发布-订阅程序失败

iis-7.5 - 带有 IIS 7.5 的 ServiceStack

c# - FluentValidation 中依赖规则的顺序