方法完成后会话清除

时间:2016-10-12 06:11:37

标签: asp.net web-services session

我的[WebMethod(EnableSession = true)] public void checkEmployee(string emailId, string password) { List<Employee> ListEmp = new List<Employee>(); string cs = ConfigurationManager.ConnectionStrings["rushDB"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("select * from Employees where Email='" + emailId + "' AND Password='" + password + "'", con); string personName = null; string personEmail = null; string personDeptName = null; con.Open(); SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.HasRows == true) { while (rdr.Read()) { Employee empChild = new Employee(); empChild.ID = Convert.ToInt16(rdr["ID"]); empChild.Name = rdr["Name"].ToString(); empChild.Email = rdr["Email"].ToString(); empChild.Password = rdr["Password"].ToString(); empChild.DepartName = rdr["DepartName"].ToString(); personName = rdr["Name"].ToString(); personEmail = rdr["Email"].ToString(); personDeptName = rdr["DepartName"].ToString(); ListEmp.Add(empChild); } HttpContext.Current.Session["NamePerson"] = personName; HttpContext.Current.Session["EmailPerson"] = personEmail; HttpContext.Current.Session["DepartPerson"] = personDeptName; JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Write(js.Serialize(ListEmp)); } } } [WebMethod(EnableSession = true)] public void GetCurrentData() { Employee ListEmp = new Employee(); if (HttpContext.Current.Session["NamePerson"] != null) ListEmp.Name = HttpContext.Current.Session["NamePerson"].ToString(); if (HttpContext.Current.Session["EmailPerson"] != null) ListEmp.Email = HttpContext.Current.Session["EmailPerson"].ToString(); if (HttpContext.Current.Session["DepartPerson"] != null) ListEmp.DepartName = HttpContext.Current.Session["DepartPerson"].ToString(); JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Write(js.Serialize(ListEmp)); } 页面中有两种方法,如下所示。

    int main(int argc, char* argv[])
    {
    (void) argc; // unused

    bool matched_result = false;

    // extra size
     char pwd[512]={0};
     getcwd(pwd, sizeof(pwd)/sizeof(pwd[0]));

     printf("Current working dir is: %s\n", pwd);

    // Check is already mounted ramfs on the current directory.
    // The beter way is manual parsing.
    // in order to avoid that process we have to write dirwalker
    // to list all mounted fses but for now will use this one
    FILE* mntstatus = popen("mount", "r");

    // assume no more than 512 chars per line
    char buff[512]={0};

    while (fgets(buff, 512, mntstatus) != NULL) {
        char* match = strchr(buff, '/');
        // trim 3 whitespaces from the end
        trim_end(match, ' ', 3);
        if (strcmp(match, pwd)==0) {
            printf("Match: [%s]\n", match);
            matched_result = true;
            break; // no need to search more
        }
    }

    // our cwd is not mounted
    if (!matched_result) {
        // empty, ok we can mount here
        // just efective-uid is not enough for mount
        // i need real-uid
        setuid(0);

        int res = mount_filesystem(pwd, "ramfs", "ramfs", 0, "rw", 0);
        //int res = system("mount -t ramfs ramfs `pwd`");
        if (res == 0) {
            printf("Mount ok\n");
            // convert to octals
            int octal_perms = strtol(g_Permissions, 0, 8);
            if (chmod(pwd, octal_perms) < 0) {
                fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s)\n",
                        argv[0], // program
                        pwd,    // current dir
                        g_Permissions, // with permissions
                        errno,          // Ermac :)
                        strerror(errno));
                exit(1);
            }

        } else {
            printf("Mount failed!\n");
            return 1;
        }
    } else {
        printf("Dude you are ok!\n");
    }

    return 0;
    }


     void trim_end(char *str, const char delim, int count)
     {
     char* begin = str;
     char* end = &str[strlen(str)-1];
    int i=0;
    while ((i < count) && ( begin != end)) {
        if (*end == delim) {
            i++;
        }
        *end-- = 0;
    }
}


void register_exit_callback(cbAtExit ex)
{
    atexit(ex);
}

int mount_filesystem(const char *src, const char *tgt, const char *fstype, unsigned long flags,
                     const char *mode, const char *uid)
{
    char mode_uid[256]={0};
    if((mode != NULL) && (uid != NULL)) {
        sprintf(mode_uid, "mode=%s,uid=%s", mode, uid);
    }

    int result = mount(src, tgt, fstype, flags, mode_uid);
    // handle result outside
    return result;
}

我的问题是checkEmployee创建的会话在checkEmployee完成执行后会消失。

我想在GetCurrentData中检索会话值,但我无法这样做。任何解决方案都会帮助我。我用谷歌搜索了它,但那没有帮助。

1 个答案:

答案 0 :(得分:1)

您需要 cookie-jar 才能在Web服务上保留会话状态。根据您使用该服务的方式,如果您之前没有这样做,可能会非常复杂非常 ......

查看以下文章:

http://www.codeproject.com/Articles/322436/RestSessionState

http://www.codeproject.com/Articles/188749/WCF-Sessions-Brief-Introduction