Click here to Skip to main content
15,910,212 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am writing Send SMS code in .Net window service and asp.net web forms for sending SMS to client But i am facing problem in send SMS per second to MBLOX url. Problem is that when i am using web form, it sends 60 SMS/S to Mblox url but in window service it sends only 4 SMS/S but both has same code.Please give me solution what should i do that case.

I am using code in asp.net web forms and window service. But forms takes very short time but window service very long time. but i would like to use window service.


C#
public void SendMessageUsingThread(string messagetext)
        {


            StreamReader objSreamReader = new StreamReader("D:/ExpressText/TestApplication/WindowService/TestWindowService/TestWindowService/Numbers.txt");
            string MobileNumber = objSreamReader.ReadToEnd();
            string[] NumberList = MobileNumber.Split(',');
            Dictionary<int, string> objdictionary = new Dictionary<int, string>();

            for (int i = 0; i < NumberList.Length; i++)
            {
                objdictionary.Add(i, NumberList[i].ToString().Replace('\n', ' ').Replace('\r', ' ').Trim());
            }

            for (int j = 0; j < objdictionary.Count - 1; j++)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(e => { SendMTMessageNew(objdictionary[j], messagetext); });
            }
        }



  public void SendMTMessageNew(string mobileNumber, string messagetext)
        {
            string requestId = string.Empty;
            string OutXml = string.Empty;
            try
            {
                string formatedMessage = formate4infoMessage(messagetext);
                StringBuilder sb = new StringBuilder();
                //int MessageCounter = GetMessageCounter();
                string MessageCounter = mobileNumber.Substring(4);

                sb.Append("<NotificationRequest Version=\"3.5\">");
                sb.Append("<NotificationHeader>");
                sb.AppendFormat("<PartnerName>{0}</PartnerName>", "XXXXXXXX");
                sb.AppendFormat("<PartnerPassword>{0}</PartnerPassword>", "XXXXXXXX");
                sb.AppendFormat("<Username>{0}</Username>", "XXXXXXXX");
                sb.Append("</NotificationHeader>");
                sb.AppendFormat("<NotificationList BatchID=\"{0}\">", MessageCounter);
                sb.AppendFormat("<Notification SequenceNumber=\"{0}\" MessageType=\"{1}\" >", MessageCounter, "SMS");
                sb.AppendFormat("<Message>{0}</Message>", formatedMessage);
                sb.AppendFormat("<Profile>{0}</Profile>", _ProfileId);// If u have single product form mblox u need to pass -1
                sb.AppendFormat("<SenderID Type=\"Shortcode\">{0}</SenderID>", XXXXXXX);
                sb.Append("<ExpireDate></ExpireDate>");
                sb.AppendFormat("<Operator></Operator>");
                sb.AppendFormat("<Tariff></Tariff>");
                sb.Append("<Subscriber>");
                sb.AppendFormat("<SubscriberNumber>{0}</SubscriberNumber>", "XXXXXXX");
                sb.Append("</Subscriber>");
                sb.AppendFormat("<ServiceId>{0}</ServiceId>", "XXXXXXX");
                sb.Append("</Notification>");
                sb.Append("</NotificationList>");
                sb.Append("</NotificationRequest>");


                OutXml = sb.ToString();

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format("http://{0}", Host));


                byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(OutXml);
                req.Method = "POST";
                req.ContentType = "text/xml";
                req.ContentLength = requestBytes.Length;

                req.Proxy = GlobalProxySelection.GetEmptyWebProxy(); ;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = true;

                Stream requestStream = req.GetRequestStream();
                requestStream.Write(requestBytes, 0, requestBytes.Length);
                requestStream.Close();

                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
                string backstr = sr.ReadToEnd();
                sr.Close();
                res.Close();

            }
            catch (Exception ex)
            {
                CreatelogfileForMloxResponseXML(ex.Message + "--------------" + "SendMTMessageNew");
            }
        }
Posted
Updated 30-Aug-15 23:42pm
v2
Comments
Sreekanth Mothukuru 31-Aug-15 6:56am    
Make sure your service doesn't stop before completing the task to send SMS to all selected phone numbers.
[no name] 31-Aug-15 7:47am    
yes i am sure. i have checked by using jetbrains dottrace tool. It shows threads takes long time(90%) to execute the "code Stream requestStream = req.GetRequestStream();" but it works good in web forms. I would like to know why it takes long time is window service does not take long time in web forms.
Sreekanth Mothukuru 31-Aug-15 10:30am    
I guess the file is being read only once in web application. May be the file is opened/closed/flushed multiple times. Not sure how often your method (SendMessageUsingThread) is being called from the onstart on service.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900