Skip to content

Commit a407afb

Browse files
hazzikfredericDelaporte
authored andcommitted
Implement SurrogateSelector (#1693)
- Supports System.Type, System.Reflection.MemberInfo and System.Delegate Closes #1652
1 parent f27fb48 commit a407afb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+765
-178
lines changed

src/NHibernate.Test/Async/CfgTest/ConfigurationSerializationTests.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using NHibernate.Driver;
1717
using NHibernate.Engine;
1818
using NHibernate.Tool.hbm2ddl;
19+
using NHibernate.Util;
1920
using NUnit.Framework;
2021

2122
namespace NHibernate.Test.CfgTest
@@ -28,8 +29,6 @@ public class ConfigurationSerializationTestsAsync
2829
[Test]
2930
public async Task Basic_CRUD_should_workAsync()
3031
{
31-
TestsContext.AssumeSystemTypeIsSerializable();
32-
3332
Assembly assembly = Assembly.Load("NHibernate.DomainModel");
3433
var cfg = new Configuration();
3534
if (TestConfigurationHelper.hibernateConfigFile != null)
@@ -38,7 +37,12 @@ public async Task Basic_CRUD_should_workAsync()
3837
}
3938
cfg.AddResource("NHibernate.DomainModel.ParentChild.hbm.xml", assembly);
4039

41-
var formatter = new BinaryFormatter();
40+
var formatter = new BinaryFormatter
41+
{
42+
#if !NETFX
43+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
44+
#endif
45+
};
4246
var memoryStream = new MemoryStream();
4347
formatter.Serialize(memoryStream, cfg);
4448
memoryStream.Position = 0;

src/NHibernate.Test/Async/ConnectionTest/AggressiveReleaseTest.cs

-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ protected override void Done()
5858
[Test]
5959
public async Task SerializationOnAfterStatementAggressiveReleaseAsync()
6060
{
61-
TestsContext.AssumeSystemTypeIsSerializable();
62-
6361
Prepare();
6462
ISession s = GetSessionUnderTest();
6563
Silly silly = new Silly("silly");
@@ -81,8 +79,6 @@ public async Task SerializationOnAfterStatementAggressiveReleaseAsync()
8179
[Test]
8280
public async Task SerializationFailsOnAfterStatementAggressiveReleaseWithOpenResourcesAsync()
8381
{
84-
TestsContext.AssumeSystemTypeIsSerializable();
85-
8682
Prepare();
8783
ISession s = GetSessionUnderTest();
8884

src/NHibernate.Test/Async/Criteria/CriteriaQueryTest.cs

-2
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,6 @@ public async Task CloningCriteria_AddCount_RemoveOrderingAsync()
475475
[Test]
476476
public async Task DetachedCriteriaTestAsync()
477477
{
478-
TestsContext.AssumeSystemTypeIsSerializable();
479-
480478
DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
481479
.Add(Property.ForName("Name").Eq("Gavin King"))
482480
.AddOrder(Order.Asc("StudentNumber"))

src/NHibernate.Test/Async/Criteria/DetachedCriteriaSerializable.cs

-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public Task DetachedCriteriaItSelfAsync()
5050
{
5151
try
5252
{
53-
TestsContext.AssumeSystemTypeIsSerializable();
54-
5553
DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
5654
.Add(Expression.Eq("Name", "Gavin King"));
5755
return SerializeAndListAsync(dc);
@@ -65,8 +63,6 @@ public Task DetachedCriteriaItSelfAsync()
6563
[Test]
6664
public async Task ExecutableCriteriaAsync()
6765
{
68-
TestsContext.AssumeSystemTypeIsSerializable();
69-
7066
// All query below don't have sense, are only to test if all needed classes are serializable
7167

7268
// Basic criterion

src/NHibernate.Test/Async/DynamicProxyTests/InterfaceProxySerializationTests/ProxyFixture.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.IO;
1414
using System.Runtime.Serialization;
1515
using System.Runtime.Serialization.Formatters.Binary;
16+
using NHibernate.Util;
1617
using NUnit.Framework;
1718

1819
namespace NHibernate.Test.DynamicProxyTests.InterfaceProxySerializationTests
@@ -36,7 +37,12 @@ private void SerializeAndDeserialize(ref ISession s)
3637
// Serialize the session
3738
using (Stream stream = new MemoryStream())
3839
{
39-
IFormatter formatter = new BinaryFormatter();
40+
var formatter = new BinaryFormatter
41+
{
42+
#if !NETFX
43+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
44+
#endif
45+
};
4046
formatter.Serialize(stream, s);
4147

4248
// Close the original session
@@ -106,8 +112,6 @@ public async Task ProxyAsync()
106112
[Test]
107113
public async Task ProxySerializeAsync()
108114
{
109-
TestsContext.AssumeSystemTypeIsSerializable();
110-
111115
ISession s = OpenSession();
112116
IMyProxy ap = new MyProxyImpl {Id = 1, Name = "first proxy"};
113117
await (s.SaveAsync(ap));
@@ -139,8 +143,6 @@ public async Task ProxySerializeAsync()
139143
[Test]
140144
public async Task SerializeNotFoundProxyAsync()
141145
{
142-
TestsContext.AssumeSystemTypeIsSerializable();
143-
144146
ISession s = OpenSession();
145147
// this does not actually exists in db
146148
var notThere = (IMyProxy) await (s.LoadAsync(typeof (MyProxyImpl), 5));

src/NHibernate.Test/Async/Legacy/FooBarTest.cs

+20-6
Original file line numberDiff line numberDiff line change
@@ -2644,8 +2644,6 @@ public async Task NewFlushingAsync()
26442644
[Test]
26452645
public async Task PersistCollectionsAsync()
26462646
{
2647-
TestsContext.AssumeSystemTypeIsSerializable();
2648-
26492647
ISession s = OpenSession();
26502648
ITransaction txn = s.BeginTransaction();
26512649
IEnumerator enumer = (await (s.CreateQuery("select count(*) from b in class Bar").EnumerableAsync())).GetEnumerator();
@@ -2843,7 +2841,20 @@ public async Task PersistCollectionsAsync()
28432841

28442842
// serialize and then deserialize the session.
28452843
Stream stream = new MemoryStream();
2846-
IFormatter formatter = new BinaryFormatter();
2844+
#if NETFX
2845+
var formatter = new BinaryFormatter();
2846+
#else
2847+
var selector = new SurrogateSelector();
2848+
selector.AddSurrogate(
2849+
typeof(CultureInfo),
2850+
new StreamingContext(StreamingContextStates.All),
2851+
new CultureInfoSerializationSurrogate());
2852+
selector.ChainSelector(new SerializationHelper.SurrogateSelector());
2853+
var formatter = new BinaryFormatter
2854+
{
2855+
SurrogateSelector = selector
2856+
};
2857+
#endif
28472858
formatter.Serialize(stream, s);
28482859

28492860
s.Close();
@@ -4659,8 +4670,6 @@ public async Task SaveDeleteAsync()
46594670
[Test]
46604671
public async Task ProxyArrayAsync()
46614672
{
4662-
TestsContext.AssumeSystemTypeIsSerializable();
4663-
46644673
ISession s = OpenSession();
46654674
GlarchProxy g = new Glarch();
46664675
Glarch g1 = new Glarch();
@@ -4697,7 +4706,12 @@ public async Task ProxyArrayAsync()
46974706

46984707
// serialize the session.
46994708
Stream stream = new MemoryStream();
4700-
IFormatter formatter = new BinaryFormatter();
4709+
var formatter = new BinaryFormatter()
4710+
{
4711+
#if !NETFX
4712+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
4713+
#endif
4714+
};
47014715
formatter.Serialize(stream, s);
47024716

47034717
// close the original session

src/NHibernate.Test/Async/Legacy/FumTest.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using NHibernate.DomainModel;
1717
using NHibernate.Criterion;
1818
using NHibernate.Type;
19+
using NHibernate.Util;
1920
using NUnit.Framework;
2021

2122
namespace NHibernate.Test.Legacy
@@ -718,7 +719,12 @@ public async Task UnflushedSessionSerializationAsync()
718719

719720
private ISession SpoofSerialization(ISession session)
720721
{
721-
BinaryFormatter formatter = new BinaryFormatter();
722+
var formatter = new BinaryFormatter
723+
{
724+
#if !NETFX
725+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
726+
#endif
727+
};
722728
MemoryStream stream = new MemoryStream();
723729
formatter.Serialize(stream, session);
724730

src/NHibernate.Test/Async/Legacy/MasterDetailTest.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using NHibernate.Engine;
1919
using NHibernate.Criterion;
2020
using NHibernate.Mapping;
21+
using NHibernate.Util;
2122
using NUnit.Framework;
2223
using Single=NHibernate.DomainModel.Single;
2324

@@ -643,8 +644,6 @@ public async Task NamedQueryAsync()
643644
[Test]
644645
public async Task SerializationAsync()
645646
{
646-
TestsContext.AssumeSystemTypeIsSerializable();
647-
648647
ISession s = OpenSession();
649648
Master m = new Master();
650649
Detail d1 = new Detail();
@@ -665,7 +664,12 @@ public async Task SerializationAsync()
665664
await (s.FlushAsync());
666665
s.Disconnect();
667666
MemoryStream stream = new MemoryStream();
668-
BinaryFormatter f = new BinaryFormatter();
667+
var f = new BinaryFormatter
668+
{
669+
#if !NETFX
670+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
671+
#endif
672+
};
669673
f.Serialize(stream, s);
670674
s.Close();
671675
stream.Position = 0;

src/NHibernate.Test/Async/NHSpecificTest/Dates/DateTimeOffsetFixture.cs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Xml.Serialization;
1717
using NHibernate.Driver;
1818
using NHibernate.Type;
19+
using NHibernate.Util;
1920
using NUnit.Framework;
2021

2122
namespace NHibernate.Test.NHSpecificTest.Dates

src/NHibernate.Test/Async/NHSpecificTest/NH2773/Fixture.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System;
1212
using System.IO;
1313
using System.Runtime.Serialization.Formatters.Binary;
14+
using NHibernate.Util;
1415
using NUnit.Framework;
1516

1617
namespace NHibernate.Test.NHSpecificTest.NH2773 {
@@ -50,9 +51,7 @@ protected override void OnTearDown() {
5051
[Test]
5152
public async Task DeserializedSession_ProxyType_ShouldBeEqualToOriginalProxyTypeAsync()
5253
{
53-
TestsContext.AssumeSystemTypeIsSerializable();
54-
55-
System.Type originalProxyType = null;
54+
System.Type originalProxyType = null;
5655
System.Type deserializedProxyType = null;
5756
ISession deserializedSession = null;
5857

@@ -64,8 +63,14 @@ public async Task DeserializedSession_ProxyType_ShouldBeEqualToOriginalProxyType
6463
}
6564

6665

67-
using (MemoryStream sessionMemoryStream = new MemoryStream()) {
68-
BinaryFormatter formatter = new BinaryFormatter();
66+
using (MemoryStream sessionMemoryStream = new MemoryStream())
67+
{
68+
var formatter = new BinaryFormatter
69+
{
70+
#if !NETFX
71+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
72+
#endif
73+
};
6974
formatter.Serialize(sessionMemoryStream, session);
7075

7176
sessionMemoryStream.Seek(0, SeekOrigin.Begin);

src/NHibernate.Test/Async/NHSpecificTest/NH2880/Fixture.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System;
1212
using System.IO;
1313
using System.Runtime.Serialization.Formatters.Binary;
14+
using NHibernate.Util;
1415
using NUnit.Framework;
1516

1617
namespace NHibernate.Test.NHSpecificTest.NH2880
@@ -45,8 +46,6 @@ protected override void OnSetUp()
4546
[Test]
4647
public async Task ProxiesFromDeserializedSessionsCanBeLoadedAsync()
4748
{
48-
TestsContext.AssumeSystemTypeIsSerializable();
49-
5049
MemoryStream sessionMemoryStream;
5150

5251
using (ISession s = Sfi.OpenSession())
@@ -58,12 +57,22 @@ public async Task ProxiesFromDeserializedSessionsCanBeLoadedAsync()
5857
}
5958

6059
sessionMemoryStream = new MemoryStream();
61-
BinaryFormatter writer = new BinaryFormatter();
60+
var writer = new BinaryFormatter
61+
{
62+
#if !NETFX
63+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
64+
#endif
65+
};
6266
writer.Serialize(sessionMemoryStream, s);
6367
}
6468

6569
sessionMemoryStream.Seek(0, SeekOrigin.Begin);
66-
BinaryFormatter reader = new BinaryFormatter();
70+
var reader = new BinaryFormatter
71+
{
72+
#if !NETFX
73+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
74+
#endif
75+
};
6776
ISession restoredSession = (ISession)reader.Deserialize(sessionMemoryStream);
6877

6978
Entity1 e1 = await (restoredSession.GetAsync<Entity1>(_id));

src/NHibernate.Test/Async/NHSpecificTest/NH2898/BinaryFormatterCache.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.IO;
1313
using System.Runtime.Serialization.Formatters.Binary;
1414
using NHibernate.Cache;
15+
using NHibernate.Util;
1516

1617
namespace NHibernate.Test.NHSpecificTest.NH2898
1718
{
@@ -28,7 +29,12 @@ public Task<object> GetAsync(object key, CancellationToken cancellationToken)
2829
if (entry == null)
2930
return Task.FromResult<object>(null);
3031

31-
var fmt = new BinaryFormatter();
32+
var fmt = new BinaryFormatter
33+
{
34+
#if !NETFX
35+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
36+
#endif
37+
};
3238
using (var stream = new MemoryStream(entry))
3339
{
3440
return Task.FromResult<object>(fmt.Deserialize(stream));
@@ -44,7 +50,12 @@ public Task PutAsync(object key, object value, CancellationToken cancellationTok
4450
{
4551
try
4652
{
47-
var fmt = new BinaryFormatter();
53+
var fmt = new BinaryFormatter
54+
{
55+
#if !NETFX
56+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
57+
#endif
58+
};
4859
using (var stream = new MemoryStream())
4960
{
5061
fmt.Serialize(stream, value);

src/NHibernate.Test/Async/NHSpecificTest/NH3119/FixtureByCode.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using NHibernate.Cfg.MappingSchema;
1818
using NHibernate.Mapping.ByCode;
1919
using NHibernate.Properties;
20+
using NHibernate.Util;
2021
using NUnit.Framework;
2122
using NHibernate.Linq;
2223

@@ -98,14 +99,22 @@ public async Task PocoComponentTuplizer_Instantiate_UsesReflectonOptimizerAsync(
9899
[Test]
99100
public async Task PocoComponentTuplizerOfDeserializedConfiguration_Instantiate_UsesReflectonOptimizerAsync()
100101
{
101-
TestsContext.AssumeSystemTypeIsSerializable();
102-
103102
MemoryStream configMemoryStream = new MemoryStream();
104-
BinaryFormatter writer = new BinaryFormatter();
103+
var writer = new BinaryFormatter
104+
{
105+
#if !NETFX
106+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
107+
#endif
108+
};
105109
writer.Serialize(configMemoryStream, cfg);
106110

107111
configMemoryStream.Seek(0, SeekOrigin.Begin);
108-
BinaryFormatter reader = new BinaryFormatter();
112+
var reader = new BinaryFormatter
113+
{
114+
#if !NETFX
115+
SurrogateSelector = new SerializationHelper.SurrogateSelector()
116+
#endif
117+
};
109118
Configuration deserializedConfig = (Configuration)reader.Deserialize(configMemoryStream);
110119
ISessionFactory factoryFromDeserializedConfig = deserializedConfig.BuildSessionFactory();
111120

0 commit comments

Comments
 (0)