简体   繁体   中英

C# Webservice and Static Variables

I'm having a bit of a problem and was wondering if someone could help me with this.

The problem is that the static Variable is shared between different thread calls/seasions. This is the original code:

public partial class ApplicationWSGlobal : System.Web.Services.WebService
{
    public static string UploadPath = @"";

    public ApplicationWSGlobal()
    {
        InitializeComponent();
    }

    [Webmethod]
    public void DoSomeThing()
    {
        ... = UploadPath;            
    }
}

and I'm setting the UploadPath from in the global.asax class.

 public void Application_BeginRequest() 
 {
     ApplicationWSGlobal.UploadPath = getData();
 }

I tried to resolve the problem by adding the [ThreadStatic] to the "UploadPath' var but it did not work. That global vars runs across all sessions and i don't want that

Is there any alternatives that i can use in this situation

I thanks for all the replies in advance!

I suggest storing UploadPath in the database. That way it's guaranteed to be global across all running sessions.

Try with this code (Remove static modifier)

    public partial class ApplicationWSGlobal : System.Web.Services.WebService
    {
        public string UploadPath = @"";

        [WebMethod]
        public void SetUploadPath(string x)
        {
            UploadPath = x;
        }

        public ApplicationWSGlobal()
        {
            InitializeComponent();
        }
     }
var applicationWSGlobal = new ApplicationWSGlobal
{
   UploadPath = your value
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM