在ASP.NET中通过GSM调制解调器发送短信

4

我开发了一个asp.net应用程序,通过浏览器的URL将短信从GSM调制解调器发送到目的地。我使用了CodeProject开发的库http://www.codeproject.com/articles/20420/how-to-send-and-receive-sms-using-gsm-modem

但是当我同时从两个浏览器请求时会遇到问题,我想让我的代码检测到模块在该时间被另一个进程使用

这是我的代码:

DeviceConnection deviceConnection = new DeviceConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["destination"] != null && Request.QueryString["text"] != null)
                {
                    deviceConnection.setBaudRate(9600);
                    deviceConnection.setPort(12);
                    deviceConnection.setTimeout(200);             
                    SendSms sendSms = new SendSms(deviceConnection);
                    if (deviceConnection.getConnectionStatus())
                    {

                        sendSms.strReciverNo = Request.QueryString["destination"];
                        sendSms.strTextMessage = Request.QueryString["text"];

                        if (sendSms.sendSms())
                        {
                            Response.Write("Mesage successfuly sent to " + Request.QueryString["destination"]);   
                        }
                        else
                        {
                            Response.Write("Message was not sent");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Index "+ex.StackTrace);
            }
        }

这是SendSms类:

 class SendSms
    {
        DeviceConnection deviceConnection;
        public SendSms(DeviceConnection deviceConnection)
        {
            this.deviceConnection = deviceConnection;
        }
        private string reciverNo;
        private string textMessage;
        private delegate void SetTextCallback(string text);
        public string strReciverNo
        {
            set
            {
                this.reciverNo = value;
            }
            get
            {
                return this.reciverNo;
            }
        }
        public string strTextMessage
        {
            set
            {
                this.textMessage = value;
            }
            get
            {
                return this.textMessage;
            }
        }

        public bool sendSms()
        {
            try
            {
                CommSetting.Comm_Port = deviceConnection.getPort();//GsmCommMain.DefaultPortNumber;
                CommSetting.Comm_BaudRate = deviceConnection.getBaudRate();
                CommSetting.Comm_TimeOut = deviceConnection.getTimeout();//GsmCommMain.DefaultTimeout;

                CommSetting.comm = new GsmCommMain(deviceConnection.getPort()
                    , deviceConnection.getBaudRate(), deviceConnection.getTimeout());
                //    CommSetting.comm.PhoneConnected += new EventHandler(comm_PhoneConnected);
                if (!CommSetting.comm.IsOpen())
                {
                    CommSetting.comm.Open();
                }
                SmsSubmitPdu smsSubmitPdu = new SmsSubmitPdu(strTextMessage, strReciverNo, "");
                smsSubmitPdu.RequestStatusReport = true;
                CommSetting.comm.SendMessage(smsSubmitPdu);
                CommSetting.comm.Close();
                return true;
            }
            catch (Exception exception)
            {
                Console.WriteLine("sendSms " + exception.StackTrace);
                CommSetting.comm.Close();
                return false;
            }
        }
        public void recive(object sender, EventArgs e)
        {
            Console.WriteLine("Message received successfuly");
        }

    }
}
1个回答

0

我不确定 ASP 的工作原理,但如果每个进程共享相同的内存空间,您可以使用静态锁来确保单一访问。无论如何都值得一试。

private static readonly object CommLock = new object();

public bool sendSms() 
{ 
  lock(CommLock) //second request should wait here until first request finishes
  {
    //all the same comm code from sendSms as before
  }            
}

我同意Dai的全局互斥锁想法作为一种改进,参见C#中使用全局互斥锁的好模式是什么?


1
所有请求都由同一个进程在同一个AppDomain中处理(除非在非常特殊的情况下),因此您的解决方案将起作用 - 但是为了绝对确定,我建议使用操作系统级别的Mutex或锁文件。 - Dai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接