3

How to call Webservice from Windows Service?

I am having one webservice on my Webserver.

I have a windows-service to trigger that webservice.

So I just want to integrate and call my webservice from my windows service.

How can I do that?

EDIT:

TempWindowService is name of my windows application

MyServ is the name of my reference of my webservice.

 TempWindowService.MyServ newService = new TempWindowService.MyServ();
 newService.BatchProcess();

Here BatchProcess() is the webmethod under my webservice.

I get error on line

 TempWindowService.MyServ newService = new TempWindowService.MyServ();

Error is as below

 'TempWindowService.MyServ' is a 'namespace' but is used like a 'type'  

ANSWER:

For Those Who are interested in solution to this question, this is the code to be written

TempWindowService.MyServ.MyServSoapClient newService = new TempWindowService.MyServ.MyServSoapClient();

Hope this Helps everyone looking for a solution to this question. :)

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216

1 Answers1

7

You'd call it just like you would from any application, web or otherwise. In Visual Studio, add a Service Reference for the web service in question to the project for the Windows Service. This will generate proxy classes for you which you would use in your code to access the web service.

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    I would suggest "Add Service Reference" as he is using .NET 3.5 to use WCF – Philippe Mar 29 '11 at 09:36
  • @David: I am not getting option "Add Web Reference" in my Windows Service. I just get the options "Add Service Reference" and "Add Reference" What should I do in that case? – Parth Bhatt Mar 29 '11 at 09:36
  • @Phillipe: I tried using "Add Service Reference" But then I am not able to declare an object of my webservice so that I can call the webmethods inside my webservice. – Parth Bhatt Mar 29 '11 at 09:39
  • @PARTH: He's right, service reference is likely the way to go in this case. How are you trying to declare an object of the reference? Can you edit your question to post the code, as well as what errors you are seeing? – David Mar 29 '11 at 09:41
  • Yeah sure. Wait a min. I will add it – Parth Bhatt Mar 29 '11 at 09:44
  • @David: I have edited my question. Please check out the code and let me know why it gives this error and what can be done? – Parth Bhatt Mar 29 '11 at 09:50
  • @PARTH: It would appear that `TempWindowService.MyServ` isn't the name of the class, but merely the name of the namespace in which the class resides. There should also be a class name within that namespace which actually references the service in question. Note that you're not declaring an instance of the entire web service project, just the individual service (perhaps a .asmx file?) within that project. That service should have a class name within the given namespace. – David Mar 29 '11 at 10:14
  • Thanks David. This did help :) Thanks a lot. Actually it should be `TempWindowService.MyServ.MyServSoapClient newService = new TempWindowService.MyServ.MyServSoapClient();`.This worked for me. – Parth Bhatt Mar 29 '11 at 10:17