Using Ria Services Soap and Windows Phone 7
I remember last year or so at this time, I began to glimpse Ria Services, from that preview so far Ria Services grew strongly and became something more just than a framework for creating applications RAD (Rapid Application Development) for Silverlight.
Ria Services initially was conceived to expose our business logic as a service, taking as a model our data access layer of EF, and get our domain in a client-side context to work with Silverlight in a dynamic manner, facilitating thus the development and access to data.
However for large scale applications we may have other requirements, such as: Exposing our services in a way that they can be consumed from other systems, or decouple the business logic from the services, to make it interchangeable or use other tools like workflow in the same, and/or perhaps also to decouple the data access layer to use other ORM than EF .
For both problems there are solutions that Ria Services team has implemented in order to continue using the facilities that it has given us working on Silverlight.
For now we will focus on the first problem, the interaction with other systems in the next article I`ll deal with the decoupling between layers.
With a friend we’ve share a vision of using RIA Services for an application that could access these services from external system in this case Windows 7 Phone!!!.
Make sure we have the latest version of Ria Services: http://www.silverlight.net/getstarted/riaservices/
1. First of all we need to create a Web application in Asp.Net as Empty.
2. In this case, we can designate it as “SoapRiaService”.
3. Add a database where we will test our Employee table .
4. Then select Entity Framework 4 new item and write it the database conection name, you could use “SoapTestDbEntities”.
5. For this test and for now we will take the Employees table to create our mapping
6. Ok, we already have our entity Employee in our ORM. Now we need to do a solution build.
7. As a next step we need to create our domain service and get the interface for the basic CRUD operations in order to make it accessible to our client.
To achieve this we need to add a new component called Domain Service Class that is our Ria Service. In this case we may call it “EmployeeDomainService”.
8. When we add a Domain Services class using the wizard we can choose the entity that we need to apply the editing operations from the context of EF.
Choose properties enabling editing metadata generation, which will allow us to access the properties of validation of the entity, and also choose to expose an endpoint of Odate (REST) http://www.odata.org/
9. For each method we add the decorator [invoke] in that way we generate the corresponding proxy methods in the client.
10) The Namespace and DomainServiceNameClass, give us a clue to find out the generated WCF service. The service address is:
1. http:// [host: port] / Services / [namespace] – [DomainServiceNameClass]. svc (the ‘.’ are replaced by ‘–’ )
2. Example: http://localhost:37390/Services/SoapRiaService-EmployeeDomainService.svc
11. Now we need to add the endpoint to access the SOAP services, if we do not want a REST enpoint , just remove the OData endpoint.
12. Having all the logic and configuration ready in our application we need to make sure to add the library Microsoft.ServiceModel.DomainServices.Hosting.dll address shown below.
But you have this path, be sure to install or reinstall WCF Silverlight RIA Services V1.0 for 4 and Visual Studio 2010 http://www.microsoft.com/downloads/en/details.aspx?FamilyID=cd3191a1-def4-4caa-8120-1f0bbcf4bb05&displaylang=en
13. After adding the library and compiled the project, we just need to try it (F5) to be host in the IIS test server and then we can see our service at the following address:
14. If we want to see the WSDL Contract http://localhost:37390/Services/SoapRiaService-EmployeeDomainService.svc?wsdl
15. Now we’ve ready our service, to create our application in Windows Phone 7, first download the developer tools http://developer.windowsphone.com/ once installed create a new Windows project Panorama Phone Application (COOL!). use “WP7SoapRiaService” to name it, if you want.
16. Once created the application we’ll need to add a service reference as an address setting http://localhost:37390/Services/SoapRiaService-EmployeeDomainService.svc, then click on GO,at this time you should now see our service and operations exposed by Ria Services, rename the namespace to “EmployeeService” for example.
17. Let see the proxy classes that were created after giving Ok double clicking on the service reference.
18. Well now as a next step we could add the Reactive eXtensions(RX) reference ,
to control the asynchronous call to our Web Service, yap Reactive Extensions now comes within the Windows SDK Phone 7!!!.
19. Once you add the reference add the following code, on my last post I’ve explained RX, and I just added a spinner property to control the visibility:
20. Add a Spinner and bind it to the Visibility property, to control its visibility depending the data in our collection.
21. Run the application, and done you have the application ready!!
The first time when you execute this test you should be patience, because it could take a little longer time until you get the information from the server.
I Hope this post could be useful 😀
May the Code be with you!!!
This work is licensed under a Creative Commons Attribution 3.0 Unported License