'Why does this service return null when return value is not null?
Not sure why this is happening.
In the SaveRole() method when I try to hover over and see the value when debugging this line always shows null on the apiResponse variable.
var apiResponse = await RoleUiService.CreateAsync(createRoleRequest);
Even though it actually sets the value on _messages, after this value is set. The debugger still shows apiResponse as null.
if (apiResponse.StatusCode != HttpStatusCode.BadRequest)
{
NavigationManager.NavigateTo("/RoleManagement/Roles",false);
}
else
{
_messages = apiResponse.ResponseMessages;
}
public class HttpService
{
public async Task<ApiResponse<T>> HttpPostAsync<T>(string uri, object dataToSend) where T : class
{
var content = ToJson(dataToSend);
var apiURL = $"{_apiBaseUrl}{uri}";
var result = await _httpClient.PostAsync(apiURL, content);
return await FromHttpResponseMessageAsync<T>(result);
}
}
public class RoleUiService
{
private readonly IHttpService _httpService;
public RoleUiService(IHttpService httpService)
{
_httpService = httpService;
}
private async Task SaveRole()
{
//Debugger always shows apiResponse as null even when _messages
gets a value from apiResponse.ResponseMessages
var apiResponse = await
RoleUiService.CreateAsync(createRoleRequest);
if (apiResponse.StatusCode != HttpStatusCode.BadRequest)
{
NavigationManager.NavigateTo("/RoleManagement/Roles",false);
}
else
{
_messages = apiResponse.ResponseMessages;
}
}
//Blazor wasm component code
public partial class CreateOrEditRole
{
[Inject]
public IRoleUiService RoleUiService { get; set; }
public async Task<ApiResponse<CreateRoleResponse>>
CreateAsync(CreateRoleRequest createRoleRequest)
{
var result = await _httpService.HttpPostAsync<CreateRoleResponse>
(CreateRoleRequest.Route,
createRoleRequest);
return result;
}
}
//Services registered in PROGRAM.cs
builder.Services.AddScoped<IHttpService, HttpService>();
builder.Services.AddScoped<IRoleUiService, RoleUiService>();
Thank you for the responses, see screen shots. Even though a value was pulled from the apiResponse and assigned to a variable the object is still reported as null.
apiResponse show null even though a value a was set to the var
Solution 1:[1]
This is one possible cause.
See the picture, exactly at that point, the name has not being changed, the change will happen as soon as you advance one step and the line where the name variable is assigned is no longer highlighted.
In this article, just bellow this picture it says:
Hover over the name variable to see its current value, which is an empty string.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Julio Cachay |