Skip to content

Commit

Permalink
Dispose the dependencies after OperationCompleted rather than BeforeR…
Browse files Browse the repository at this point in the history
…eply
  • Loading branch information
scott-xu committed Jun 12, 2021
1 parent fb54da9 commit a922bb6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Examples/WcfRestService/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

namespace WcfRestService
{
using System;
using System.Collections.Generic;

/// <summary>
/// The interface for a sample repository
/// </summary>
public interface IRepository
public interface IRepository : IDisposable
{
List<SampleItem> GetCollection();
IEnumerable<SampleItem> GetItems();
}
}
18 changes: 16 additions & 2 deletions src/Examples/WcfRestService/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,30 @@

namespace WcfRestService
{
using System;
using System.Collections.Generic;
using System.Diagnostics;

/// <summary>
/// An implementation of IRepository
/// </summary>
public class Repository : IRepository
{
public List<SampleItem> GetCollection()
private bool isDisposed = false;

public void Dispose()
{
return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
this.isDisposed = true;
}

public IEnumerable<SampleItem> GetItems()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof(Repository));
}

return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
}
}
}
8 changes: 6 additions & 2 deletions src/Examples/WcfRestService/Service1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace WcfRestService
{
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
Expand All @@ -48,11 +49,14 @@ public Service1(IRepository repository)
}

[WebGet(UriTemplate = "")]
public List<SampleItem> GetCollection()
public IEnumerable<SampleItem> GetItems()
{
// We moved the template generated functionality for GetCollection() to the Repository class which
// implements the IRepository interface
return this.repository.GetCollection();
return from i in this.repository.GetItems()
// use this.repository again to test it is not disposed.
let j = this.repository.GetItems()
select i;
}

// The other methods automatically generated by the project template were deleted since only one
Expand Down
2 changes: 1 addition & 1 deletion src/Ninject.Extensions.Wcf/WcfRequestScopeCleanup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void BeforeSendReply(ref Message reply, object correlationState)
if (this.releaseScopeAtRequestEnd)
{
var context = OperationContext.Current;
this.MapKernels(kernel => kernel.Components.Get<ICache>().Clear(context));
context.OperationCompleted += (s, e) => this.MapKernels(kernel => kernel.Components.Get<ICache>().Clear(context));
}
}
}
Expand Down

0 comments on commit a922bb6

Please sign in to comment.