WebService Experience Sharing

Source: Internet
Author: User

Webservice is a common interface for accessing data provided to the client. After the client adds a reference to webservice, the client will generate a local proxy for Webservice, then the client can use the Webservice proxy class just like a local class. Of course, the generated local proxy is a little different from the access method provided by the native Webservice, such as the List <string> type method parameter used in WebService, in the client proxy, you must use the string [] array as the parameter value and pass it to the Webservice proxy method. These will be discussed separately in the future. There are two small experiences to be shared today.

First, how to do this when we need to dynamically set the URL of the webservice we reference. For example, we want to configure the Webservice Url in the config configuration file, or let the user enter the Webservice url on the user interface.

In fact, our Webservice provides the Url attribute. When calling this Service, we can directly assign a value to this Url attribute. What we need to do is to right-click the Webservice reference we added and set the URL Behavior attribute of Webservice to Dynamic. Then we can write the following code on the client:

 LocalWebServiceProxy.StudentService service=new LocalWebServiceProxy.StudentService();
service.Url=”http://211.13.23.56/StudentService.asmx”;
string userName=service.GetUserName();

 

The second thing we will share with you is that we all know that Webservice supports Session. You only need to add the parameter EnableSession = true on the Attribute of WebMethod. As follows:

 

Code

 Public class StudentService: System. Web. Services. WebService
{
[WebMethod (Description = "login", EnableSession = true)]
Public void Login (string userName, string pwd)
{
Context. Session ["USER"] = userName;
}

[WebMethod (Description = "Get username", EnableSession = true)]
Public string GetUserName ()
{
If (Context. Session ["USER"]! = Null)
{
Return Context. Session ["USER"]. ToString ();
}
Else
Return string. Empty;
}
}

 

However, we all know that, even so, when the client calls the Login method and the client calls the GetUserName method again, the Context. Session ["User"] is still empty. We can solve this problem by using the following method. When the client calls the proxy class of Webservice, it needs to assign the CookieContainer attribute to the proxy class instance. As follows:

 

Code

 Public class StudentServiceInstance
{
Static LocalWebService. StudentService service;
Public static LocalWebService. StudentService Service ()
{
If (service = null)
{
Service = new LocalWebService. StudentService ();
// You can dynamically set the Url of WebService.
// Service. Url = "http: // 192.168.84.47/SchoolWebService ";
Service. CookieContainer = new System. Net. CookieContainer ();
Service. Credentials = System. Net. CredentialCache. DefaultCredentials;
Return service;
}
Else
Return service;
}
}

 

The above method is to customize a method on the client to instantiate this Webservice proxy instance. The instance object must be a static object, in this way, the client can use the custom class anywhere to obtain the Webservice proxy class object, which is the same proxy class object. At the same time, you must set the CookieContainer attribute and Credentials attribute for the instantiated proxy class object.

Then, we can call Webservice as follows:

 

Code

 Protected void BtnGetUserName_Click (object sender, EventArgs e)
{
String userName = StudentServiceInstance. Service (). GetUserName ();

// The Session cannot be shared in the following calls:
// LocalWebService. StudentService service = new SessionWebService. LocalWebService. StudentService ();
// String userName = service. GetUserName ();

ClientScriptManager cs = this. ClientScript;
Cs. RegisterClientScriptBlock (this. GetType (), "info", "javascript: alert ('" + userName + "')", true );
}
Protected void BtnLogin_Click (object sender, EventArgs e)
{
StudentServiceInstance. Service (). Login ("school", "111111 ");

// The Session cannot be shared in the following calls:
// LocalWebService. StudentService service = new SessionWebService. LocalWebService. StudentService ();
// Service. Login ("school", "111111 ");
}

 

 

 

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.