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.

Weird serialization behaviour

See original GitHub issue

Consider the following message definition in C#:

public interface CreateUser
{
    string UserId { get; }
    string UnverifiedPrimaryEmail { get; }
    string Language { get; }
}

F# has explicit interface implementations. The syntax looks like this:

type CreateUserMsg(uid, email, lang) =
  interface CreateUser with
    member _.UserId = uid
    member _.UnverifiedPrimaryEmail = lang
    member _.Language = email

For some reason, instantiating and sending this will send a message where all the properties are null.

As a counter-example, the following works fine:

type CreateUserMsg(uid, email, lang) =
  member _.UserId = uid
  member _.UnverifiedPrimaryEmail = email
  member _.Language = lang

  interface CreateUser with
    member _.UserId = "some"
    member _.UnverifiedPrimaryEmail = "other"
    member _.Language = "value"

Note that if one of the non-interface members are removed or renamed, that member will be serialized to null even though the interface contains the correct value. And that the actual (explicit) interface members are not used at all.

It might seem like MassTransit assumes the class itself contains members corresponding to those in the interface, and does not account for explicit interface implementations (which AFAIK is supported in C# too, by the way).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
cmeerencommented, Mar 13, 2020

Adapt this huge thing here? No thanks, that’s a maintenance burden I’d rather be without. I’ll look for other workarounds instead. 😃

For example, have C# contract projects where in addition to the message interfaces, I could add message classes implementing them. It’s not idiomatic, but it seems that’s the only way of sending strongly typed interface-based messages from F#, given the current serialization implementation.

Thanks for the help, though!

0reactions
phatboygcommented, Mar 13, 2020

You can certainly create a new serializer by implementing the interfaces and adding it to the supported serializers/deserializers.

You can configure the entire bus instance (all receive endpoints) or a single receive endpoint to use a serializer, as well as support the deserializer, using:

configurator.SetMessageSerializer(() => new RawJsonMessageSerializer());

configurator.AddMessageDeserializer(RawJsonMessageSerializer.RawJsonContentType,
    () => new RawJsonMessageDeserializer(RawJsonMessageSerializer.Deserializer));

That’s an example, you’d need your own content type. If you call:

configurator.ClearMessageDeserializers();

That will remove all of the default serializer/deserializers.

(In this example, RawJsonMessageSerializer is an example – if you wanted to support System.Text.Json, I’d suggest using the built-in JsonMessageSerializer as a base).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Weird serialisation behavior with HashMap - java
This is a problem with circular initialisation. Whilst Java Serialisation can handle arbitrary cycles, the initialisation has to happen in ...
Read more >
Odd serialization behaviour of UnityEvent inside an Editor ...
Hi Uniteers, I am observing a strange serialization behaviour for UnityEvents that I can't explain. Unity Events don't update their changes ...
Read more >
Strange behaviour with serialisation after upgrade to 5.5
Hi, we were stable on 4.5 for a long time and recently went live, upgrading to 5.5 and started noticing random issues.
Read more >
Weird serialization bug in prefab mode
In the external behavior tree we created all variables that are needed for the workings of that tree, and we planned on linking...
Read more >
Mind-bending behavior for deserialization in Haskell
While that's weird behavior, it's also kind of weird code. Serialization is a major system boundary and thus needs to be done really ......
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