builder.Register(c => new ChannelFactory<IBuildingInfoService>
("BasicHttpBinding_IBuildingInfoService"))
.SingleInstance();
builder.Register(c => c
.Resolve<ChannelFactory<IBuildingInfoService>>().CreateChannel())
.As<IBuildingInfoService>()
.UseWcfSafeRelease();
我在 WCF 客户端的依赖注入(inject)中得到了这些代码行..
有人能解释一下它是如何工作的吗?
单实例如何工作?
channel Factory 在内部做什么?
最佳答案
上面是创建一个Singleton .您每次请求时都会获得相同的实例。
There are different ways to create a WCF Client and Channel Factory is one of them . Channel Factory 类用于在不创建代理的情况下在客户端和服务器之间构建 channel 。
当您创建 channel 工厂时 - 它会在内部调用 Open
。
你可以看到 source code here如果深入研究,CreateChannel
最终会调用 EnsuredOpen
。
protected void EnsureOpened()
{
base.ThrowIfDisposed();
if (this.State != CommunicationState.Opened)
{
lock (this.openLock)
{
if (this.State != CommunicationState.Opened)
{
this.Open();
}
}
}
}
关于wcf - 带 wcf 的 Autofac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43883934/