Skip to content

Commit 49ce1dd

Browse files
ngbrownhazzik
authored andcommitted
NH-3807 - Switch stream's over to using statements.
Stream.Close() is removed from CoreClr.
1 parent abcbb6f commit 49ce1dd

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
@@ -2829,15 +2829,16 @@ public void PersistCollections()
28292829

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

2836-
s.Close();
2837+
s.Close();
28372838

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

28432844
s.Reconnect();
@@ -2879,14 +2880,15 @@ public void PersistCollections()
28792880

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

2885-
s.Close();
2887+
s.Close();
28862888

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

28922894
Qux nonexistentQux = (Qux) s.Load(typeof(Qux), (long) 666); //nonexistent
@@ -4687,17 +4689,18 @@ public void ProxyArray()
46874689

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

4694-
// close the original session
4695-
s.Close();
4697+
// close the original session
4698+
s.Close();
46964699

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

47034706
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
@@ -426,13 +426,15 @@ public void Performance()
426426
[Test]
427427
public void Serialize()
428428
{
429-
MemoryStream stream = new MemoryStream();
430-
BinaryFormatter f = new BinaryFormatter();
431-
f.Serialize(stream, _shm);
432-
stream.Position = 0;
429+
SequencedHashMap shm;
430+
using (MemoryStream stream = new MemoryStream())
431+
{
432+
BinaryFormatter f = new BinaryFormatter();
433+
f.Serialize(stream, _shm);
434+
stream.Position = 0;
433435

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

437439
Assert.AreEqual(3, shm.Count);
438440
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)