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


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

I have got these lines of code in dependency injection for WCF client..

Can somebody explain how does it work.. ?

How does single instance work ?

What is channel Factory doing internally ?

Abi
  • 283
  • 3
  • 16

1 Answers1

2

SingleInstance

The above is creating a Singleton. You will get the same instance every time you request it.

There are different ways to create a WCF Client and Channel Factory is one of them. The Channel Factory class is used to construct a channel between the client and server without creating a proxy.

When you create a channel factory - it calls Open internally.

You can see the source code here and if you dig into it, CreateChannel eventually calls EnsuredOpen.

    protected void EnsureOpened()
    { 
        base.ThrowIfDisposed();
        if (this.State != CommunicationState.Opened)
        {
            lock (this.openLock) 
            {
                if (this.State != CommunicationState.Opened) 
                { 
                    this.Open();
                } 
            }
        }
    }
William Xifaras
  • 5,212
  • 2
  • 19
  • 21
  • But how can i Test it and check whether two instances returned are the one and the same.. because when i tested resolving two instances back to back and compared them with == operator i got false result. – Abi May 11 '17 at 05:02
  • @Abi That's a different question than the one you posted. That being said, you can try Object.ReferenceEquals https://msdn.microsoft.com/en-us/library/system.object.referenceequals(v=vs.110).aspx – William Xifaras May 11 '17 at 12:56