How do I create an DataContract with optional parameters?
Options
I created a DataContract (former Event) class with this property:
[DataMember(Code = (byte)ParameterKey.ReplacementPlayerPositioning, IsOptional = true)] public byte[] ReplacementPlayerPositioning { get; private set; }And this is my method inside the DataContract class to get EventData to send the event to the peers:
public virtual EventData GetEventData() { return new EventData((byte) Code,this); }I'm getting an exception when I try to send events without the optional property, i.e., when the property is null:
2012-08-14 18:57:33,867 [13] ERROR FutWeb.Server.Match.MatchPeer [(null)] - System.ArgumentException: Null value: 48 (LineupChangedEvent.ReplacementPlayerPositioning) at Photon.SocketServer.Rpc.Reflection.ObjectDataMemberMapper.GetValues[TAttribute](Object source) in c:\build\photon-socketserver-sdk_3.0\src\Photon.SocketServer\Rpc\Reflection\ObjectDataMemberMapper.cs:line 77 at Photon.SocketServer.EventData.SetParameters(Object dataContract) in c:\build\photon-socketserver-sdk_3.0\src\Photon.SocketServer\EventData.cs:line 178 at FW.Server.Match.Events.MatchEvent.GetEventData() at FW.Server.Match.MatchPeer.State.PublishEvent(MatchEvent matchEvent)So what's the right way to create this DataContract?
0
Comments
-
Apparently this is a bug, when I do this:
public virtual EventData GetEventData() { return new EventData((byte) Code,this.ToDictionary()); }
it works!
So, use the EventData(Byte, Dictionary<Byte, Object>) constructor instead of EventData(Byte, Object) when you have optional parameters.0 -
I cannot reproduce the bug. I have created the follwing test code and evrything works like expected.
public enum ParameterKey { ReplacementPlayerPositioning = 1 } private class LineupChangedEvent : DataContract { public byte Code { get { return 1;} } [DataMember(Code = (byte)ParameterKey.ReplacementPlayerPositioning, IsOptional = true)] public byte[] ReplacementPlayerPositioning { get; private set; } public virtual EventData GetEventData() { return new EventData((byte)this.Code, this); } } public void Test() { var e = new LineupChangedEvent(); var evenDate = e.GetEventData(); // no error here }
0