locked
Webservice access delay through client application RRS feed

  • Question

  • Hi ,

    I Have created a web service while accessing webservice function from client to service is taking 1-3 seconds of time.

    My webservice Code:

    ***************************************************************************************************

                                           

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Services;
    using System.Net;
    using System.Net.Sockets;using System.Runtime.InteropServices;

    using System.IO;
    using System.Text;
    using System.Threading;

    namespace FirstWebService
    {

        /// <summary>
        /// Summary description for Service1
        /// </summary>
        [WebService(Namespace = "https://rupeeseed.com/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        public class Service1 : System.Web.Services.WebService
        {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("100.0.2.49"), 6236);
           // IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("100.0.2.49"), 6236);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //public delegate void ChangedReceivedHandler(byte[] ByteBuffer,int recv);
            //public event ChangedReceivedHandler Received;
            byte[] recBuffer = new byte[252];
            int recvBytes=0;
            bool recvFlag = false;
            //static EventWaitHandle _waitHandle = new AutoResetEvent(false);

            [WebMethod(EnableSession = true)]
            public void GetConnectedServer()
            {
                try
                {
                   server.Connect(ipep);
                    Session["server"] = server;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            [WebMethod(EnableSession = true)]
            public void GetDisConnectedServer()
            {
                try
                {
                    server = ((Socket)(Session["server"]));
                    server.Shutdown(SocketShutdown.Both);
                    server.Close();
                }
                catch(Exception ex)
                {
                    throw ex;
                }

            }

            [WebMethod(EnableSession = true)]
            public byte[] RecvResponse(out int recvBytes)
            {
                server = ((Socket)(Session["server"]));
                recBuffer = new byte[252];
                LingerOption lingerOption = new LingerOption(true, 1);
                server.LingerState = lingerOption;
                server.ReceiveTimeout = 500;
                byte[] newBuffer=new byte[252];
                IntPtr Iptr;
                //while (true)
                //{
                    try
                    {
                        recvBytes = server.Receive(recBuffer, SocketFlags.Peek);
                        if (recvBytes > 0)
                        {
                            recvBytes = server.Receive(newBuffer, 0, 252, SocketFlags.None);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    return newBuffer;

            }


            [WebMethod(EnableSession = true)]
            public void SendRequest(byte[] data)
            {
                try
                { 
                    server = ((Socket)(Session["server"]));
                    server.Send(data);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    }

    ***************************************************************************************************                                Calling Sequence to the service is       1.GetConnected Server 2.SendRequest   3. ReceiveResponse  4.DisconnectedServer All the calls to service from client is delayed.


    SaiVaibhav@Acumen

    Wednesday, November 21, 2012 2:37 PM

All replies

  • First of all, do yourself a big favor and get rid of the "throw ex;". All that does is destroy your stack trace.

    Do you have any logging turned on? If not, then turn on ASP.NET Health Monitoring by adding

    <healthMonitoring Enabled="true"/>
    

    to your web.config. Then remove the try/catch blocks entirely. If there are any exceptions, they will be logged in the Windows Event Log as ASP.NET warnings. Look to see what the exception is, if any.

    Also, to be clear, are you saying that there's a delay even when calling GetConnectedServer? You should do some debugging and try to find out where the delay is. One way to do this would be to add a new operation that does nothing at all, and see if that has a delay.


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    Wednesday, November 21, 2012 5:40 PM
    Moderator