wcf - 带 wcf 的 Autofac

标签 wcf dependency-injection autofac single-instance

   builder.Register(c => new ChannelFactory<IBuildingInfoService>
    ("BasicHttpBinding_IBuildingInfoService"))
                .SingleInstance();


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

我在 WCF 客户端的依赖注入(inject)中得到了这些代码行..

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

单实例如何工作?

channel Factory 在内部做什么?

最佳答案

SingleInstance

上面是创建一个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/

相关文章:

wcf - 使用安全模式="TransportWithMessageCredential"测试WCF服务wsHttpBinding

没有异常消息详细信息的 WCF CommunicationException

c# - 依赖注入(inject)机制以提供通用服务接口(interface)的最具体实现

winforms - 如何在 Windows 窗体中使用 DbContext 实现 DI

.net - 重写成员时违反了继承安全规则 : 'Autofac.Integration.WebApi.AutofacWebApiDependencyResolver.BeginScope()'

dependency-injection - Autofac 命名注册构造函数注入(inject)

asp.net-web-api2 - Hangfire 作业成功或失败后,如何执行新作业?

.net - 禁用 "application has stopped working"窗口

java - Google Guice 自定义范围

c# - wcf 将列表作为方法参数传递