views:

546

answers:

1

I have a web site which has a simple web service. I can call the web service successfully from javascript on the page. I need to be able to call the same web service from a C# forms application. The web service code is very simple:

[WebService(Namespace = "http://myurl.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class IDCService : System.Web.Services.WebService {

    public IDCService () {
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

My javascript works:

        function HelloWorld() {
            var yourName = $get('txtYourName').value;
            alert(yourName);
            IDCService.HelloWorld(HelloWorldCalback, failureCB);
        }

        function HelloWorldCalback(result) {
            alert(result);
        }

        function failureCB(result) {
            alert("Failed");
        }

However, when I try to set a reference to the WS in my C# code what I expect to see is an object with a method "HelloWorld", what I in fact see is an object with properties like "HelloWorldRequest", "HelloWorldResponse", "HelloWorldRequestBody" and so forth.

I am new to web services, and am very confused. Any help would be appreciated.

+2  A: 

Depends on how you added your reference :-)

If you added it by clicking "Add Web Reference", you specified the location of the service, and you gave it a namespace - let's assume it would be called "MySVC".

In that case, you should be able to do this in your Winforms program:

MySVC.MyTestService svc = new MySVC.MyTestService();
string message = svc.HelloWorld();

and thus retrieve the output of the HelloWorld method.

On the other hand, if you clicked on "Add Service Reference" (which is not the same - this will add a WCF client side proxy to your web service), then you'd get those request and response object classes. You should also get a xxxxClient class, and that's what you'll use:

MyWCFService.MyTestServiceSoapClient client = 
     new MyWCFService.MyTestServiceSoapClient();
string message = client.HelloWorld()

That way, you should be able to access all your methods on your web service, too.

Marc

marc_s
That explains a lot, however, for some reason my windows form project doesn't have an option to add a web reference, just add reference and add service reference. Which is weird, because the web application in the same solution does have an add web reference. Any thoughts on why I can't add a web reference?
Great explanation, @Marc! +1
Cerebrus
Jessica: if you use "Add Service Reference", click on the "Advanced" button, and on that screen, there should be an "Add Web Reference" button for you to use.
marc_s
Thank you Marc, that fixed my problem. I really appreciate your help.
Accept my answer then? Thanks!
marc_s