in this article, WCF acts as a server that provides loginvaild services out of the box, and Silverlight acts as a client that invokes the Loginvaild service provided by WCF. The idea has, the following code implementation.
Database Scripting ImplementationCreate a new T_user table, add two fields to the table username, password, insert a data to the Table Admin Admin, the script is as follows:
Use [test]go/****** Object: Table [dbo].[ T_user] Script date:09/28/2014 21:12:02 ******/set ansi_nulls ongoset quoted_identifier ongoset ANSI_PADDING Ongocreate TABLE [dbo]. [T_user] ([username] [varchar] () not null,[password] [varchar] (a) not NULL, CONSTRAINT [Pk_t_user] PRIMARY KEY CLUSTERED ([Usern AME] ASC) with (pad_index = off, statistics_norecompute = off, Ignore_dup_key = off, allow_row_locks = ON, all Ow_page_locks = on) on [PRIMARY]) on [Primary]goset ansi_padding offgoinsert [dbo]. [T_user] ([username], [password]) VALUES (n ' admin ', n ' admin ')
WCF server implementations
1. Create a new WCF Service library project, in the default generated IServer1.cs interface in the Add Loginvaild Service declaration:
[OperationContract] BOOL Loginvaild (string userName, string password);
2. Add the ADO Entity Data Model file--MODEL.EDMX for access to the data table T_user;
3. Implement the Loginvaild method in Service1.svc:
public bool Loginvaild (string userName, string password) { bool result = false; Required access to the ADO Data entity Model using (sltestentitiessecond entities = new Sltestentitiessecond ()) { var user = Entities. T_user.where (c = c.username = = Username && C.password = = password). Singleordefault (); if (user = = null) { result = false; } else { result = true; } } return result; }
4. Add the cross-domain Access file ClientAccessPolicy.xml at the root of the project as follows:
<?xml version= "1.0" encoding= "Utf-8"?><access-policy> <cross-domain-access> <policy > <allow-from http-request-headers= "*" > <domain uri= "*"/> </allow-from> <grant-to> <resource path= "/" include-subpaths= "true"/> </grant-to> </policy > </cross-domain-access></access-policy>
5. Set the WCF server to use a specific port for access by selecting the WCF Server item--message properties-->web--> specific port, enter 1316. This guarantees that we can access the services provided by WCF every time through port 1316.
To this, the WCF server-side configuration is complete, select the Service1.svc file in the browser under the browser under WCF to provide the service.
Silverlight Client Implementation1. Create a new Silverlight application, add a service reference in the reference, enter the path to the Service1.svc file in the browser in the address bar, for example my is: http://localhost:1316/Service1.svc;2. New Silverlight User Control Login.xaml file, add user name, password textbox and login button on the display page;3. In the Login.xaml background , the user input is judged by invoking the services provided by WCF, with the following code:
private void Button1_Click (object sender, RoutedEventArgs e) {string userName = txtUserName. Text.trim (); string password = Txtpassword. Text.trim (); Service1client client = new Service1client (); Client. loginvaildcompleted + = new eventhandler<loginvaildcompletedeventargs> (client_loginvaildcompleted); Client. Loginvaildasync (userName, password); Client. Closeasync (); } void Client_loginvaildcompleted (object sender, Loginvaildcompletedeventargs e) {if (E.error = = NULL) {//messagebox.show (e.result.tostring ()); if (E.result = = True) {this. Content = new MainPage (); } else {MessageBox.Show ("User name or password is wrong!"); }} else {MessageBox.Show (e.error.tostring ()); } }
4. In the App.xaml configuration file Settings Login.xaml as the start page, the code is as follows:
private void Application_Startup (object sender, StartupEventArgs e) {this . RootVisual = new Login (); }
This completes the client configuration and runs the Silverlight client project to see the results. Source: Http://pan.baidu.com/s/1mgn3IEOnow looking back at the entire implementation process, a bit of service-oriented programming means: WCF provides a service, and then exposes the interface that accesses the service, and the project that wants to invoke this service can invoke the service provided by WCF whenever it adds a reference to the service. In the implementation process, there is a problem that has not been resolved, when configuring the ADO Entity Data Model in a WCF server project, if the SQL Server authentication method is logged in, the "server returned error not found" exception is reported Change to Windows authentication problem resolution. This is a solution to the problem, hoping that the great God can give an explanation.
SILVERLIGHT+WCF implementing cross-domain calls