Closed GoogleCodeExporter closed 9 years ago
r585
Original comment by marc.gravell
on 29 Aug 2012 at 7:36
Hi,
The fix works now for my example, so thanks for that.
However, I am now getting a stackoverflow exception now when I call GetSchema
on types that use serialization surrogates. The problem is in MetaType.cs in
the following code, where GetSchemaTypeName keeps calling itself:
internal string GetSchemaTypeName()
{
if (surrogate != null) return model[surrogate].GetSchemaTypeName();
if (!Helpers.IsNullOrEmpty(name)) return name;
#if !NO_GENERICS
if (type
#if WINRT
.GetTypeInfo()
#endif
.IsGenericType)
{
StringBuilder sb = new StringBuilder(type.Name);
int split = type.Name.IndexOf('`');
if (split >= 0) sb.Length = split;
foreach (Type arg in type
#if WINRT
.GetTypeInfo().GenericTypeArguments
#else
.GetGenericArguments()
#endif
)
{
sb.Append('_');
Type tmp = arg;
int key = model.GetKey(ref tmp);
MetaType mt;
if (key >= 0 && (mt = model[tmp]) != null)
{
sb.Append(mt.GetSchemaTypeName());
}
else
{
sb.Append(tmp.Name);
}
}
return sb.ToString();
}
#endif
return type.Name;
}
This was not happening before revision 585
Original comment by Franches...@gmail.com
on 29 Aug 2012 at 8:11
oops! I'll check that; probably another commit in a few minutes
Original comment by marc.gravell
on 29 Aug 2012 at 8:13
I have been unable to reproduce this. Do you have an example model that shows
this stackoverflow? (I have, however, tidied the general behaviour when
handling surrogates)
Original comment by marc.gravell
on 29 Aug 2012 at 8:42
Yes, in my model I have System.Exception, and a few other exceptions that
inherit from it. I am adding them all to the model with a surrogate as follows,
so I can receive server exceptions and rethrow them on the client:
metaType = runtimeTypeModel.Add(myExceptionType, false);
metaType.SetSurrogate(typeof(BinarySerializationSurrogate<>).MakeGenericType(myE
xceptionType));
This is my surrogate:
/// <summary>
/// Surrogate class to allow Protobuf-net to serialize any class that implements ISerializeable
/// (e.g. Exceptions).
/// </summary>
/// <typeparam name="T">The type of an object that implements ISerializeable.</typeparam>
[ProtoContract]
internal class BinarySerializationSurrogate<T>
{
[ProtoMember(1)]
private byte[] objectData = null;
public static implicit operator T(BinarySerializationSurrogate<T> surrogate)
{
T returnValue = default(T);
if (surrogate == null)
{
return returnValue;
}
var serializer = new BinaryFormatter();
using (var serializedStream = new MemoryStream(surrogate.objectData))
{
returnValue = (T)serializer.Deserialize(serializedStream);
}
return returnValue;
}
public static implicit operator BinarySerializationSurrogate<T>(T objectToSerialize)
{
if (objectToSerialize == null)
{
return null;
}
var returnValue = new BinarySerializationSurrogate<T>();
var serializer = new BinaryFormatter();
using (var serializedStream = new MemoryStream())
{
serializer.Serialize(serializedStream, objectToSerialize);
returnValue.objectData = serializedStream.ToArray();
}
return returnValue;
}
}
Original comment by Franches...@gmail.com
on 29 Aug 2012 at 8:57
k; try now
Original comment by marc.gravell
on 29 Aug 2012 at 9:16
Everything works fine now, thanks!!!!! :)
Original comment by Franches...@gmail.com
on 29 Aug 2012 at 11:29
Original comment by marc.gravell
on 29 Aug 2012 at 12:35
Original issue reported on code.google.com by
Franches...@gmail.com
on 27 Aug 2012 at 2:16