It seems to be a lack of understanding of the ASP.NET page lifecycle.
In WPF, the lifecycle spans from when you run the program til when it is closed; it is statefull. ASP.NET is not; whatever you do in a Page_Load
(and the other lifecycle events) will be disposed with the completion of rendering the page.
You have a few ways of solving your problem.
- You can keep the
MqttClient
instance in theApplication
object. This keeps the instance alive from when the AppPool starts (instantiate the client in theApplication_Start
event inGlobal.asax
. It is fired when the AppPool starts) and until it shuts down (Application_End
, where you get the opportunity to shut yourMqttClient
down gracefully if you want/need to). It is shared between all users and can be accessed anywhere withApplication[key]
wherekey
is any string,"MqttClient"
for example. - You can keep it in the
Session
object the same way you would in theApplication
object. You can useSesson_Start
andSession_End
in the same way. TheSession
object is unique to each user, in terms of it staying alive until the user stops browsing your website. - You can instantiate
MqttClient
every time you need it or with everyPage_Load
.
0
solved Mqtt and asp.net web application, need assistance