httpContext.Current.Session为null(Inside WebService)Asp.net

时间:2013-04-01 06:39:47

标签: c# asp.net web-services session

我有一个网络服务。我在该服务中使用了一些会话变量。

代码:

[WebMethod(EnableSession = true)]
public static string UploadFiles_Local(Dictionary<int, string> accFile)
{
    string FilePath = "";
    Dictionary<int, string> dicStatus = new Dictionary<int, string>();
    Dictionary<int, string> dicUpload = new Dictionary<int, string>();

    System.Web.HttpContext.Current.Session["uLocal"] = System.Web.HttpContext.Current.Server.MapPath("UplodedFiles") + "\\" + DateTime.Now.Ticks.ToString();

    foreach (var f_l in accFile)
    {
        FilePath = f_l.Value;
        string fName = FilePath.Substring(FilePath.LastIndexOf("\\") + 1, FilePath.Length - FilePath.LastIndexOf("\\") + 1);

        //File reading
        FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
        //Directory Existens checking
        if (!Directory.Exists(System.Web.HttpContext.Current.Session["uLocal"].ToString()))
        {
            Directory.CreateDirectory(System.Web.HttpContext.Current.Session["uLocal"].ToString());
        }
        try
        {
            //Upload files
            long FileSize = new FileInfo(FilePath).Length; // File size of file being uploaded.
            string uploadFileName = new FileInfo(FilePath).Name; // File name

            Byte[] buffer = new Byte[FileSize];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            fs = null;

            fs = File.Open(System.Web.HttpContext.Current.Session["uLocal"].ToString() + "\\" + fName, FileMode.OpenOrCreate);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(buffer);
            bw.Close();

            dicStatus.Add(f_l.Key, "File " + fName + ". Successfuly uploded to:" + System.Web.HttpContext.Current.Session["uLocal"].ToString() + "\\" + fName);
            dicUpload.Add(f_l.Key, System.Web.HttpContext.Current.Session["uLocal"].ToString() + "\\" + fName);
        }
        catch (Exception ex)
        {
            if (fs != null)
            {
                fs.Close();
            }
            dicStatus.Add(f_l.Key, "File " + fName + ". Error in uploding to:" + System.Web.HttpContext.Current.Session["uLocal"].ToString() + "\\" + fName + "\r\nError :" + ex.Message);
        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
            }
        }
    }
    if (dicUpload.Count > 0)
    {
        //Making rar of uploded files
        ClsClass.RarFilesT(System.Web.HttpContext.Current.Session["uLocal"].ToString() + ".rar", dicUpload);
        FilePath = System.Web.HttpContext.Current.Session["uLocal"].ToString() + ".rar";
    }
    else
    {
        FilePath = "Error";
    }
    return FilePath;
}

我已经在该网络服务中启用了会话,但是我收到了错误消息: -

错误消息: -

 'System.Web.HttpContext.Current' is null 

还有一点,我需要从globle.ashx文件中调用此服务。

1 个答案:

答案 0 :(得分:1)

也许这会有所帮助:

http://www.codeproject.com/Articles/35119/Using-Session-State-in-a-Web-Service

但是,让我告诉你,Web服务不应该存储任何会话状态,这是错误的。