I have a MVC4 application that calls some WCF services.
I read about some problems that could happen when disposing WCF proxies with "using" syntax ( read this... ). I am trying to implement WCF proxy disposal properly in my solution ( ...and now this ).
A standard request runs through the following layers in the application:
MVC Controller > "Main Facade" > "Minor Facades" > WCF proxies (Legacy code. Not my fault)
I am using Ninject 3.2.2.0 to automatically inject the dependencies into these layers. For example:
Controller:
public class HomeController : Controller
{
private readonly SomeMainFacade _someMainFacade;
public HomeController(SomeMainFacade someMainFacade)
{
_someMainFacade = someMainFacade;
}
...
}
"Main Facade":
public class SomeMainFacade
{
private readonly MinorFacade1 _minorFacade1;
private readonly MinorFacade2 _minorFacade2;
public SomeMainFacade (
MinorFacade1 minorFacade1,
MinorFacade2 minorFacade2
){
_minorFacade1 = minorFacade1;
_minorFacade2= minorFacade2;
}
...
}
"Minor Facades"
public class MinorFacade1
{
private readonly IWCFService _wcfServiceClient;
public MinorFacade1(IWCFService wcfServiceClient)
{
_wcfServiceClient= wcfServiceClient;
}
...
}
I'd like to dispose the WCF services used in the "minor facades" properly, but I cannot think of a way of this without a big refactoring. Maybe Ninject comes to the rescue...
Any help is appreciated.
Thanks.
This is a class I wrote a long long time ago, in a galaxy far far away:
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class RemotingClient<T> : IDisposable
{
private bool _disposed;
private readonly ChannelFactory<T> _factory;
/// <summary>
///
/// </summary>
public event OnCloseChannel ChannelClosed;
public event OnFaultedChannel ChannelFaulted;
/// <summary>
///
/// </summary>
public delegate void OnCloseChannel();
public delegate void OnFaultedChannel();
/// <summary>
///
/// </summary>
/// <param name="factory"></param>
public RemotingClient(ChannelFactory<T> factory)
: this(factory, SynchronizationContext.Current)
{
}
private void OnClose(object sender, EventArgs e)
{
if (null != ChannelClosed)
ChannelClosed();
}
private void OnFaulted(object sender, EventArgs e)
{
if (null != ChannelFaulted)
ChannelFaulted();
}
/// <summary>
///
/// </summary>
/// <param name="factory"></param>
/// <param name="context"></param>
public RemotingClient(ChannelFactory<T> factory, SynchronizationContext context)
{
Context = context;
_factory = factory;
CreateNewChannel();
}
/// <summary>
///
/// </summary>
/// <param name="uiCallback"></param>
public void PostToContext(SendOrPostCallback uiCallback)
{
Context.Post(uiCallback, null);
}
public void CreateNewChannel()
{
Channel = _factory.CreateChannel();
ICommunicationObject c = Channel as ICommunicationObject;
if (null == c)
throw new ArgumentException(
typeof(T) + " Can not be used as an ICommunicationObject");
c.Closed += OnClose;
c.Faulted += OnFaulted;
c.Open();
}
/// <summary>
///
/// </summary>
public SynchronizationContext Context
{ get; private set; }
/// <summary>
///
/// </summary>
public T Channel
{ get; set; }
public CommunicationState ChannelState
{
get
{
ICommunicationObject c = (ICommunicationObject)Channel;
return c.State;
}
}
#region IDisposable Members
/// <summary>
///
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
Close();
this._disposed = true;
}
}
public void Close()
{
ICommunicationObject c = Channel as ICommunicationObject;
if (c.State == CommunicationState.Faulted)
c.Abort();
c.Close();
}
#endregion
}
then basically you create a static ChannelFactory somewhere (I imagine this is what would be passed to your minorfacade) and then call RemotingClient. Like so:
public class MinorFacade1
{
private static ChannelFactory<IMyServiceContract> MyFactory{ get; set; }
public MinorFacade1(ChannelFactory<IMyServiceContract> factory)
{
MyFactory = factory;
}
public void DoSomeAwesomeWcfCall()
{
using(RemotingClient<IMyServiceContract> proxy = new RemotingClient<IMyServiceContract>(MyFactory)
proxy.Channel.ThisIsAnAwesomeWcfCall();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.