menu

Sunday, October 9, 2016

Signing - Strong vs Weak Reference

Prerequisite - Create 2 solution first one (Solution1) with 2 project (console - ConsoleApp and class library - CLApp) another (Solution2) with one project (class library - CLApp).
Create a class with same name (say MyClass) and function (say MyFun) in both class library and print message like "Test1" and "Test1" respectively. Now in Solution1 add the reference of class library to console app and inside main function call MyClass.MyFun(), build and copy ConsoleApp.exe and CLApp.dll in a new folder and run the ConsoleApp.exe and verify the output (Test1) on console.

Problem - Build Solution2 and copy the CLApp.dll to newly created folder and replace the old CLApp.dll of Solution1. Run the ConsoleApp.exe and verify the output (Test2) on console. The problem here is that ConsoleApp.exe is not strongly referenced with CLApp.dll and this is not safe at all because at anytime one who know about the class and function can change its internal logic and that will replace its behaviour. This is because of weak reference.

Solution - Go to property of class library project in Solution1 and sign in with a MyTest.snk file

and then refer it to console app (this will be a strong reference) and follow the same procedure of replacing the dll of Solution2 and run the app. This time it will throw exception saying that ...
Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Signing-StrongAndWeakReferenceDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9498fd7f75415bf8' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT:0x80131040) at Signing_StrongAndWeakReference.Program.Main(String[] args)


No comments:

Post a Comment