In this article we will discuss about the differences between strong reference and weak reference in .NET Projects and will also learn about strong name key or public key token.
Strong Reference and Weak Reference
.NET fundamentals is an ocean in itself there are many essential topics that are must learning while some are of worth knowing it. Here today we will see something from
GAC(Global Assembly Cache). When we talk of
GAC then it means we are dealing with
assembly and DLL(Dynamic Library Link). In this article we will see differences between strong reference and weak reference also we will learn about strong name key or public key token. We will see practical’s of strong reference and weak reference to get clear understanding of the topic.
For that below is a simple project where we have class library(DLL) and a windows client(Windows Application) . Under a class library we have a simple call back method named “CallMeBack()” and this class library is consumed by windows client with an event action when “button” is clicked by displaying message present under CallMeBack() method.
In order to consume ClassLibrary we need to add it as “reference” in Windows Application then add it’s namespace with syntax “using ClassLibrary1” under Form1.cs of Windows Application. Thereafter create object named “cllib” of “Class1” present in “ClassLibrary1” so that method “CallMeBack” can invoked which will display text “I am the actual class” on message box when button clicked.
Now we will look into weak reference and strong reference for that first copy existing project code from project folder to new location, here location used in “D:\CSharp\StrongWeakRef”
After you copy and paste “ClassLibrary.dll” and “WindowsFormApplication1.exe” just do double click and run “WindowsFormApplication1.exe” file and check whether you see correct output which is by displaying message “I am the actual class” on message box as shown in the figure down below.
As output is seen clearly now if we ship our files to any environment and also if you do the deployment of it will work perfectly fine.
Now suppose if tomorrow some known person or internal developer if tries to change or alter your .DLL file from actual one and replaces it with his file, then that can be easily achieved by him. As knowing inner details of “ClassLibrary1.dll”like function or method name and also data types used in .DLL file can be done by ILDASM tool.
This is big issue without your concern the actual .DLL is replaced by someone code which is written by himself. This is because our client(windows application) is not tied up with .DLL(Class Library) file due to which client has no idea with which .DLL file it is working? Is .DLL published by the original publisher or not. In short it can be easily manipulated because it only knows the name of .DLL and within it is only concern with namespace, class, function or method name and if found correct those things it works fine with it.
This is termed as Weak reference where client is identifying .DLL and assembly by it given name and internally with namespace, class and function or method names only.
So now question is how to prevent this from happening?
This can be done by assigning Strong Name to the .DLL or assembly which is called as Strong reference. With use of Strong Name client will be able to identify original publisher and issue of weak reference will be resolved. Now client will identify .DLL or assembly with strong name and other internal details where strong name is unique across all and same cannot be created by anyone.
How to assign Strong Name to .DLL practically?
- For that go on Visual Studio under Solution Explorer do a right click on “ClassLibrary1”.
- Then click on Properties.
Once the Properties screen is open on that do the following: -
- Click “Signing” option available on the left of Properties window.
- Do a check mark on the tick option of “Sign the Assembly”.
- From the dropdown select “<
New...>” which will open “Create Strong Name Key” window.
- Under new window give suitable name to key “MyStrongName”.
- Do uncheck to create key file without password.
- Click OK to close the “Create Strong Name Key” window.
Once clicked OK it will create a file with name “MyStrongName.snk” which will be associated with .DLL. Now there will be a strong bonding between client and .DLL file using this key file.
Now rebuild the solution again and then copy "ClassLibrary1.dll" and "WindowsFormsApplication1" file from existing project location to new folder location “StrongWeakRef”.
Run .EXE file and see whether new output is seen with “I am the actual strong class” message displayed on the message box.
Now enable altered .DLL which is named to “ClassLibrary1altered.dll” to “ClassLibrary1.dll” and then run .EXE client "WindowsFormsApplication1". It will open “Form1” and then click “button1” to see output it will throw error. If you read error message which states that client is not able to match up strong name key of altered .DLL. “MyStrongName.snk” is strong name key of strong .DLLassociated with client "WindowsFormsApplication1".
With the above demonstrated practical hope that difference between weak reference and strong reference along with strong name key is clearly understood by the reader.
Also see an interesting article which is referred to topic strong reference called delay signing.
Following is the video from below fresher’s C# project series which will be helpful to the one who is new to C# programming language: -