[Video] Learn Garbage Collector (Part -1) : Object Creation and it’s life cycle

Hello All,

Garbage Collector is one the basic concepts in .NET framework and ironically one of most misunderstood concepts as well. Also, It is also one of the most asked interview questions. To understand the whole concept, one need to understand Object creation and it’s life cycle life cycle and memory management from the application point of view.

I am going to post a series of videos on Garbage Collector and hope this will help you in learning the basics of Garbage Collector. In my first video, I have talked on Object creation and about it’s life cycle.

Hope you’ll enjoy the video and don’t forget to share your feedback.

Cheers,
Brij

Advertisement

How to Override Finalize method in C#

Being a .NET Developer, you must have basic information about Garbage Collector. Garbage Collector is a boon for .Net developers which takes care of memory management for .NET programs in the background.

But as we know that there are some limitations as well. Garbage collector can collect only managed object. So what about unmanaged objects. Garbage collector cannot clean up unmanaged object properly because these object are not limited to .NET framework and CLR does not have complete control on it. So it is developer’s responsibility to clean up unmanaged resources. It is advised to override the Finalize method (that is a virtual method in Object class) to clean up the unmanaged resources. Garbage collector calls the finalize method of each object (which has overridden Finalize method) while collection process.

So have you ever tried to override the Finalize method in C#? Let’s do that

 public class MyClass
    {
        protected override void Finalize()
        {
            // Do unmanaged resource clean up
        }
    }

For simplicity, I just override the Finalize method. Now let’s build the code.
Ohh.. The above code does not build. It gives the following errorErrorSo if you see the error, it says that do not override Finalize. Instead use destructor.

It means We cannot override Finalize directly in C#.

So what is other way? As in error it suggests to provide destructor. So let us write the destructor (destructor in any class starts with tilde (~)). So now my class looks like

    public class MyClass
    {
        ~MyClass()
        {
            // Do unmanaged resource clean up

            Console.WriteLine("In destructor");
        }
    }

Now let’s build it.
It builds successfully. Now let’s see this class using assembly Reflector.
destuctorSo here our destructor turned into Finalize method. So it means that destructor and Finalize are same in C#. But obviously while writing code in C#, it does not allow to override the Finalize method so we have only option to write destructor for that.

So don’t get confused ever if you see somewhere that say to override Finalize and you are not able to override it. Instead use destructor for that purpose.

Cheers,
Brij

How to consume WCF REST services using C#

In this Post, we’ll see how can we easily write a code for consuming WCF Rest services. There could be many other scenarios where we need to write a test Client application to test WCF Rest services. In this post, we will consume two type of HTTP methods GET and POST. Let’s see one by one

Consume Get Method
Here, We can have a WCF Rest service which is deployed somewhere on web server or at IIS on local machine or even uses Visual Studio integrated web server. In my sample, I am using Visual Studio one. And the URL of that service is

http://localhost:26944/Service1.svc/User/GetAllUsers

By Default Get method’s input and output parameters are of type XML(default) or JSON and returned in string format. Here the method returns in JSON format. Let’s write the sample

So what all steps we need to perform

1- Create a HTTPWebRequest object with the provided REST service URL

2- Initiate the request and get response

3- Read the stream from the response

4- Serialize the stream and read the result

Let’s jump to the code

GetCodeSo as each line of code is also commented as mentioned in the step. Let’s run it as well

So it is fairly easy to pass a parameter in Get method. It is passed via URL only so it can of type string only.

Also, the REST Services exposed via GET method, can be called and Tested via browser. So to test it via browser, you should browse the complete URL (service url including method name or as URI defined while exposing). Here the method takes no parameter and returns a list of object of custom type.

Now Let’s move to Post method

Consume Post Method

It is not as straight forward as above one. As we know that in the Post method the input is passed in body of request and that should be properly serialized as well. As we know that WCF uses it’s own serialization named as System.Runtime.Serialization so it should be used for serializing the input. If you are using XmlSerializer or other then you might face issue.

Here in my service method, it is taking a custom object type parameter and returning same type as well. The REST method signature looks like

[WebInvoke(Method = "POST", UriTemplate = "/User/GetAllUsersInfo")]
UserInformation GetUserDetails(UserInformation user);

And the service URL would be like

http://localhost:26944/Service1.svc//User/GetAllUsersInfo

Here we need to have some more steps while calling this service. the steps are like

1- Create a HTTPWebRequest object with the provided REST service URL.

2- Provide the method type and Content Type to the request.

3- Create the input object and serialize it and then assign that stream the request.

4- Initiate the request and get response.

5- Read the stream from the stream and deserialize the stream into the object

Let’s see the code

PostThe code is self explanatory and commented as well .Here some of the key things to keep in mind that How the input object is serialized and assigned to the body of the request and similarly reading the response from the response. The code is

Hope you all have enjoyed the post. Thank you all.

Cheers,
Brij