当前位置: 动力学知识库 > 问答 > 编程问答 >

I can't seem to call my web service from C# Forms app

问题描述:

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.

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: