-1

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

代码:

[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 

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

4

1 回答 1

2

也许这可以帮助:

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

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

于 2013-04-01T06:42:38.203 回答