Example #1
0
        private static void ListenerCallback(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;

            //process another request
            listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);

            try
            {
                HttpListenerContext context = listener.EndGetContext(result);
                HttpListenerHost    host    = new HttpListenerHost(context, uriBaseAddress);

                object hostWrapper;
                if (useV2Host)
                {
                    hostWrapper = new IDSH2Wrapper(host);
                }
                else
                {
                    hostWrapper = new IDSHWrapper(host);
                }

                object service = Activator.CreateInstance(serviceType);

                MethodInfo attachMethod = serviceType.GetMethod("AttachHost", new Type[] { typeof(IDataServiceHost) });
                attachMethod.Invoke(service, new object[] { hostWrapper });
                MethodInfo processRequestMethod = serviceType.GetMethod("ProcessRequest", BindingFlags.Public | BindingFlags.Instance);
                try
                {
                    processRequestMethod.Invoke(service, new object[0]);
                }
                catch (Exception e)
                {
                    if (!host.processExceptionCalled)
                    {
                        throw e;
                    }
                }

                if (host.processExceptionCalled)
                {
                    if (context.Request.HasEntityBody)
                    {
                        context.Request.InputStream.Close();
                        context.Request.InputStream.Dispose();
                    }
                }

                context.Response.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred:");
                Console.WriteLine(e.ToString());
                listener.Abort();
                Console.ReadLine();
            }
        }
        private static void ListenerCallback(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;
            
            //process another request
            listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);

            try
            {
                HttpListenerContext context = listener.EndGetContext(result);
                HttpListenerHost host = new HttpListenerHost(context, uriBaseAddress);

                object hostWrapper;
                if (useV2Host)
                    hostWrapper = new IDSH2Wrapper(host);
                else
                    hostWrapper = new IDSHWrapper(host);

                object service = Activator.CreateInstance(serviceType);

                MethodInfo attachMethod = serviceType.GetMethod("AttachHost", new Type[] { typeof(IDataServiceHost) });
                attachMethod.Invoke(service, new object[] { hostWrapper });
                MethodInfo processRequestMethod = serviceType.GetMethod("ProcessRequest", BindingFlags.Public | BindingFlags.Instance);
                try
                {
                    processRequestMethod.Invoke(service, new object[0]);
                }
                catch (Exception e)
                {
                    if (!host.processExceptionCalled)
                        throw e;
                }

                if (host.processExceptionCalled)
                {
                    if (context.Request.HasEntityBody)
                    {
                        context.Request.InputStream.Close();
                        context.Request.InputStream.Dispose();
                    }
                }

                context.Response.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred:");
                Console.WriteLine(e.ToString());
                listener.Abort();
                Console.ReadLine();
            }
        }