【问题标题】:Autofac with wcf带有 wcf 的 Autofac
【发布时间】:2017-10-08 14:21:11
【问题描述】:
   builder.Register(c => new ChannelFactory<IBuildingInfoService>
    ("BasicHttpBinding_IBuildingInfoService"))
                .SingleInstance();


    builder.Register(c => c
    .Resolve<ChannelFactory<IBuildingInfoService>>().CreateChannel())
               .As<IBuildingInfoService>()
               .UseWcfSafeRelease();

我在 WCF 客户端的依赖注入中获得了这些代码行。

有人能解释一下它是如何工作的吗?

单实例如何工作?

channel Factory 在内部做什么?

【问题讨论】:

    标签: wcf dependency-injection autofac single-instance


    【解决方案1】:

    SingleInstance

    上面是创建一个Singleton。每次请求时都会得到相同的实例。

    There are different ways to create a WCF Client and Channel Factory is one of them。 Channel Factory 类用于在客户端和服务器之间构建通道,无需创建代理。

    当您创建通道工厂时 - 它会在内部调用 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();
                    } 
                }
            }
        }
    

    【讨论】:

    • 但是我如何测试它并检查返回的两个实例是否相同。因为当我测试背靠背解析两个实例并将它们与 == 运算符进行比较时,我得到了错误的结果。
    • @Abi 这与您发布的问题不同。话虽如此,您可以尝试 Object.ReferenceEquals msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多