[Silverlight entry series] independent deployment of WCF Ria Service (Independent/cross-origin)

Source: Internet
Author: User

Create a Silverlight project in vs2010 and select to support the WCF Ria service, which can easily implement the WCF Ria service and be called by the siverlight client. However, the problem is that this mode is tightly coupled. How to independently develop and deploy a WCF Ria Service to achieve loose coupling between Silverlight and background services? What are the advantages? We can have another team responsible for developing the service layer of the WCF Ria service. Silverlight or other clients can call the services of this service layer to achieve loose system coupling, share services to Achieve Synchronous and collaborative development of teams. How? Let's take a step.

1. Download the WCF Ria service toolkit oct2010: http://www.microsoft.com/downloads/en/details.aspx? Familyid = a23325ef-7b1f-4c92-9fd5-ffee48f7c7bc

2. Create a New silverlight4 application in vs2010 and an empty project.

3. Since the WCF Ria service relies on websites, a new Asp.net web application is created in vs2010. Add an ado.net data entity model to this project (link to the database, how can I Google this process)

4. Add a domain service class named domainservice1, which is for the entity in the previous step. Several methods are automatically generated.

5. Add a Silverlight enabled WCF Service:

6. Write a method in it and call the domain service method in Step 4 above,Code:

 UsingSystem;
Using System. LINQ;
Using System. runtime. serialization;
Using System. servicemodel;
Using System. servicemodel. activation;

Namespace Webapplication1
{
[Servicecontract (namespace =   "" )]
[Aspnetcompatibilityrequirements (requirementsmode = Aspnetcompatibilityrequirementsmode. Allowed)]
Public   Class Slenalbedwcfservice1
{
Domainservice1 S1 =   New Domainservice1 ();

[Operationcontract]
Public StringGetfirsttestrecord ()
{
ReturnS1.gettestlist (). First (). recordname;
}

//Add more operations here and mark them with [operationcontract]
}
}

Note:: If the return value of this method is not a basic type, serialization is required, or the custom type with the datacontract keyword WCF will automatically serialize it. For example, an object class is returned:

 1   Using  System;
2   Using System. Collections. Generic;
3   Using System. LINQ;
4   Using System. Web;
5   Using System. runtime. serialization;
6
7   Namespace Mywcfservices
8 {
9 [Datacontract]
10 Public Class Myentity
11 {
12 [Datamember]
13 Public String [] Myfield1 { Get ; Set ;}
14
15 [Datamember]
16 Public String [] Myfield2 { Get ; Set ;}
17
18 }
19 }

7. Add a file named clientaccesspolicy. xml. The content is as follows, which is used to solve cross-origin problems and supports cross-origin access:

<? XML version = "1.0" encoding = "UTF-8" ?>  
< Access-Policy >  
< Cross-Domain-Access >  
< Policy >  
< Allow-from HTTP-request-headers = "*" >  
< Domain Uri = "*" />  
</ Allow-from >  
< Grant- >  
< Resource Path = "/" Include-subpaths = "True" />  
</ Grant- >  
</ Policy >  
</ Cross-Domain-Access >  
</ Access-Policy >

8. Start the website (or deploy the website in IIS)

9. access the website in IE and access the SVC address of the WCF Service, for example, http: // localhost: 7354/slenalbedwcfservice1.svc.

10. Click the first link to display the WSDL content. copy URL: the WSDL address in the IE Address Bar.

11. In the previously created Silverlight project, right-click the Silverlight Project (not hosting the website. Web) and add service reference)

12. Enter the copied WSDL address, click Go, and enter the desired namespace (whatever you want ),

13. vs2010 will automatically generate a proxy class for you and modify the config file

14. In mainpage. XAML. CS, you can call this service method:

 UsingSystem;

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 Silverlightapplication1
{
Public   Partial   Class Mainpage: usercontrol
{
// Servicereference1 is the namespace you entered in step 12.
Servicereference1.slenalbedwcfservice1client Client =   New Servicereference1.slenalbedwcfservice1client ();

PublicMainpage ()
{
Initializecomponent ();

// Cross-origin calls to the WCF Ria service by Silverlight must be asynchronous (using winform to call the WCF Ria service can be synchronous !)
Client. getfirstrecordcompleted + =   Delegate ( Object Sender, servicereference1.getfirstrecordcompletedeventargs e ){
If (E. Error =   Null )
This . Label2.content = E. result;
Else
This . Label2.content = E. Error. message;
};
Client. getfirstrecordasync ();
}
}
}

15. If you have other permission settings in IIS, you must also start the WCF Service.

16. Independently deploy the WCF Ria service to solve cross-domain problems, etc. Here there are some other foreign resources:

Http://blogs.msdn.com/ B /brada/archive/2009/11/22/ria-services-a-domainservice-is-a-wcf-service-add-service-reference.aspx

Http://timheuer.com/blog/archive/2009/12/10/tips-to-deploy-ria-services-troubleshoot.aspx

http://blogs.msdn.com/ B /drnick/archive/2009/12/30/deploying-wcf-ria-services-system-files.aspx

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.