Blazor Server + WebAPI/EFCore + NSwag - BtServer.ApiException: Could not deserialize the response body stream as System.Collections.Generic.ICollection`1[[BtServer.Transactio[Answered]RSS
My solution has a Blazor Server and a WebAPI with NSwag project.
The API runs on port 44317 and displays the Swagger interface. A request through Swagger returns the expected result.
The Blazor Server runs on port 44391
<div>[2020-12-26T21:18:33.032Z] Information: Normalizing '_blazor' to 'https://localhost:44391/_blazor'.</div> <div>blazor.server.js:1 [2020-12-26T21:18:33.073Z] Information: WebSocket connected to wss://localhost:44391/_blazor?id=VXt7-sg0JrjdT-WShseaKA.</div>
<div>blazor.server.js:19 [2020-12-26T21:18:37.785Z] Error: HTTP Response: </div> <div></div> <div></div> <div></div> <div>BtServer.ApiException: Could not deserialize the response body stream as
System.Collections.Generic.ICollection`1[[BtServer.Transaction, BtServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].</div> <div></div> <div>Status: 200</div> <div>Response: </div> <div></div>
<div> ---> Newtonsoft.Json.JsonSerializationException: Required property 'client' expects a non-null value. Path '[0]', line 1, position 1698.</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EndProcessProperty(Object
newObject, JsonReader reader, JsonObjectContract contract, Int32 initialDepth, JsonProperty property, PropertyPresence presence, Boolean setDefaultValue)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object
newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty
member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract,
JsonProperty containerProperty, String id)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)</div>
<div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)</div> <div> at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,
Type objectType)</div> <div> at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)</div> <div> at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)</div> <div> at BtServer.swaggerClient.ReadObjectResponseAsync[T](HttpResponseMessage
response, IReadOnlyDictionary`2 headers) in C:\Users\Robert\source\Repos\BtServer\obj\swaggerClient.cs:line 226</div> <div> --- End of inner exception stack trace ---</div> <div> at BtServer.swaggerClient.ReadObjectResponseAsync[T](HttpResponseMessage
response, IReadOnlyDictionary`2 headers) in C:\Users\Robert\source\Repos\BtServer\obj\swaggerClient.cs:line 233</div> <div> at BtServer.swaggerClient.TransactionAsync(CancellationToken cancellationToken) in C:\Users\Robert\source\Repos\BtServer\obj\swaggerClient.cs:line
90</div> <div> at BtServer.Pages.Transactions.OnInitializedAsync() in C:\Users\Robert\source\Repos\BtServer\Pages\Transactions.razor:line 75</div> <div> at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()</div>
<div> at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)</div> <div>e.log @ blazor.server.js:19</div> <div>blazor.server.js:1 [2020-12-26T21:18:37.787Z] Information: Connection disconnected.</div>
<div>Transactions:1 </div> <div></div> <div>Found
this on SO. The OP describes my exact same problem, and there is a response (but no answer):</div> <div>I have problems with autogenerated NSwag code too. try changing JsonConvert.DeserializeObject<ICollection<LocationDto> to JsonConvert.DeserializeObject<List<LocationDto>></div>
<div></div> <div></div> <div>Any suggestions would be appreciated as usual.</div> <div></div> <div></div> <div></div>
My Razor page to display Transactions looks like this:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddHttpClient();
services.AddServerSideBlazor();
//services.AddScoped<>();
services.AddCors(o => o.AddPolicy("Policy", builder => {
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
Click on "Mark as Answer" if my answers help you along.
You need to troubleshoot. The first step is reading the error message.
BtServer.ApiException: Could not deserialize the response body stream... Newtonsoft.Json.JsonSerializationException: Required property 'client' expects a non-null value. Path '[0]', line 1, position 1698
Clearly the "client" property is null but the serializer expects a value. Review your code and figure out why "client" is null.
Secondly, you are using somehting named Swagger Client and the code makes no logical sense as shown. I expect an actual C# constructor, not an anonymous type.
Is swaggerClient JS interopt? If so why are making an HTTP request from the browser when you purposely built a Blaozr Server application. As explained in one of your other threads, make the HTTP request from the server not the browser. That's one of the
main reason to use Blazor Server. Why are you creating a second connection when you have a dedicated connection to the server?
If you want to make HTTP requests from the browser then built a Blazor WASM application.
Fantastic. I learned new syntax in .net 5. Still, the error message clearly and openly states "client" is null but expected not to be null. Why is client null? Use a tool like PostMan to to make requests to the API end point to see if there are any errors.
Perhaps build a simple test to see if the issue is with your design or making the HTTP request.
You are getting a deserialization error. the response does match the api specified by swagger or maybe it’s the error page. You can use a network sniffer, or add a middleware response logger.
Contributor
2590 Points
2660 Posts
Blazor Server + WebAPI/EFCore + NSwag - BtServer.ApiException: Could not deserialize the response...
Dec 26, 2020 09:32 PM|wavemaster|LINK
My solution has a Blazor Server and a WebAPI with NSwag project.
The API runs on port 44317 and displays the Swagger interface. A request through Swagger returns the expected result.
The Blazor Server runs on port 44391
<div>[2020-12-26T21:18:33.032Z] Information: Normalizing '_blazor' to 'https://localhost:44391/_blazor'.</div> <div>blazor.server.js:1 [2020-12-26T21:18:33.073Z] Information: WebSocket connected to wss://localhost:44391/_blazor?id=VXt7-sg0JrjdT-WShseaKA.</div> <div>blazor.server.js:19 [2020-12-26T21:18:37.785Z] Error: HTTP Response: </div> <div> </div> <div> </div> <div> </div> <div>BtServer.ApiException: Could not deserialize the response body stream as System.Collections.Generic.ICollection`1[[BtServer.Transaction, BtServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].</div> <div> </div> <div>Status: 200</div> <div>Response: </div> <div> </div> <div> ---> Newtonsoft.Json.JsonSerializationException: Required property 'client' expects a non-null value. Path '[0]', line 1, position 1698.</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EndProcessProperty(Object newObject, JsonReader reader, JsonObjectContract contract, Int32 initialDepth, JsonProperty property, PropertyPresence presence, Boolean setDefaultValue)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)</div> <div> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)</div> <div> at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)</div> <div> at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)</div> <div> at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)</div> <div> at BtServer.swaggerClient.ReadObjectResponseAsync[T](HttpResponseMessage response, IReadOnlyDictionary`2 headers) in C:\Users\Robert\source\Repos\BtServer\obj\swaggerClient.cs:line 226</div> <div> --- End of inner exception stack trace ---</div> <div> at BtServer.swaggerClient.ReadObjectResponseAsync[T](HttpResponseMessage response, IReadOnlyDictionary`2 headers) in C:\Users\Robert\source\Repos\BtServer\obj\swaggerClient.cs:line 233</div> <div> at BtServer.swaggerClient.TransactionAsync(CancellationToken cancellationToken) in C:\Users\Robert\source\Repos\BtServer\obj\swaggerClient.cs:line 90</div> <div> at BtServer.Pages.Transactions.OnInitializedAsync() in C:\Users\Robert\source\Repos\BtServer\Pages\Transactions.razor:line 75</div> <div> at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()</div> <div> at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)</div> <div>e.log @ blazor.server.js:19</div> <div>blazor.server.js:1 [2020-12-26T21:18:37.787Z] Information: Connection disconnected.</div> <div>Transactions:1 </div> <div></div> <div>Found this on SO. The OP describes my exact same problem, and there is a response (but no answer):</div> <div>I have problems with autogenerated NSwag code too. try changing JsonConvert.DeserializeObject<ICollection<LocationDto> to JsonConvert.DeserializeObject<List<LocationDto>></div> <div></div> <div></div> <div>Any suggestions would be appreciated as usual.</div> <div></div> <div></div> <div></div>My Razor page to display Transactions looks like this:
My startup class
All-Star
52091 Points
23211 Posts
Re: Blazor Server + WebAPI/EFCore + NSwag - BtServer.ApiException: Could not deserialize the resp...
Dec 27, 2020 01:53 PM|mgebhard|LINK
You need to troubleshoot. The first step is reading the error message.
BtServer.ApiException: Could not deserialize the response body stream... Newtonsoft.Json.JsonSerializationException: Required property 'client' expects a non-null value. Path '[0]', line 1, position 1698
Clearly the "client" property is null but the serializer expects a value. Review your code and figure out why "client" is null.
Secondly, you are using somehting named Swagger Client and the code makes no logical sense as shown. I expect an actual C# constructor, not an anonymous type.
Is swaggerClient JS interopt? If so why are making an HTTP request from the browser when you purposely built a Blaozr Server application. As explained in one of your other threads, make the HTTP request from the server not the browser. That's one of the main reason to use Blazor Server. Why are you creating a second connection when you have a dedicated connection to the server?
If you want to make HTTP requests from the browser then built a Blazor WASM application.
Contributor
2590 Points
2660 Posts
Re: Blazor Server + WebAPI/EFCore + NSwag - BtServer.ApiException: Could not deserialize the resp...
Dec 27, 2020 03:21 PM|wavemaster|LINK
NET5.0 has tighter integration with Swagger. A new Core WebAPI project, now comes with Swagger all set up.
https://www.youtube.com/watch?v=nY-w9wPFEuY 22 minutes in it shows how the swaggerclient is used.
All-Star
52091 Points
23211 Posts
Re: Blazor Server + WebAPI/EFCore + NSwag - BtServer.ApiException: Could not deserialize the resp...
Dec 27, 2020 03:46 PM|mgebhard|LINK
Fantastic. I learned new syntax in .net 5. Still, the error message clearly and openly states "client" is null but expected not to be null. Why is client null? Use a tool like PostMan to to make requests to the API end point to see if there are any errors. Perhaps build a simple test to see if the issue is with your design or making the HTTP request.
Contributor
2590 Points
2660 Posts
Re: Blazor Server + WebAPI/EFCore + NSwag - BtServer.ApiException: Could not deserialize the resp...
Dec 27, 2020 08:17 PM|wavemaster|LINK
Postman responds with 200 and shows the requested information.
All-Star
57844 Points
15487 Posts
Re: Blazor Server + WebAPI/EFCore + NSwag - BtServer.ApiException: Could not deserialize the resp...
Dec 27, 2020 08:36 PM|bruce (sqlwork.com)|LINK
You are getting a deserialization error. the response does match the api specified by swagger or maybe it’s the error page. You can use a network sniffer, or add a middleware response logger.
Contributor
2590 Points
2660 Posts
Re: Blazor Server + WebAPI/EFCore + NSwag - BtServer.ApiException: Could not deserialize the resp...
Dec 30, 2020 04:39 PM|wavemaster|LINK
I stopped using Swagger because I couldn't get it to work and reverted to this:
While messing around with this to get it to work I learned that I was missing public DbSet<Transaction> Transactions { get; set; } in AppDbContext.
Then it started working.
Since I have somehow messed up Swagger, and can no longer use it. Started using HttpRepl, which let's me test the api from a command line.