Web Services and static classes - need help :(

Hi folks,

I'm trying to contact a web service from a PocketPC 2003 application. Right now I'm just using the PocketPC 2003 Emulator, and my web service's Url property is set to a real URL on my LAN - not the "localhost:1130" you get by default.

If I place this code in the "Load" event of my main form, everything works fine:



using (SitesProxy.SitesService ssvc = new SitesProxy.SitesService())
{
    DataSet sites = ssvc.GetSites(true);

    label1.Text = sites.Tables[0].Rows[0]["SiteName"].ToString();
}

 



The app successfully retrieves data from my "SitesService" web service and sets the label's text to the name of the first site returned. Lovely.

However, I want to wrap this logic up into a static class, like this:



static class MySite
{
    static DataSet sites;

    static MySite()
    {
        using (SitesProxy.SitesService ssvc = new SitesProxy.SitesService())
        {
            sites = ssvc.GetSites(true);
        }
    }

    static DataRow Current
    {
        get { return sites.Tables[0].Rows[0]; }
    }
}

 


(Ok, this is a dumb example, but you get the idea.)

If I then put this code into my main form's Load event:



label1.Text = MySite.Current["SiteName"].ToString();

 


... then the program simply locks up. It sits forever with the 'cursor' window (the little rotating coloured box).

Do web service calls not work from within static methods Have I missed something Any help would be greatly appreciated.



Answer this question

Web Services and static classes - need help :(

  • Gil Kozlowski

    Hi Matt,
    I'm investigating this and I will get back to you on this shortly.
    Cheers.


  • Simon M

    So I verified that this is an issue on NETCF. We will be tracking it for the next release (not 2.0 though). The current work around will be for you not to call your web method in a static constructor. Is there any reason in particular why you have to do this

    Thanks.

  • Darshan Pandit

    Thanks Mark.

    I have done some further tests, and it seems that it only dies if I try to access a webmethod in a static constructor. Using "lazy loading" and accessing the webmethod in a static method or property works fine, so that's got me out of the woods for now.

    I'd much rather do it in the constructor, though, and this sounds like a bug unless I'm missing something very obvious.

    Cheers!

  • giskard73

    I just knocked up a smaller application to test this. I defined two extra classes in my "Form1.cs" file:


        public static class MyStaticSite
        {
            private static DataSet sites;

            static MySite()
            {
                using (SitesProxy.SitesService ssvc = new DeviceApplication1.SitesProxy.SitesService())
                {
                    sites = ssvc.GetSites(true);
                }
            }

            public static DataRow Current
            {
                get { return sites.Tables[0].Rows[0]; }
            }
        }

        public class MySite
        {
            private DataSet sites;

            public MySite()
            {
                using (SitesProxy.SitesService ssvc = new DeviceApplication1.SitesProxy.SitesService())
                {
                    sites = ssvc.GetSites(true);
                }
            }

            public DataRow Current
            {
                get { return sites.Tables[0].Rows[0]; }
            }
        }

     



    In my Form1.Load event I have two lines:



                // label1.Text = MyStaticSite.Current["SiteName"].ToString();
                label1.Text = new MySite().Current["SiteName"].ToString();

     


    If I run that code as-is, everything works fine. If I comment out the second line and uncomment the first, the program never starts - it seems to hang on the call to ssvc.GetSites() in the static class's constructor.

    This is VS.NET 2005 Beta 2; I don't know if this problem has been addressed in a later CTP. Perhaps someone out there who has a web service they can test with can confirm this as a bug or not.

    Cheers,
    Matt


  • John avis

    Only coz I'm a pedant :)

    I'll make do with a lazy-loading approach and call the web service from a static method/property instead for now. Thanks for your help, Mark. Good to know I caught a bug! Big Smile

  • Web Services and static classes - need help :(