Permalink
Browse files

Fix #507 Console doesn't work if ~/bin/Composite.Generated.dll is mis…

…sing
  • Loading branch information...
napernik committed Jan 3, 2018
1 parent 1a0cc02 commit 4b3863b566de30a9b1721ca2f0db3f64a961f4fc
@@ -253,6 +253,7 @@
<Compile Include="Core\Implementation\StatelessDataConnectionImplementation.cs" />
<Compile Include="Core\Instrumentation\DisposableResourceTracer.cs" />
<Compile Include="Core\Serialization\CompositeJsonSerializer.cs" />
<Compile Include="Core\Serialization\CompositeSerializationBinder.cs" />
<Compile Include="Core\WebClient\PageStructureRpc.cs" />
<Compile Include="Core\WebClient\Renderings\Page\IPageContentFilter.cs" />
<Compile Include="Core\WebClient\Renderings\INonCachebleRequestHostnameMapper.cs" />
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
@@ -40,7 +40,8 @@ public static string Serialize(object obj)
{
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
TypeNameHandling = TypeNameHandling.Auto,
Converters = {new JsonTypeConverter()}
Converters = {new JsonTypeConverter()},
Binder = CompositeSerializationBinder.Instance
});
return serializedData;
@@ -58,7 +59,8 @@ public static string SerializeObject(object obj)
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
TypeNameHandling = TypeNameHandling.Objects,
Formatting = Formatting.None,
Converters = {new JsonTypeConverter()}
Converters = {new JsonTypeConverter()},
Binder = CompositeSerializationBinder.Instance
});
return serializedData;
@@ -128,7 +130,8 @@ public static T Deserialize<T>(string str)
{
var obj = JsonConvert.DeserializeObject<T>(str, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
TypeNameHandling = TypeNameHandling.Auto,
Binder = CompositeSerializationBinder.Instance
});
return obj;
}
@@ -159,7 +162,8 @@ public static T Deserialize<T>(params string[] strs)
}
var obj = JsonConvert.DeserializeObject<T>(combinedObj.ToString(), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
TypeNameHandling = TypeNameHandling.Auto,
Binder = CompositeSerializationBinder.Instance
});
return obj;
}
@@ -173,7 +177,8 @@ public static object Deserialize(string str)
{
var obj = JsonConvert.DeserializeObject(str, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
TypeNameHandling = TypeNameHandling.Objects,
Binder = CompositeSerializationBinder.Instance
});
return obj;
}
@@ -0,0 +1,45 @@
using System;
using System.Runtime.Serialization;
using Composite.Core.Types;
using Newtonsoft.Json.Serialization;
namespace Composite.Core.Serialization
{
/// <summary>
/// Removes temproraty assembly references when serializing references to generated classes.
/// </summary>
internal class CompositeSerializationBinder: DefaultSerializationBinder
{
private const string GeneratedTypesNamespacePrefix = "CompositeGenerated.";
private const string GeneratedTypesAssemblyName = "Composite.Generated";
public static SerializationBinder Instance { get; } = new CompositeSerializationBinder();
public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
typeName = serializedType.FullName;
if (typeName.StartsWith(GeneratedTypesNamespacePrefix, StringComparison.OrdinalIgnoreCase)
&& Guid.TryParse(serializedType.Assembly.GetName().Name, out _))
{
assemblyName = GeneratedTypesAssemblyName;
}
else
{
assemblyName = serializedType.Assembly.FullName;
}
}
public override Type BindToType(string assemblyName, string typeName)
{
if (assemblyName == GeneratedTypesAssemblyName
&& typeName.StartsWith(GeneratedTypesNamespacePrefix, StringComparison.OrdinalIgnoreCase))
{
var result = TypeManager.TryGetType($"{typeName}, {assemblyName}") ?? TypeManager.TryGetType(typeName);
if (result != null) return result;
}
return base.BindToType(assemblyName, typeName);
}
}
}

0 comments on commit 4b3863b

Please sign in to comment.