Skip to content

Commit 225d8e0

Browse files
committed
NH-3807 - Switch stream's over to using statements.
Stream.Close() is removed from CoreClr.
1 parent f9a7693 commit 225d8e0

File tree

6 files changed

+88
-93
lines changed

6 files changed

+88
-93
lines changed

src/NHibernate.Test/Legacy/FooBarTest.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,15 +2830,16 @@ public void PersistCollections()
28302830

28312831
#if FEATURE_SERIALIZATION
28322832
// serialize and then deserialize the session.
2833-
Stream stream = new MemoryStream();
28342833
IFormatter formatter = new BinaryFormatter();
2835-
formatter.Serialize(stream, s);
2834+
using (Stream stream = new MemoryStream())
2835+
{
2836+
formatter.Serialize(stream, s);
28362837

2837-
s.Close();
2838+
s.Close();
28382839

2839-
stream.Position = 0;
2840-
s = (ISession) formatter.Deserialize(stream);
2841-
stream.Close();
2840+
stream.Position = 0;
2841+
s = (ISession) formatter.Deserialize(stream);
2842+
}
28422843
#endif
28432844

28442845
s.Reconnect();
@@ -2880,14 +2881,15 @@ public void PersistCollections()
28802881

28812882
#if FEATURE_SERIALIZATION
28822883
// serialize and then deserialize the session.
2883-
stream = new MemoryStream();
2884-
formatter.Serialize(stream, s);
2884+
using (MemoryStream stream = new MemoryStream())
2885+
{
2886+
formatter.Serialize(stream, s);
28852887

2886-
s.Close();
2888+
s.Close();
28872889

2888-
stream.Position = 0;
2889-
s = (ISession) formatter.Deserialize(stream);
2890-
stream.Close();
2890+
stream.Position = 0;
2891+
s = (ISession) formatter.Deserialize(stream);
2892+
}
28912893
#endif
28922894

28932895
Qux nonexistentQux = (Qux) s.Load(typeof(Qux), (long) 666); //nonexistent
@@ -4688,17 +4690,18 @@ public void ProxyArray()
46884690

46894691
#if FEATURE_SERIALIZATION
46904692
// serialize the session.
4691-
Stream stream = new MemoryStream();
4692-
IFormatter formatter = new BinaryFormatter();
4693-
formatter.Serialize(stream, s);
4693+
using (Stream stream = new MemoryStream())
4694+
{
4695+
IFormatter formatter = new BinaryFormatter();
4696+
formatter.Serialize(stream, s);
46944697

4695-
// close the original session
4696-
s.Close();
4698+
// close the original session
4699+
s.Close();
46974700

4698-
// deserialize the session
4699-
stream.Position = 0;
4700-
s = (ISession) formatter.Deserialize(stream);
4701-
stream.Close();
4701+
// deserialize the session
4702+
stream.Position = 0;
4703+
s = (ISession) formatter.Deserialize(stream);
4704+
}
47024705
#endif
47034706

47044707
s.Close();

src/NHibernate.Test/Legacy/MasterDetailTest.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -662,14 +662,15 @@ public void Serialization()
662662
}
663663
s.Flush();
664664
s.Disconnect();
665-
MemoryStream stream = new MemoryStream();
666665
BinaryFormatter f = new BinaryFormatter();
667-
f.Serialize(stream, s);
668-
stream.Position = 0;
669-
Console.WriteLine(stream.Length);
666+
using (MemoryStream stream = new MemoryStream())
667+
{
668+
f.Serialize(stream, s);
669+
stream.Position = 0;
670+
Console.WriteLine(stream.Length);
670671

671-
s = (ISession) f.Deserialize(stream);
672-
stream.Close();
672+
s = (ISession) f.Deserialize(stream);
673+
}
673674

674675
s.Reconnect();
675676
Master m2 = (Master) s.Load(typeof(Master), mid);
@@ -696,12 +697,13 @@ public void Serialization()
696697
object mid2 = s.Save(new Master());
697698
s.Flush();
698699
s.Disconnect();
699-
stream = new MemoryStream();
700-
f.Serialize(stream, s);
701-
stream.Position = 0;
700+
using (MemoryStream stream = new MemoryStream())
701+
{
702+
f.Serialize(stream, s);
703+
stream.Position = 0;
702704

703-
s = (ISession) f.Deserialize(stream);
704-
stream.Close();
705+
s = (ISession) f.Deserialize(stream);
706+
}
705707

706708
s.Reconnect();
707709
s.Delete(s.Load(typeof(Master), mid));
@@ -711,20 +713,18 @@ public void Serialization()
711713

712714
s = OpenSession();
713715
string db = s.Connection.Database; //force session to grab a connection
714-
try
716+
using (MemoryStream stream = new MemoryStream())
715717
{
716-
stream = new MemoryStream();
717-
f.Serialize(stream, s);
718-
}
719-
catch (Exception e)
720-
{
721-
Assert.IsTrue(e is InvalidOperationException, "illegal state");
718+
try
719+
{
720+
f.Serialize(stream, s);
721+
}
722+
catch (Exception e)
723+
{
724+
Assert.IsTrue(e is InvalidOperationException, "illegal state");
722725
s.Close();
723-
return;
724-
}
725-
finally
726-
{
727-
stream.Close();
726+
return;
727+
}
728728
}
729729
Assert.IsTrue(false, "serialization should have failed");
730730
}

src/NHibernate.Test/NHSpecificTest/NH317/Fixture.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ public void ProxySerialization()
4545

4646
// Serialize
4747
IFormatter formatter = new BinaryFormatter();
48-
MemoryStream ms = new MemoryStream();
49-
formatter.Serialize(ms, nodeProxy);
48+
Node deserializedNodeProxy;
49+
using (MemoryStream ms = new MemoryStream())
50+
{
51+
formatter.Serialize(ms, nodeProxy);
5052

51-
// Deserialize
52-
ms.Seek(0, SeekOrigin.Begin);
53-
Node deserializedNodeProxy = (Node) formatter.Deserialize(ms);
54-
ms.Close();
53+
// Deserialize
54+
ms.Seek(0, SeekOrigin.Begin);
55+
deserializedNodeProxy = (Node) formatter.Deserialize(ms);
56+
}
5557

5658
// Deserialized proxy should implement the INHibernateProxy interface.
5759
Assert.IsTrue(deserializedNodeProxy is INHibernateProxy);

src/NHibernate.Test/UtilityTest/LinkedHashMapFixture.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,15 @@ public void Serialization()
240240
IDictionary<string, Player> lhm = new LinkedHashMap<string, Player>();
241241
Fill(lhm);
242242

243-
MemoryStream stream = new MemoryStream();
244-
BinaryFormatter f = new BinaryFormatter();
245-
f.Serialize(stream, lhm);
246-
stream.Position = 0;
243+
LinkedHashMap<string, Player> dlhm;
244+
using (MemoryStream stream = new MemoryStream())
245+
{
246+
BinaryFormatter f = new BinaryFormatter();
247+
f.Serialize(stream, lhm);
248+
stream.Position = 0;
247249

248-
LinkedHashMap<string, Player> dlhm = (LinkedHashMap<string, Player>)f.Deserialize(stream);
249-
stream.Close();
250+
dlhm = (LinkedHashMap<string, Player>) f.Deserialize(stream);
251+
}
250252

251253
Assert.AreEqual(6, dlhm.Count);
252254
int index = 0;

src/NHibernate.Test/UtilityTest/SequencedHashMapFixture.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,15 @@ public void Performance()
427427
[Test]
428428
public void Serialize()
429429
{
430-
MemoryStream stream = new MemoryStream();
431-
BinaryFormatter f = new BinaryFormatter();
432-
f.Serialize(stream, _shm);
433-
stream.Position = 0;
430+
SequencedHashMap shm;
431+
using (MemoryStream stream = new MemoryStream())
432+
{
433+
BinaryFormatter f = new BinaryFormatter();
434+
f.Serialize(stream, _shm);
435+
stream.Position = 0;
434436

435-
SequencedHashMap shm = (SequencedHashMap) f.Deserialize(stream);
436-
stream.Close();
437+
shm = (SequencedHashMap) f.Deserialize(stream);
438+
}
437439

438440
Assert.AreEqual(3, shm.Count);
439441
int index = 0;

src/NHibernate/Cfg/Configuration.cs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -669,30 +669,25 @@ public Configuration AddResource(string path, Assembly assembly)
669669
{
670670
string debugName = path;
671671
log.Info("Mapping resource: " + debugName);
672-
Stream rsrc = assembly.GetManifestResourceStream(path);
673-
if (rsrc == null)
672+
using (Stream rsrc = assembly.GetManifestResourceStream(path))
674673
{
675-
LogAndThrow(new MappingException("Resource not found: " + debugName));
676-
}
674+
if (rsrc == null)
675+
{
676+
LogAndThrow(new MappingException("Resource not found: " + debugName));
677+
}
677678

678-
try
679-
{
680-
return AddInputStream(rsrc, debugName);
681-
}
682-
catch (MappingException)
683-
{
684-
throw;
685-
}
686-
catch (Exception e)
687-
{
688-
LogAndThrow(new MappingException("Could not configure datastore from resource " + debugName, e));
689-
return this; // To please the compiler
690-
}
691-
finally
692-
{
693-
if (rsrc != null)
679+
try
680+
{
681+
return AddInputStream(rsrc, debugName);
682+
}
683+
catch (MappingException)
694684
{
695-
rsrc.Close();
685+
throw;
686+
}
687+
catch (Exception e)
688+
{
689+
LogAndThrow(new MappingException("Could not configure datastore from resource " + debugName, e));
690+
return this; // To please the compiler
696691
}
697692
}
698693
}
@@ -1503,10 +1498,8 @@ public Configuration Configure(Assembly assembly, string resourceName)
15031498
throw new HibernateException("Could not configure NHibernate.", new ArgumentNullException("resourceName"));
15041499
}
15051500

1506-
Stream stream = null;
1507-
try
1501+
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
15081502
{
1509-
stream = assembly.GetManifestResourceStream(resourceName);
15101503
if (stream == null)
15111504
{
15121505
// resource does not exist - throw appropriate exception
@@ -1516,13 +1509,6 @@ public Configuration Configure(Assembly assembly, string resourceName)
15161509

15171510
return Configure(new XmlTextReader(stream));
15181511
}
1519-
finally
1520-
{
1521-
if (stream != null)
1522-
{
1523-
stream.Close();
1524-
}
1525-
}
15261512
}
15271513

15281514
/// <summary>

0 commit comments

Comments
 (0)