开发者

Is WCF MessageBuffer.CreateMessage thread safe?

http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.messagebuffer(v=vs.85).aspx is somewhat vague when it says that "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."

As a general rule, it seems like instance members do not have the thread-safe guarantee.开发者_开发问答

However, I'm guessing that some instance member methods are reentrant and thread-safe and others are not.

MessageBuffer.CreateMessage is an instance method. Has anyone confirmed whether this specific method is reentrant (or whether callers need to implement locking around calls to the method) ?


I don't think until explicity specified, instance methods are always non thread safe. You can look at this method through reflector to confirm.
Also why are you concern about thread safety of this method? What is your usage scenario?


MessageBuffer.CreateMessage is abstract, so it doesn't make sense to ask whether it's thread safe or not. The subclasses of MessageBuffer in WCF are all internal, so they can potentially be changed. As Chandermani said, you should assume that it is not thread-safe.

Update: it is not thread-safe. The created message may have dependencies on other components, such as serialization of the message body. If those components aren't thread-safe, then the CreateMessage call cannot be considered thread-safe either.

In the example below, the serialized version of the object is time-dependent (it could have some additional dependencies as well), so the order in which the CreateMessage calls are made impacts the result.

public class StackOverflow_6209650_751090
{
    [DataContract]
    public class MyDC
    {
        [DataMember]
        public DateTime SerializedTime
        {
            get { return DateTime.Now; }
            set { }
        }
    }
    public static void Test()
    {
        Message message = Message.CreateMessage(MessageVersion.None, "foo", new MyDC());
        var buffer = message.CreateBufferedCopy(int.MaxValue);
        Console.WriteLine(buffer.CreateMessage());
        Console.WriteLine();
        Console.WriteLine(buffer.CreateMessage());
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜