C # webservice call method summary

Source: Internet
Author: User

1. WebService calling in cs background program
A. Use the namespace and class name to directly call
Example:
WebService ws = new WebService ();
String s = ws. HelloWorld ();
B. Call by adding a WEB reference. First, add a WEB reference and use a URL to point to WEBSERVICE,
Specify the WEB reference name, which is assumed to be KK;
Example:
Kk. WebService n = new kk. WebService ();
String ss = n. HelloWorld ();
2. WebService JS on the front-end page Call Method
1. First, use the following method to reference Webservice in the foreground:
<Asp: ScriptManager runat = "server">
<Services>
<Asp: ServiceReference Path = "WebService. asmx" InlineScript = "True"/>
</Services>
</Asp: ScriptManager>
2. You can call the SDK through the JS program, for example:
<Script type = "text/jscript">
Function ()
{
WebService. HelloWorld (onresult );
}
// Here the onresult is the callback function
Function onresult (result)
{
Alert (result );
}
Function B ()
{
WebService. add (1, 2, onreturn)
}
Function onreturn (result)
{
Alert (result );
}
// The context below is the context, which can be obtained by returning to the function through reloading;
Function c ()
{
WebService. div (1, 1, onresultC, onerror, context );
}
Function onresultC (res, c)
{
Alert (res );
Alert (c );
}
// Onerror is the callback function used to obtain the exception information. The following describes how to obtain the exception information.
Function onerror (error)
{
Var a = "";
A = String. format ("Get server exception type: {0} get detailed exception description: {1} Get exception cause: {2} Get server exception Stack
Trace information: {3} gets a Boolean value, indicating whether the exception is caused by network connection timeout {4 }",
Error. get_exceptionType (),
Error. get_message (),
Error. get_statusCode (),
Error. get_stackTrace (),
Error. get_timedOut ())
Alert ();
}
A ();
B ();
C ();
</Script>

---- Example of self-writing ---

Web Service ---:

[WebMethod]
Public string HelloWorld (){
Return "Hello World, wwg ";
}

[WebMethod]
Public int AddWwg (int a, int B)
{
Return a + B;
}

Exe ---

Using CallWebService. localhost; // The namespace is not defined.

Namespace CallWebService
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void button#click (object sender, EventArgs e)
{
Service serviceWwg = new Service ();
Int i1 = Int32.Parse (txt1.Text. ToString ());
Int i2 = Int32.Parse (txt2.Text. ToString ());
Int iResult = serviceWwg. AddWwg (i1, i2 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.