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

deserializeMsgPack method throws exception while using with ASP.NET Core #1

Closed
anuraj opened this issue Sep 5, 2018 · 6 comments
Closed

Comments

@anuraj
Copy link

anuraj commented Sep 5, 2018

I am using msgpack.js with Message Pack implementation in C# for ASP.NET Core. But I am getting an error like this.

image

Here is the code.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddMvcOptions(option =>
    {
        option.OutputFormatters.Clear();
        option.OutputFormatters.Add(new MessagePackOutputFormatter(StandardResolver.Instance));
        option.InputFormatters.Clear();
        option.InputFormatters.Add(new MessagePackInputFormatter(StandardResolver.Instance));
    }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

And here is the client code.

<script src="jquery-3.3.1.min.js"></script>
<script src="msgpack.js"></script>
<script>
    $(function () {
        $.get("/api/person", function (data) {
            console.log(deserializeMsgPack(data));
        });
    });
</script>

I am using it in a Web API project, and returning default values controller.

@ygoe
Copy link
Owner

ygoe commented Sep 5, 2018

Can you please also log the content of data and its type (typeof(data))? This doesn't look like a byte array which it's supposed to be.

@anuraj
Copy link
Author

anuraj commented Sep 5, 2018

It is a complex object. But I tried with simple string, that also not working.

[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
    return "value";
}

Here is the response header.

image

@ygoe
Copy link
Owner

ygoe commented Sep 5, 2018

The thing is, the deserializeMsgPack function expects a byte array to be passed in. A byte array consists only of bytes. A byte is an integer value in the range of 0 to 255. All of these values are handled in the code. If something else occurs, then it's not a byte array. And chances are that it needs further conversion to be deserialisable.

I'll have to create a sample project and try out myself what's happening there. I can't make any more assumptions without investigating live code.

Here's code from where I use this library. It processes data received from a WebSocket client.

function onMessage(event) {
    let array = new Uint8Array(event.data);
    let obj = deserialize(array);
}

event.data is already a binary array and is then converted into a Uint8Array. Maybe you need to do something similar.

@anuraj
Copy link
Author

anuraj commented Sep 5, 2018

Okay. Let me try.

@ygoe
Copy link
Owner

ygoe commented Oct 11, 2018

Any news here? I can't create a demo project because the type MessagePackOutputFormatter cannot be found anywhere. Can you maybe provide a minimal test case for this? I only know about ASP.NET Core 2.1, you seem to use 2.2. Maybe I'll have to wait a bit to run your example.

@anuraj
Copy link
Author

anuraj commented Oct 11, 2018

Oops, my bad. I am able to resolve it. Full details here - https://dotnetthoughts.net/using-message-pack-with-asp-net-core/. Thank you for your response 👍

@ygoe ygoe closed this as completed Oct 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants