【问题标题】:Mqtt and asp.net web application, need assistanceMqtt 和 asp.net web 应用,需要帮助
【发布时间】:2017-03-22 07:26:35
【问题描述】:

我无法使用 mqtt 将 wpf 转换为 asp.net。我的代码没有显示任何错误,但是当我启动并输入一些文本并单击按钮时,它会显示一个错误 "在 WebApplication4.dll 中发生了 'System.NullReferenceException' 类型的异常,但未在用户代码中处理"

public partial class Testing : System.Web.UI.Page
    {
        MqttClient client;
        string clientId;
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public void MainWindow()
        {


            string BrokerAddress = "test.mosquitto.org";

            client = new MqttClient(BrokerAddress);

            // register a callback-function (we have to implement, see below) which is called by the library when a message was received
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

            // use a unique id as client id, each time we start the application
            clientId = Guid.NewGuid().ToString();

            client.Connect(clientId);
        }




        void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            string ReceivedMessage = Encoding.UTF8.GetString(e.Message);


                txtReceived.Text = ReceivedMessage;

        }

    protected void btnPublish_Click(object sender, EventArgs e)
            {
                if (txtTopicPublish.Text != "")
                {
                    // whole topic
                    string Topic = "" + txtTopicPublish.Text + "";

                    // publish a message with QoS 2
                    client.Publish(Topic, Encoding.UTF8.GetBytes(txtPublish.Text), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('You have to enter a topic to publish!')</script>");
                }
            } 

【问题讨论】:

  • 这是“我的代码不起作用,请为我修复”的问题之一。您可以通过在提出问题之前表明您已努力解决问题来避免投反对票。
  • client 在哪里定义?你为什么要从txtTopicPublish.Text 组成一个字符串Topic 而从不使用它?为什么在测试txtTopicPublish.Textisn'tpy 时使用txtPublish.Text
  • @Heki 好吧,它适用于我的 wpf,我只尝试将其转换为 asp.net,我尝试了但找不到代码
  • btnPublish_Click 被调用时,我愿意打赌clientnull。对吗?

标签: c# asp.net wpf mqtt


【解决方案1】:

看来是对ASP.NET page lifecycle缺乏了解。 在 WPF 中,生命周期从您运行程序到关闭程序;它是有状态的。 ASP.NET 不是;你在Page_Load(和其他生命周期事件)中所做的任何事情都将在页面渲染完成后处理。

你有几种方法可以解决你的问题。

  1. 您可以将MqttClient 实例保留在Application 对象中。这使实例从 AppPool 启动(在 Global.asax 中的 Application_Start 事件中实例化客户端。它在 AppPool 启动时被触发)一直保持活动状态,直到它关闭(Application_End,您有机会关闭你的MqttClient 如果你想/需要,请优雅地放下)。它在所有用户之间共享,并且可以使用Application[key] 在任何地方访问,其中key 是任何字符串,例如"MqttClient"
  2. 您可以将其保存在Session 对象中,就像保存在Application 对象中一样。您可以以相同的方式使用Sesson_StartSession_EndSession 对象对于每个用户来说都是独一无二的,因为它会一直保持活动状态,直到用户停止浏览您的网站。
  3. 您可以在每次需要时实例化MqttClient,或者使用每个Page_Load

【讨论】:

    猜你喜欢
    • 2011-09-09
    • 2011-04-13
    • 2011-11-19
    • 2013-05-22
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    相关资源
    最近更新 更多