httpContext.Current.Session是空的(在WebService内部)

本文关键字:WebService 内部 Current Session httpContext | 更新日期: 2023-09-27 18:06:53

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

代码:

[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;
}

我已经在该web服务中启用了会话,但仍然得到错误消息:-

错误消息:-

 'System.Web.HttpContext.Current' is null 

还有一件事我需要从全局调用这个服务。ashx文件。

httpContext.Current.Session是空的(在WebService内部)

也许这能帮到你:

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

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