none
HttpContext is null RRS feed

  • Question

  • Hi,

    I'm developing a web server (C#) and got stuck with receiving POST requested files.

    HttpContex.Current is null. How to fix it?

    At the moment, code is:

            public void POSTRequest(byte[] Header, TcpClient Client )
            {
                if (Header != null)
                {
                    if (HttpContext.Current == null) //THIS IS NULL, WHAT TO DO???
                    {
                        MessageBox.Show("HttpContext.Current is null :("); 
                    }

                    if (HttpContext.Current != null)
                    {
                        MessageBox.Show("HttpContext.Current is NOT null :)");

                        try
                        {
                            HttpFileCollection uploadedFiles = HttpContext.Current.Request.Files;
                            foreach (HttpPostedFile file in uploadedFiles)
                            {
                                if (file != null)
                                {
                                    if (file.ContentLength > 0)
                                    {
                                        System.IO.Stream fs = new FileStream(RootPath, FileMode.Create);
                                        fs = file.InputStream;
                                        byte[] fileData = new byte[file.ContentLength];
                                        fs.Write(fileData, 0, file.ContentLength);
                                        file.SaveAs(RootPath + "\\" + file.FileName);
                                        MessageBox.Show("File uploaded to Server!");
                                        SendString("<h1>Fail edukalt &uuml;les laetud!</h1>", Client, "");
                                    }
                                }
                            }
                        }
                        catch (NullReferenceException e)
                        {
                            MessageBox.Show(e.Message + e.StackTrace);
                        }
                    }
                  
                }
            }

    Or is there some better way to save client uploaded files to server's hard drive?

    Tuesday, July 23, 2013 9:31 PM

Answers

  • YOu are passing the function a TCPclient which doesn't contain the Httpcontext Property.  why is the Httpcontext coming from?  Is the property attached to the socket object? Why don't you pass the function the Httpcontext object in th eparamter list of the function?

    jdweng

    Wednesday, July 24, 2013 2:25 AM
  • As Joel mentioned, HttpContext.Current is available only in some special application host environment such as ASP.NET web application, WCF REST service (enable ASP.NET compatiblility), etc...
    If you're using TCP or sockets components for implementing your server application, HttpContext is not supposed to be available, you need to consider other approaches for implementing the functions.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 30, 2013 6:53 AM
    Moderator

All replies

  • Hello Jurgen,

    Not sure from the snippet above but I had a similar issue with the context being lossed when spawning threads.  If this is the case, then it could be resolved by simply passing the context into the created task.  Please reply if this is not the case.


    Jeff

    Wednesday, July 24, 2013 12:23 AM
  • YOu are passing the function a TCPclient which doesn't contain the Httpcontext Property.  why is the Httpcontext coming from?  Is the property attached to the socket object? Why don't you pass the function the Httpcontext object in th eparamter list of the function?

    jdweng

    Wednesday, July 24, 2013 2:25 AM
  • As Joel mentioned, HttpContext.Current is available only in some special application host environment such as ASP.NET web application, WCF REST service (enable ASP.NET compatiblility), etc...
    If you're using TCP or sockets components for implementing your server application, HttpContext is not supposed to be available, you need to consider other approaches for implementing the functions.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 30, 2013 6:53 AM
    Moderator
  • I have hosted my wcf with asp.net web application Also have done following changes to my wcf service still i am not able to get files from httpcontext.

    1. In Service interface

     [System.Web.Services.WebMethod(EnableSession = true)]
            [OperationContract]
            [WebInvoke(UriTemplate = "uploadImage2/?id={id}", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            string uploadImage2()

    2. in service method

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    3. web.config

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"  />

    4. in service method i am getting error if i write following 

    System.Web.HttpContext.Current.Request.Files.Count

    Please advice.

    Thanks
    Sunil

    Friday, November 15, 2013 9:07 AM