Skip to main content

Disposing WCF service after invoking in .NET

There are so many ways available in .NET to invoke WCF services. In this article I will explain how to dispose after consuming or invoking WCF services.


There are three most commonly used ways to consume a WCF Service in a Service Oriented Application built on the WCF Infrastructure which are as follows:
a) Referencing WCF Service from VS.NET Studio 2010.
b) Executing ServiceModel Metadata Utility Tool (Svcutil.exe) and retrieve metadata from a WCF service and use it to create a WCF proxy that can access the service.
c) Using ChannelFactory class used in advanced scenarios that can be used to create more than one channel(to create multiple endpoint listeners).

There are other approaches which are more advanced and less common to all. One such approach is generating proxy using the Mex service endpoints at runtime and another, worth mentioning is using the discovery API. No matter what approach is being used, the code block must always be executed the Dispose method so that it improves performance and releases memory right away (however, the GC still decides when to collect it).

Problem with invoking the Dispose method is due to the fact that there is no such design restriction in place which ensures the Dispose method always executes. Today, if you invoke Dispose method, you invoke it. If you don’t, you don’t. Well, you know your requirement and you got to execute it at any cost.

So, this ProxyInvoker class is designed to fill this gap using the power of lambda expression, Execute Around Method pattern and ChannelFactory class. Well, there is another approach which is not so clean but it works is using .NET reflection. But this approach much either in a way that you need to specify the method name as string parameter and passing arguments as object[] value. On the other hand, this ProxyInvoker class is really a cool design which does exactly the same thing but works with type safety and performs better. It is a static class which provides two set of public methods that you can use to execute your WCF Service methods. It guarantees that the Dispose method is always executed and the Execute<> methods are all you have it.

First set of methods are defined to configure through configuration file from the serviceModel section:
object Execute (string name, Func invoker)
object Execute (string name, THeader header, Func invoker)

Second set of methods are defined to use explicitly the binding and address objects without a configuration file:
object Execute (Binding binding, EndpointAddress address, Func invoker)
object Execute (Binding binding, EndpointAddress address, THeader header, Func invoker)

Either case you need to write a code block that matches to Func delegate. And that’s where you are executing your type safe WCF methods.
The following code snippet shows the examples:
ProxyInvoker.Execute (configName, (proxy) => { return proxy.Eecho(“Hello, World!”); }); ProxyInvoker.Execute(binding, address, (proxy) => { return proxy.Eecho(“Hello, World!”); });

Finally, this is how the Execute<> method makes sure that dispose is invoked always:

try
{
if (header == null)
{
return invoker.Invoke((TContract)proxy);
}
using (OperationContextScope ocs = new OperationContextScope((proxy as IContextChannel)))
{
OperationContext.Current.OutgoingMessageHeaders.Add( MessageHeader.CreateHeader(headerName, headerNamespace, header)); returninvoker.Invoke((TContract)proxy);
}
}
finally
{
if
(proxy != null) { proxy.Dispose(); }
}

Comments

Popular posts from this blog

C# Generic Factory

Implement Factory pattern using generics     public interface IDoWork   {       string DoWork();   }     Declare an Interface first by abstracting the common  functionality    Here I am taking the example of DoWork     public class Manager : IDoWork   {     public string DoWork()     {         return "Manager Manages School" ;     }   }     Implement the IDoWork in concrete classes as shown      public class Teacher : IDoWork     {         public string DoWork()         {             return "Teacher teaches student in school" ;         ...

How to enable windows authentication in PostgreSQL

1.     Steps to create user or role in PostgreSQL ·         Open pgAdmin III ·         Login to PostgreSQL database ·         Select “Login Roles” and right click on mouse to view the context menu ·         Select “New Login Role” from context menu ·         Enter desired user name in “Role name” text box ·         Select “Definition” tab in “New Login Role” window ·         Enter desired Password in the given text box ·         Select “Role privileges” tab in “New Login Role” window ·         Select the privileges for the entered user or role ·         Select “SQL”...

UML - Association, Aggregation, Composition, Generalization, Specialization, Realization and Dependency

Association Association is a simple relationship between two classes. For example A relationship between Professor Class and Student class is known as Association. Both Classes can exist without each other, so Professor and Student are two independent classes. In this kind of relationships there will not be any owner class. Both classes have their own life cycle. UML Notation:     Aggregation Aggregation is a special type of Association. It is known as “Has-A” relationship. For example A Department class can contain Professor Class. Here Department class is owner class. Here in this relationship even after deleting Department class, Professor Class can exits. UML Notation: Composition Composition is a special type of Aggregation. It is known as “Is-A” relationship. For example A University Class has many Department class. Here University and Department objects are dependent on each other. If we delete Univ...