我有一个调用一些WCF服务的MVC4应用程序。
我读到了使用“using”语法(read this...)处理WCF代理时可能发生的一些问题。 我正在尝试在我的解决方案(...and now this)中正确实现WCF代理处理。
标准请求通过应用程序中的以下层运行:
MVC控制器> “主要立面”> “Minor Facades”> WCF代理(遗留代码。不是我的错)
我正在使用Ninject 3.2.2.0自动将依赖项注入这些层。例如:
控制器:
public class HomeController : Controller
{
private readonly SomeMainFacade _someMainFacade;
public HomeController(SomeMainFacade someMainFacade)
{
_someMainFacade = someMainFacade;
}
...
}
“主要立面”:
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;
}
...
}
我想妥善处理“小门面”中使用的WCF服务,但如果没有大的重构,我想不出这种方法。也许Ninject来救援......
感谢任何帮助。
感谢。
答案 0 :(得分:0)
这是我很久以前写的一堂课,位于遥远的星系中:
/// <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
}
然后基本上你在某处创建一个静态ChannelFactory(我想这是传递给你的minorfacade的东西),然后调用RemotingClient。像这样:
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();
}
}