サーバーにリクエストを送信しようとしています。POSTリクエストはMiddlewareのInvokeメソッドに入ります。
ただし、Contentオブジェクトのタイプに関係なく、常に null です。
送信者
public async Task<string> RunTestAsync(string request)
{
try
{
var content = new StringContent(JsonConvert.SerializeObject(request),Encoding.UTF8,"application/json");
var response=await this.client.PostAsync("http://localhost:8500/mat",
content);
string str=await response.Content.ReadAsStringAsync();
stringdata = JsonConvert.DeserializeObject<string>(str);
return data;
}
catch (Exception ex)
{
Console.WriteLine("Threw in client" + ex.Message);
throw;
}
}
サーバ
サーバーには がservice定義されておらずmiddleware、route. (リクエストはInvokeメソッドに入ります!)
起動
public class Startup
{
public void ConfigureServices(IServiceCollection services) {
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.UseDeveloperExceptionPage();
app.UseBlazor<Client.Startup>();
app.Map("/mid", a => {
a.UseMiddleware<Mware>();
});
});
}
}
ミドルウェア
public class Mware
{
public RequestDelegate next{get;set;}
public Mware(RequestDelegate del)
{
this.next=del;
}
public async Task Invoke(HttpContext context)
{
using (var sr = new StreamReader(context.Request.Body))
{
string content = await sr.ReadToEndAsync();//null ,tried other types too , still null ,and the ContentLength is null too
var request=JsonConvert.DeserializeObject<string>(content);
if (request == null)
{
return;
}
}
}
}
シリアル化を確認したところ、オブジェクトは正常にシリアル化されています。
それにもかかわらず、私は常にnull反対側にいます。
PS
私はまたmiddleware、以下のような単純なデリゲートを使用してみました:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.UseDeveloperExceptionPage();
app.UseBlazor<Client.Startup>();
app.Map("/mid",x=>{
x.Use(async(context,del)=>{
using (var sr = new StreamReader(context.Request.Body))
{
string content = await sr.ReadToEndAsync();//null ,tried other types too , still null ,and the ContentLength is null too
var request=JsonConvert.DeserializeObject<string>(content);
if (request == null)
{
return;
}
}
});
}
専用がなくてもmiddleware問題は解決しません。
問題は ではなく、middlewareクライアントで正しくシリアル化され、サーバーに送信されたリクエストであり、何らかの形でbodyとして表示されnullます。オブジェクトに
失敗した場合は理解できましたが、文字列として受信され、それは!!deserializeHttpContext.Request.Bodynulllengthnull