question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

system.InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Routing.Patterns.RoutePatternParameterPart' to type 'Microsoft.AspNetCore.Routing.Patterns.RoutePatternLiteralPart'.

See original GitHub issue

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 ---

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
vimateckcommented, Apr 3, 2023

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?

0reactions
saber-wangcommented, Jun 1, 2022

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to cast object of type 'Microsoft.Asp.NetCore.Http. ...
In the application I upload an file (image) to the azure blob storage, and the URL of the uploaded file is stored in...
Read more >
System.InvalidCastException: Unable to cast object of type ...
HI, I received this message : System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Int32'.
Read more >
ASP.NET Core 中遇到了dapr subscribe 路由问题_已解决
InvalidCastException : Unable to cast object of type 'Microsoft.AspNetCore.Routing.Patterns.RoutePatternParameterPart' to type 'Microsoft.
Read more >
Converting integration tests to .NET Core 3.0
InvalidOperationException : Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.Server.IServer' while attempting to activate ' ...
Read more >
How can I fix the 'InvalidCastException: Unable to cast object ...
Coding example for the question How can I fix the 'InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found