Skip to content

Commit a922bb6

Browse files
committed
Dispose the dependencies after OperationCompleted rather than BeforeReply
1 parent fb54da9 commit a922bb6

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/Examples/WcfRestService/IRepository.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
namespace WcfRestService
2323
{
24+
using System;
2425
using System.Collections.Generic;
2526

2627
/// <summary>
2728
/// The interface for a sample repository
2829
/// </summary>
29-
public interface IRepository
30+
public interface IRepository : IDisposable
3031
{
31-
List<SampleItem> GetCollection();
32+
IEnumerable<SampleItem> GetItems();
3233
}
3334
}

src/Examples/WcfRestService/Repository.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,30 @@
2222

2323
namespace WcfRestService
2424
{
25+
using System;
2526
using System.Collections.Generic;
27+
using System.Diagnostics;
2628

2729
/// <summary>
2830
/// An implementation of IRepository
2931
/// </summary>
3032
public class Repository : IRepository
3133
{
32-
public List<SampleItem> GetCollection()
34+
private bool isDisposed = false;
35+
36+
public void Dispose()
3337
{
34-
return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
38+
this.isDisposed = true;
39+
}
40+
41+
public IEnumerable<SampleItem> GetItems()
42+
{
43+
if (this.isDisposed)
44+
{
45+
throw new ObjectDisposedException(nameof(Repository));
46+
}
47+
48+
return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
3549
}
3650
}
3751
}

src/Examples/WcfRestService/Service1.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace WcfRestService
2323
{
2424
using System.Collections.Generic;
25+
using System.Linq;
2526
using System.ServiceModel;
2627
using System.ServiceModel.Activation;
2728
using System.ServiceModel.Web;
@@ -48,11 +49,14 @@ public Service1(IRepository repository)
4849
}
4950

5051
[WebGet(UriTemplate = "")]
51-
public List<SampleItem> GetCollection()
52+
public IEnumerable<SampleItem> GetItems()
5253
{
5354
// We moved the template generated functionality for GetCollection() to the Repository class which
5455
// implements the IRepository interface
55-
return this.repository.GetCollection();
56+
return from i in this.repository.GetItems()
57+
// use this.repository again to test it is not disposed.
58+
let j = this.repository.GetItems()
59+
select i;
5660
}
5761

5862
// The other methods automatically generated by the project template were deleted since only one

src/Ninject.Extensions.Wcf/WcfRequestScopeCleanup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void BeforeSendReply(ref Message reply, object correlationState)
6969
if (this.releaseScopeAtRequestEnd)
7070
{
7171
var context = OperationContext.Current;
72-
this.MapKernels(kernel => kernel.Components.Get<ICache>().Clear(context));
72+
context.OperationCompleted += (s, e) => this.MapKernels(kernel => kernel.Components.Get<ICache>().Clear(context));
7373
}
7474
}
7575
}

0 commit comments

Comments
 (0)