Silverlight and server-side Communication

Source: Internet
Author: User


To communicate with the server, we first use wcf to create a page for wcf.
Add a Simple summation method:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Runtime. Serialization;
Using System. ServiceModel;
Using System. Text;
Namespace SLDome. Web
{
// Note: You can use the "RENAME" command on the "refactoring" menu to change the Interface Name "ISLS" in the Code and configuration file at the same time ".
[ServiceContract]
Public interface ISLS
{
[OperationContract]
Int Add (int number1, int number2 );
}
}
 
========================================================== ============================================
Then we can implement this interface:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Runtime. Serialization;
Using System. ServiceModel;
Using System. Text;
Namespace SLDome. Web
{
// Note: You can use the "RENAME" command on the "refactoring" menu to change the class name "SLS" in the code, svc, and configuration file at the same time ".
// Note: To start the WCF test client to test the service, select SLS. svc or SLS. svc. cs in Solution Explorer and start debugging.
Public class SLS: ISLS
{
Public int Add (int number1, int number2)
{
Return number1 + number2;
}
}
}
 
========================================================== ========================================================== =
Now let's set the foreground page:

<UserControl x: Class = "SLDome. MainPage"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
Mc: Ignorable = "d"
D: DesignHeight = "300" d: DesignWidth = "400">
<Grid x: Name = "LayoutRoot" Background = "White">
<TextBox HorizontalAlignment = "Left" Height = "23" Margin = "118,58, 120 "TextWrapping =" Wrap "Name =" txtName "verticalignment =" Top "Width =" "/>
<TextBox HorizontalAlignment = "Left" Height = "23" Margin = "118,113, 120 "TextWrapping =" Wrap "Name =" txtPwd "verticalignment =" Top "Width =" "/>
& Lt; Button Content = "WCF" Name = "btnWcf" HorizontalAlignment = "Left" Margin = "46,216, 0, 0 "VerticalAlignment =" Top "Width =" 75 "Click =" btnWcf_Click "/>
<Button Content = "General handler" Name = "btnHandler" HorizontalAlignment = "Left" Margin = "211,216, 110 "VerticalAlignment =" Top "Width =" "Click =" btnHandler_Click "/>
</Grid>
</UserControl>
 
In this way, our wcf is ready. below is the code of our general processing program:
Create a page for the general processing program to process user login:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Namespace SLDome. Web
{
/// <Summary>
/// LoginHandler Summary
/// </Summary>
Public class LoginHandler: IHttpHandler
{
Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
String name = context. Request ["txtName"]. ToString ();
String password = context. Request ["txtPwd"]. ToString ();
If (name = "admin ")
{
If (password = "admin123 ")
{
Context. Response. Write ("Login successful! ");
}
Else
{
Context. Response. Write ("Incorrect password! ");
}
}
Else
{
Context. Response. Write ("the user name does not exist! ");
}
}
Public bool IsReusable
{
Get
{
Return false;
}
}
}
}
 
========================================================== ==========================================================
Then we will handle this general handler:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Namespace SLDome
{
Public partial class MainPage: UserControl
{
Public MainPage ()
{
InitializeComponent ();
}
Private void btnWcf_Click (object sender, RoutedEventArgs e)
{
ServiceReference1.SLSClient SC = new ServiceReference1.SLSClient ();
SC. AddAsync (10, 20 );
SC. AddCompleted + = SC _AddCompleted;
}
Void SC _AddCompleted (object sender, ServiceReference1.AddCompletedEventArgs e)
{
MessageBox. Show (e. Result. ToString ());
}
Private void btnHandler_Click (object sender, RoutedEventArgs e)
{
String name = txtName. Text. Trim ();
String password = txtPwd. Text. Trim ();
WebClient wc = new WebClient ();
Wc. DownloadStringAsync (new Uri ("http: // localhost: 7090/LoginHandler. ashx? TxtName = "+ name +" & txtPwd = "+ password, UriKind. RelativeOrAbsolute ));
Wc. DownloadStringCompleted + = wc_DownloadStringCompleted;

}

Void wc_DownloadStringCompleted (object sender, DownloadStringCompletedEventArgs e)
{
MessageBox. Show (e. Result );
}
}
}
 

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.