dotnet-sdk
dotnet-sdk copied to clipboard
system.InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Routing.Patterns.RoutePatternParameterPart' to type 'Microsoft.AspNetCore.Routing.Patterns.RoutePatternLiteralPart'.
Error reporting using apiversion
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class SubscriptionEventsController : SaaSControllerBase
{
private readonly ILogger _logger;
public SubscriptionEventsController(ILogger<ServiceInvocationController> logger)
{
_logger = logger;
}
[Topic(DaprDefaults.BusDefaults.DAPR_PUBSUB_NAME, nameof(DaprDemo_TestEvent))]
[HttpPost("subscription")]
public void Subscription(DaprDemo_TestEvent demoEvent)
{
_logger.LogInformation("Subscription===>{@demoEvent}", demoEvent);
}
}
access /dapr/subscrib
error:
An unhandled exception has occurred while executing the request.
System.InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Routing.Patterns.RoutePatternParameterPart' to type 'Microsoft.AspNetCore.Routing.Patterns.RoutePatternLiteralPart'.
at System.Linq.Enumerable.CastIterator[TResult](IEnumerable source)+MoveNext()
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at System.String.Concat(IEnumerable`1 values)
at Microsoft.AspNetCore.Builder.DaprEndpointRouteBuilderExtensions.<>c.<RoutePatternToString>b__3_0(RoutePatternPathSegment segment)
at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
at System.String.Join(String separator, IEnumerable`1 values)
at Microsoft.AspNetCore.Builder.DaprEndpointRouteBuilderExtensions.RoutePatternToString(RoutePattern routePattern)
at Microsoft.AspNetCore.Builder.DaprEndpointRouteBuilderExtensions.<>c.<CreateSubscribeEndPoint>b__2_11(ValueTuple`6 e)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.ToList()
at Microsoft.AspNetCore.Builder.DaprEndpointRouteBuilderExtensions.<>c__DisplayClass2_0.<CreateSubscribeEndPoint>b__5(IOrderedEnumerable`1 e)
at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
at System.Linq.Enumerable.SelectEnumerableIterator`2.ToArray()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.OrderedEnumerable`1.GetEnumerator()+MoveNext()
at System.Text.Json.Serialization.Converters.IEnumerableOfTConverter`2.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter`2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at Microsoft.AspNetCore.Builder.DaprEndpointRouteBuilderExtensions.<>c.<<CreateSubscribeEndPoint>b__2_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
Removing or hardcoding the version from the controller route fixes the issue?
[Route("api/v1.0/[controller]")]
@fbridger Yes, it can be solved. Apiversion cannot be supported
@saber-wang - Given the fix, does this work for you? Or do you think we should have the direct support for ApiVersion?
@halspang If you can support, this is the best. If you can't, I'll use hard coding😉
Any Update for this ?
@itsoli91 Because dapr does not support subscribing to the same topic multiple times in an instance, it is meaningless to support versions. @halspang For this error, I think we should throw more explicit exceptions and add relevant documents.
@saber-wang Thank you I got the point, but what if app only subscribes to the latest version? Another Idea whould be to include versions in topics, but I'm not sure if it's related to the sdks or dapr itself.
@itsoli91
Currently, only the first route is selected for multiple routes on the SDK
The version of the topic is independent of the SDK. In addition, you can refer to the cloud event version #868 route-messages
Hello,
we have also encountered this problem. We would like to have the ability to define multiple api versions within a controller and also map them to the pub sub model of dapr.
As far as I understand the ASP.net implementation, I can do the following:
[ApiController]
[ApiVersion("1.0")]
[ApiVersion("2.0")]
[route("api/v{v:apiVersion}/test")]
public class TestController : ControllerBase
{
[Topic(DaprComponents.PubSub, "pubsub-test")]
[HttpPost("pubsub")]
[MapToApiVersion("1.0")]
public IActionResult PubSubTest()
{
return Ok("PubSubTest");
}
[Topic(DaprComponents.PubSub, "pubsub-test")]
[HttpPost("pubsub2")]
[MapToApiVersion("2.0")]
public IActionResult PubSubTest2()
{
return Ok("PubSubTest2");
}
}
I have defined two versions within the controller and mapped the two actions to a specific version each.
I would now expect the Dapr Topic subscription to use the version defined using MapToApiVersion (or, if only one ApiVersion is defined, use the only existing one).
Right now, DaprEndpointRouteBuilderExtensions:201 is trying to turn the path segment into a RoutePatternLiteralPart, but if we are working with variables within the path, it could also be a RoutePatternParameterPart.
Is there a plan to support these parameters in the near future?