# Static Service Management

### Problem&#x20;

One important challenge in the upgrade from Desktop to Web application is the handle of the static variables, in this scenario, the Desktop application only have one scope, if you execute multiple instances of the same application each one will have its own context, in Web application there are different scenarios.&#x20;

For example, in Blazor WebAssembly deploy it will create one scope per tab or SignalR connection, so it won’t require any change given is a similar execution mode.&#x20;

In Blazor Server we only have a server-client architecture so, It'll be the same server handling different connections, the scope of the static variables are global, so if you as a user open different tabs, they’ll share the same static variables and values, not per session/tab as expected. As we can see in the following diagram the life cycle of a static variable will persist during the whole server execution.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdcYJlqWiRnRqIy63qibFB9eQdX2NoFR4L-3t0divjNl8MT5OWrRS6OYOCBpztCwCdwrQb6Zp8vxgoIBwzCWYmztMt_XHgZQxmeOdrOJWAaZegyns7r67nLO9KlIn7297vJ8J74_ejQ_8LUJwFTkmNc7xZc6PCH_K3OMeHIkA?key=DoxXxo4EatJbUmEXDRwtbQ" alt=""><figcaption><p>Static scope and service scope</p></figcaption></figure>

### Solution

After some research of different approaches a StaticService is implemented, this service is global and will store the value of the static variable per session, handling by itself the session and the storage. It will give us the following methods:

* Get which will return the value stored in the service for this static variable.
* Set will set the value into the service for this static variable.
* InitializeData, when we have a static property auto-implemented in the source code with a default value, we need to keep the value, but C# doesn’t allow non-auto-implemented properties to be initialized, so this method will set the corresponding value to the property in the first call on Get.

This service will involve changes on the conversion of the static variables that we’ll review in the following section.

**Conversion Changes**

This feature implies that we need changes in the converted code, principally on the getters and setters, each one will require to call the methods. One important decision we made during the design of this feature is to keep the code visible instead of using some attribute in order to increase the visibility and the maintainability of the converted code. Here we have a basic example with no initialization:

Winforms:

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdb6HvpjMwPcdZ-ngzZLVG-A2Ba6wEBOk1_uPhvFcQlRQYIEZpb2AGeA6eNw-KR1VbxEd4DTPwoUSfWtr0F_MDsZLDuv7vsV9YLOdvYDgoskSygHg4PDLmjxL6HvibZkD6L-g7MoLOOm54m53kcFNKy5g0eMJq22TEM5yQY-A?key=DoxXxo4EatJbUmEXDRwtbQ" alt=""><figcaption></figcaption></figure>

Blazor:

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdoUOjbZ55fbE3ETp4t42FLSv1mgMV4JFcqeqw_wCsHk2rVuWXsru-xt-MlkQhQoc6mdowEA89hKJ8FFPpTXNrP9B4JxRIOjfadWbcV33ULIRRSsg4iSr3CCWDCJDo-UTdkKf3uYVJj2jZ_kUzVHXsWaWBGhRfhYF4a4XNN8Q?key=DoxXxo4EatJbUmEXDRwtbQ" alt=""><figcaption></figcaption></figure>

There is another example when an initialization is set on the static variable.

Winforms:

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdm1c2-0SCRkGB4H4KM9Hm6J_csy-cqmzbIqvJ6eIWsPdD52FNkDjs_gdt0h7qNd4-P8vkt4ArIEOEQfLUz42wyZlCkRRNHKgafmeKfK27FDVg97WJsuLxog3QcYs5UhhqCuD7RxjMNT2kRQcgolFK5UeZr2kM5Zu2hckQhGw?key=DoxXxo4EatJbUmEXDRwtbQ" alt=""><figcaption></figcaption></figure>

Blazor:

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf34YKhMbPHrxntj7KvvVKQkwluwd4XXvpRqwMQQ4lAYe1O3eI4j0UeCeECIBmlg_0OM0-Mk4y4xiEumLXtovw367fBYhE--EcV_bRAvZZXOEuweK-ZDczlk8YkyxRbs-xcrfWJldtFE9nfGT5bwVXfgybYKX7qeOpyS-q98w?key=DoxXxo4EatJbUmEXDRwtbQ" alt=""><figcaption></figcaption></figure>

**Service Setup**

The StaticService requires some configuration in the program of our new application, is necessary to add the Session, DistribuitedMemoryCache service, HttpContextAccessor and initialize the services as we can see in the following code:

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdKZ6_e1OVDf3fKWCvUKhLozK0GrMlHkavo0hvG2h4EBRngDc5IYybsLQkS0s70sFQPj3PQ2X5qY2zFQgYdZExymYv3t16lO9N9uu1zEoHUTPTAFmOJGCXAoED_8skzpNQ0iFGF-C0vVxueahILjOfUvMa2Bzj5EvaqrrN5Xg?key=DoxXxo4EatJbUmEXDRwtbQ" alt=""><figcaption></figcaption></figure>

These services are required to be provided to the StaticServices to handle the different sessions and identify which one is the current session that is being consulted.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gapvelocity.ai/webmap-for-blazor-documentation/blazor-ai-migrator/conversion-tool/static-service-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
