"are binaryformatter serialize and deserialize thread safe?" Code Answer

3

according to msdn:

any public static (shared in visual basic) members of this type are thread safe. any instance members are not guaranteed to be thread safe.

so you need to synchronize access to serialize/deserialize methods.

have you identified particular performance issues by creating a local serializer instance every time?


update:

i would trust msdn because even if in some cases we can verify that instance members might be thread safe this doesn't mean that with the next service pack/update/framework version this will continue to be the case.

looking with reflector at binaryformatter constructor:

public binaryformatter()
{
    this.m_typeformat = formattertypestyle.typesalways;
    this.m_securitylevel = typefilterlevel.full;
    this.m_surrogates = null;
    this.m_context = new streamingcontext(streamingcontextstates.all);
}

and streamingcontext constructor:

public streamingcontext(streamingcontextstates state, object additional)
{
    this.m_state = state;
    this.m_additionalcontext = additional;
}

quite frankly assigning 6 properties (most of which are enums) should be blindingly fast. imho most of the time would be spent in serialize/deserialize methods.

By Sachin Joglekar on June 14 2022

Answers related to “are binaryformatter serialize and deserialize thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.