📜 ⬆️ ⬇️

Remote reflection in .NET runtime

The author of the article is Serdar Kurbanov SerdarKurbanov , the founder of the team and the leading developer of Telescope.


When using the software, there may be situations when the application cannot be or is undesirable to stop even in the event of a malfunction. In such situations, when using the debugger is not possible, error detection can be a non-trivial task.
The situation may be even more confusing if the application uses multithreading. Determining exactly which of the threads caused the error can help logging, but a more universal solution would be to look inside the running program and see the state of the objects in it without stopping it. One approach to this task could be to use the features of System.Reflection.



')
Solution using Reflection
Consider the use of Reflection for the mentioned problem - a multi-threaded program does not work correctly, and we need the details of how the threads work in this program. To simulate this situation, create a console application in which the two threads are synchronized using System.Threading.ManualResetEventSlim. One of the threads completes execution before the second, and we need to understand which of them is late.

Now we will add a class to our program that will display the properties of synchronization objects using Reflection.



Also add the code that allows you to display the data obtained using Reflection in the browser window on request 127.0.0.1 : 20000



By running the program and requesting the specified address in the browser, we will see the properties of our objects and understand that the first thread has completed its work, but the second has not yet.



Idea development


Using this idea, we made a Telescope application, which you can download from flussig.org . This is a program for remote viewing the values ​​of objects in .NET applications, like the watch window in Visual Studio, but allowing you to remotely view objects without stopping the program. The solution consists of two parts - the TelescopeNode.dll assembly, which is embedded in your project and receives data about objects using Reflection, and the Telescope.exe program, which plays the role of an aggregator of information about monitored objects and sends information to the browser, where you can see the list of objects and their properties.

In the same task about synchronization objects in a multi-threaded application, you can bind to the properties of objects using the following code:



As a result, the properties of the objects in the browser window will be displayed as follows:



Unambiguous philosophy
Modern development tries to use as flexible as possible approaches and tight connections between developers, testers, managers and customers — Agile and DevOps are used (explicitly or implicitly) in most development teams creating products for commercial companies, as this gives advantages in speed of creating products and leads to greater satisfaction of the end users of the software. Flexibility is determined both by the work of managers and by software - it is facilitated by the correct choice of IDE, the covering of program scenarios by unit tests, and continuous integration.
Having created Telescope, we hope to add flexibility to the level of testing and debugging, making it possible to produce an analogue debugging after the release. Telescope can be used as a final line of defense against errors (especially for internal products that are easier to access) - when the program is already running in a release, but monitoring the state of some objects or determining the source of an already committed error is required.

Source: https://habr.com/ru/post/276053/


All Articles