You create a channel by calling create ChannelFactory. Behind the scene when you say create channel, WCF will actually build the underline WCF runtime for transferring a message on the wire to the service. It returns you an object and implement the service contract type. When you do method calls, these method calls are translated into message on the wire through that underline channel stack. Now, all channels will also implement another interface called IClientChannel. This is a special interface in the channel life cycle. This will provide both - Close and Abort method you can use when you need to shut things down.
Let me take this example :
Here I have a ChannelFactory of type IInvoiceService as my service contract type which is targeting tcpEndpoint defined in web.config file.
To create a ChannelFactory I will simply call :
Let me take this example :
Here I have a ChannelFactory of type IInvoiceService as my service contract type which is targeting tcpEndpoint defined in web.config file.
To create a ChannelFactory I will simply call :
IInvoiceService channel = factory.CreateChannel();
This will give me an object back of IInvoiceService. Then, I actually want to send a message to the service that I will send by simply calling a method on the service contract type :
channel.SubmitInvoice(invoice);
And when I am done with my this channel, I can cast the channel object to IClientChannel and then call close method :
((IClientChannel)channel).Close();
Difference between Close and Abort
Close :
1. Close performs graceful shutdown of the client channel
2. It waits for In-Progress calls to complete before closing things down
3. It closes underlying n/w resources
4. It doesn't close the channel before all the operations are completed
5. Close can throw few Exceptions (Communication Exception & Timeout Exception)
Abort:
1. It doesn't wait for anything to happen
2. When you are calling Abort, system assumes that you want to abort In-Progress calls & want to close the channel immediately.
3. You call abort in a situation where something has gone wrong and you just need to clean the things as soon as possible.
4. Typically you call abort when you have "faulted" channels. In fact, you must call abort in this situation. If you try to call close on "faulted" channel, that will actually throw an another exception.
Both close & abort are found on IClientChannel interface and that interface will be available to all the channel objects returned by your ChannelFactory.
Note : You have to cast to IClientChannel inorder to call close/abort.