私はServiceStackに慣れ始めたばかりで、FluentValidationに出くわしました。紹介に従い、小さなHelloアプリを作成しました。
私の問題は、リクエストDTOを検証しようとすると、検証に失敗した方法を説明するエラーメッセージ{}
が返されず、空白のJsonオブジェクトのみが返されることです。
私自身、検証はDTOに自動配線されているので、余分なコードを記述する必要はないと思います。
答えはおそらく露骨ですが、私にはわかりません。どんな助けでも大歓迎です。私のコードは以下の通りです:
namespace SampleHello2
{
[Route("/hello")]
[Route("/hello/{Name}")]
public class Hello
{
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
public class HelloService : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
public class HelloValidator : AbstractValidator<Hello>
{
public HelloValidator()
{
//Validation rules for all requests
RuleFor(r => r.Name).NotNull().NotEmpty().Equal("Ian").WithErrorCode("ShouldNotBeEmpty");
RuleFor(r => r.Name.Length).GreaterThan(2);
}
}
public class Global : System.Web.HttpApplication
{
public class HelloAppHost : AppHostBase
{
//Tell Service Stack the name of your application and where to find your web services
public HelloAppHost() : base("Hello Web Services", typeof(HelloService).Assembly) { }
public override void Configure(Funq.Container container)
{
//Enable the validation feature
Plugins.Add(new ValidationFeature());
container.RegisterValidators(typeof(HelloValidator).Assembly);
//register any dependencies your services use, e.g:
// container.Register<ICacheClient>(new MemoryCacheClient());
}
}
//Initialize your application singleton
protected void Application_Start(object sender, EventArgs e)
{
new HelloAppHost().Init();
}
}
}
PS ServiceStackの使用を本当に楽しんでいます。本当に素晴らしいプロジェクトなので、ありがとうございます。
編集
したがって、たとえば:
呼び出し:をhttp://localhost:60063/hello/Ian?format=json
返します{"Result":"Hello, Ian"}
。一方、Calling:は。をhttp://localhost:60063/hello/I?format=json
返します{}
。
2番目の呼び出しは{}
、自動生成されたエラーメッセージを期待していた場所に戻ります。