Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscribe to raw messages using .Net 6 and Dapr SDK #953

Open
mandarj123 opened this issue Sep 20, 2022 · 2 comments
Open

subscribe to raw messages using .Net 6 and Dapr SDK #953

mandarj123 opened this issue Sep 20, 2022 · 2 comments
Labels
kind/bug Something isn't working

Comments

@mandarj123
Copy link

mandarj123 commented Sep 20, 2022

I am listening to MQTT provider using Dapr and it will be not be sending events in CloudEvents Format.

My intention is to be able to process messages as raw string and then do any conversion needed.

Currently my setup is as follows.

using Dapr.Client;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
//AddDapr() registers the Dapr integration with controllers. 
builder.Services.AddControllers().AddDapr(
     builder => builder.UseJsonSerializationOptions(
          new System.Text.Json.JsonSerializerOptions()
          {
              PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
              PropertyNameCaseInsensitive = true
          }));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();



var app = builder.Build();


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseRouting();

//registers the Cloud Events middleware in the request processing pipeline.
//This middleware will unwrap requests with Content-Type application/cloudevents+json so that model binding can access the event payload in the request body directly. 
app.UseCloudEvents();

app.UseAuthorization();


app.UseEndpoints(endpoints =>
{
    endpoints.MapSubscribeHandler();
    endpoints.MapControllers();
});

In controller

    [Topic("mqtt-pubsub", "testtopic", true)]
    [HttpPost("ReceiveMessages")]
    public ActionResult<string> Process([FromBody] object message, [FromServices] DaprClient daprClient)
    {
        return message.ToString();
    }

I am getting serialization errors.

time="2022-09-20T12:36:11.9731999+05:30" level=error msg="Failed processing MQTT message: testtopic#51: retriable error returned from app while processing pub/sub event 52f1b281-71e9-4bee-bacc-6a12b8c85841, topic: testtopic, body: {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13\",\"title\":\"Unsupported Media Type","status":415,"traceId":"00-93976651ea98962241a0dd3015eaca01-3db0514083fbbc9f-00"}. status code returned: 415" app_id=order-processor instance=LIN22004804 scope=dapr.contrib type=log ver=1.8.4

If i do not put app.UseCloudEvents() then the subscription doesn't even work.

As you can see in controller, i have enabled raw messages. but how to get around this error if sender is not using cloudevents format at all? I would assume, it's a very common scenario!

@mandarj123 mandarj123 added the kind/bug Something isn't working label Sep 20, 2022
@mandarj123 mandarj123 changed the title subscribe to raw messages using .Net Core 6 subscribe to raw messages using .Net 6 and Dapr SDK Sep 20, 2022
@fgheysels
Copy link

I'm actually running into the same error, so I am actually interested if there's a solution :)

@fgheysels
Copy link

I've fixed it by creating a custom input-formatter.
More info can be found here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants