How do I use a webservice client in a static class? RRS feed

  • Question

  • Hiya, it's been a while since I've been here


    I've run into a problem which I can't figure out on my own. I'm trying to make webservice calls from a static class :


    public static class MyClass


    static ServiceReference.ServiceClient WebServiceClient



    the thing is, that service client isn't instantiated, since I can't use a "new"  in a static class. I remember being able to do this during the beta stage, now it won't work however. Can anyone assist?

    Thursday, November 27, 2008 6:21 AM

All replies

  • Hi,

     You Can try some thing like this..

    public static class Class1


    public static void callService()


    Service1Client clnt=new ServiceReference1.Service1Client();clnt.DoWorkCompleted+=new EventHandler<serviceCallFromStaticClass.ServiceReference1.DoWorkCompletedEventArgs>(clnt_DoWorkCompleted);




    static void clnt_DoWorkCompleted(object sender, serviceCallFromStaticClass.ServiceReference1.DoWorkCompletedEventArgs e)




    Thursday, November 27, 2008 6:56 AM
  •  Hey! Thanks for the answer.

    That seems to work, but what if I want to have a single client that can be shared by all methods of my static class ?

    Thursday, November 27, 2008 7:44 AM
  • Hey,

    Can you explain me what do you mean bty single client here.

    I am bit confused...

    Thursday, November 27, 2008 7:50 AM
  •  static class myClass


    //class properties

    static Service.ServiceClient myServiceClient ;

    //class methods


    static void MyMethod()




    static  myClass()


    myServiceClient = new blahblahblah.ServiceClient(); // THAT IS NOT ALLOWED

    myServiceClient.DoWorkCompleted += new DoWorkComplted();




    something like that. I want to have a single service client for the whole class rather then instantiating it in each method separately. Ideally I'd add it's completed event handlers in the constructor and I'd just call the async methods from the class methods?

    Thursday, November 27, 2008 8:05 AM
  •  anyone?

    Thursday, November 27, 2008 3:49 PM
  • myServiceClient = new blahblahblah.ServiceClient(); // THAT IS NOT ALLOWED

    I duplicated your code with one of my web services and had no problem compiling and running it. What problem are you having?

    Thursday, November 27, 2008 4:24 PM
  •  The problem is : I'm using a static class b/c I want every page to have access to the said class. Since it's static it needs to have static members/properties etc. I'd like to have a static ServiceClient that I only initialize once for the whole class - so that each of my methods can use it straight away and make async calls. Due to the fact that the ServiceClient is static , I'm getting an exception when I'm using the myServiceClient = new (...).ServiceClient; Is there any way around this ?

     Problem is , I can't instantiate a static class member using the new keyword it would seem.

    Thursday, November 27, 2008 4:29 PM
  • For following works for me:

         public static class WorksForMe
            static WordsClient _Client;

            static WorksForMe()
                _Client = new WordsClient();
                _Client.AudioFilesSearchCompleted += new EventHandler<AudioFilesSearchCompletedEventArgs>(_Client_AudioFilesSearchCompleted);

            static void _Client_AudioFilesSearchCompleted(object sender, AudioFilesSearchCompletedEventArgs e)
                //this is called succesfully


            public static void ForceLoad()

    Thursday, November 27, 2008 6:45 PM
  •  Interesting,

    My class looks pretty much the same and should work.

    I'm using a method Initialize(string someData) to set some initial values for some properties within. I'm making the call from Application_Startup() in App.xaml.cs.It looks more - or less like this :

    private void Application_Startup(object sender, StartupEventArgs e)
                    MyClass.Initialize("SOME STRING"); // At this line I'm getting a TypeInitializationException

     An exception appears and I'm not entirely sure why.


    Please note that I'm doing the Client = new ServiceClient(); in the static class's constructor


    Thursday, November 27, 2008 7:17 PM
  •  stillhaven't figured it out Indifferent

    Friday, November 28, 2008 6:30 AM