Objekte können nicht gießen, wenn die Typen vererbt werden könnenC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Objekte können nicht gießen, wenn die Typen vererbt werden können

Post by Anonymous »

Der Fehler tritt nur mit ReverseProxy -Server auf. Mit normalem Server ist alles in Ordnung. /> Der Fehler trat in dieser Methode auf: < /p>

Code: Select all

public async Task MarkNotifications(AccountId accountKey, NotificationDto[] notificationsToUpdate)
{
var result = new List(notificationsToUpdate.Length);
var notificationKeys = notificationsToUpdate.Select(x => x.NotificationKey).ToHashSet();
var notifications = await _uow.Notifications.GetNotifications(notificationKeys, accountKey);

foreach (var updateItem in notificationsToUpdate)
{
var foundNotification = notifications.FirstOrDefault(x => x.NotificationKey == updateItem.NotificationKey);

if (foundNotification != null)
{
foundNotification.Status = updateItem.Status ?? foundNotification.Status;
foundNotification.Importance = updateItem.Important switch
{
true => NotificationImportance.High,
false => NotificationImportance.Low,
_ => foundNotification.Importance,
};

result.Add(new NotificationMarkResultDto(updateItem.NotificationKey, foundNotification.Status, foundNotification.Importance));
}
else
{
result.Add(new NotificationMarkResultDto(updateItem.NotificationKey, "Notificaton not found"));
}
}

await _uow.SaveAsync();

return result;
}
Warum tritt ein Fehler auf, wenn ireadonlyCollection in der Erbschaftskette von "List" angezeigt wird?

Code: Select all

[HttpPost("marks")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ValidationProblemDetails))]
[ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(ProblemDetails))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ProblemDetails))]
public async Task MarkNotifications([FromBody, Required] NotificationDto[] update)
{
return await _notificationService.MarkNotifications(params);
}
< /code>
Auch der gesamte Fehler enthält diese Zeilen: < /p>
{
"type": "https://httpstatuses.com/500",
"title": "Unable to cast object of type 'd__1[Acp.Domain.Queries.Dtos.Notifications.NotificationMarkResultDto]' to type 'System.Collections.Generic.IReadOnlyCollection`1[Notifications.NotificationMarkResultDto]'.",
"status": 500,
"detail": "System.InvalidCastException: Unable to cast object of type 'd__1[NotificationMarkResultDto]' to type 'System.Collections.Generic.IReadOnlyCollection`1[Notifications.NotificationMarkResultDto]'.\n
at Net.NetCommHost.NetServiceProxy.TaskWrapper`1.c.b__0_0(Task`1 x)
\n at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()\n
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\n--- End of stack trace from previous location ---\n
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\n
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)\n--- End of stack trace from previous location ---\n
at Controllers.NotificationController.MarkNotifications(NotificationDto[] update) in /source/src/Controllers/NotificationController.cs:line 101\n at lambda_method305(Closure, Object)\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean&  isCompleted)\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|26_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)"
}
< /code>
In der IDE hebt der Compiler die explizite Typumwandlung für die Servicemethode in grauer Farbe als unnötigen Code hervor:return (IReadOnlyCollection)result;

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post