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
MqttClientinstance in theApplicationobject. This keeps the instance alive from when the AppPool starts (instantiate the client in theApplication_Startevent inGlobal.asax. It is fired when the AppPool starts) and until it shuts down (Application_End, where you get the opportunity to shut yourMqttClientdown gracefully if you want/need to). It is shared between all users and can be accessed anywhere withApplication[key]wherekeyis any string,"MqttClient"for example. - You can keep it in the
Sessionobject the same way you would in theApplicationobject. You can useSesson_StartandSession_Endin the same way. TheSessionobject is unique to each user, in terms of it staying alive until the user stops browsing your website. - You can instantiate
MqttClientevery time you need it or with everyPage_Load.
0
solved Mqtt and asp.net web application, need assistance