Lập trình .net 4.0 và visual studio 2010 part 31 ppsx

7 262 0
Lập trình .net 4.0 và visual studio 2010 part 31 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 9    207 WCF Data Services Availability: .NET 3.5SP1 (limited functionality) onwards WCF Data Services (previously “Astoria” and ADO.NET Data Services) allows data to be modified and exposed over an HTTP RESTful interface. WCF Data Services (WDS) contains a rich query language and can be accessed easily with automatically generated proxy classes or crafting raw HTTP requests. WCF Data Services supports returning data in a number of popular data formats such as XML, AtomPub, and JSON and is potentially very useful for integration scenarios and applications that don’t maintain a direct connection to the database such as Silverlight.  NOTE During the course of writing this chapter, Microsoft changed the name of ADO.NET Data Services to WCF Data Services. However, the VS template names have not changed yet, so the examples in this chapter use the ADO.NET Data Services template names. Hello WCF Data Services Before we can get started with WDS we are going to need some data to play with. If you haven’t already done so, please refer to the introduction and set up the example database. In this chapter we will be using SQL Server 2008, but don’t think that you are limited to using just SQL Server since WDS will work with anything supported by Entity Framework (Chapter 8). To expose our data we have to perform four steps: 1. Create Entity Framework classes for the data we want to expose 2. Create a host ASP.NET application for the WDS service 3. Create the WDS service 4. Configure access rules for the service Let’s get started. Open Visual Studio and create a new ASP.NET web site; change the Web location dropdown to HTTP and enter the location as http://localhost/Chapter9/. CHAPTER 9  WCF DATA SERVICES 208  WARNING Hosting WCF Data Services in IIS on one machine gave me HTTP 500 whenever I tried to query data. I never got to the bottom of why this was, so all I can say is if you experience this try working with the local web server instead. Entity Framework WDS needs to know how the data we want to expose is structured and related. We will utilize the Entity Framework to provide this information: 1. Add a new ADO.NET entity data model to the project. 2. Call the ADO.NET entity data model Chapter9Model.edmx. 3. Click Add. Figure 9-1. Adding ADO.NET entity data model 4. Visual Studio will ask you about placing these files in the App_Code directory. Agree to this. CHAPTER 9  WCF DATA SERVICES 209 5. Visual Studio will now ask you how you want it to generate the model. Select the "Generate from database model" option and then click Next. Figure 9-2. Generate model from database 6. If you don’t have a connection already to the example database, then create one by clicking New Connection and enter the connection details for the example database. Figure 9-3. Creating a new database connection CHAPTER 9  WCF DATA SERVICES 210 7. Visual Studio will now examine the database structure and present you with a screen similar to Figure 9-4, where you select the items to generate EF classes for. Expand the Tables node to show all the available tables. Figure 9-4. Selecting items to generate EF classes for 8. Put a check against each individual table apart from sysdiagrams. 9. Ensure that the “Pluralize or singularize generated object names” checkbox is checked. 10. Ensure the Model Namespace box is set to Models. 11. Click Finish. 12. Click OK. VS2010 will then generate EF classes for the database and display the design surface (Figure 9-5): CHAPTER 9  WCF DATA SERVICES 211 Figure 9-5. Entity model Creating a Data Service All that is left to do now is to expose the EF classes by adding a new data service and configuring the rules to access it. 1. Add a new ADO.NET data service to your project called MovieService.svc. 2. Click OK. 3. Open ~/MovieService.cs. CHAPTER 9  WCF DATA SERVICES 212 4. You now need to tell WDS what type of class your service will expose. Amend the code to the following: public class MovieService : DataService<Models.BookEntities> { // This method is called only once to initialize service-wide policies. public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.AllRead); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } }  WARNING In my example, DataService accepts a type called BookEntities, but yours may be different if you have a different database name. VS by default prefixes the entities with the database name (you can amend this in Chapter9Model.Designer.cs if you wish). IE Content Settings By default, WDS will return XML when we query it. It can be useful to view this returned XML in a browser such as Internet Explorer while we are testing it. However, by default when Internet Explorer processes the results of a data service, it will think that it is working with an RSS feed as the data is returned in AtomPub form. This is not very helpful for us, so to see the raw XML we need to change a setting in IE: 1. Open IE and go to the ToolsInternet OptionsContent tab. 2. Click the Settings button in the Feed section and uncheck the box marked “Turn on feed reading view” (Figure 9-6). Figure 9-6. Altering content view setting in IE8 CHAPTER 9  WCF DATA SERVICES 213 Hello WDS OK, we are now ready to work with our data service. 1. Right click on MovieService.svc and select Set As Start Page. 2. Press F5 to run your application. If all is well then you will see a screen similar to Figure 9-7 showing an XML representation of our EF classes: Figure 9-7. Output from accessing our test service Querying WCF Data Services WCF Data Services uses the URL to pass query parameters. For example, to retrieve the film entities, add /Films to the end of the existing URL (for example, http://localhost/Chapter9/MovieService.svc/Films). You should then be returned a list of all the films in AtomPub format (Figure 9-8): . a new ADO .NET entity data model to the project. 2. Call the ADO .NET entity data model Chapter9Model.edmx. 3. Click Add. Figure 9-1. Adding ADO .NET entity data model 4. Visual Studio will. Create a host ASP .NET application for the WDS service 3. Create the WDS service 4. Configure access rules for the service Let’s get started. Open Visual Studio and create a new ASP .NET web site;. connection CHAPTER 9  WCF DATA SERVICES 2 10 7. Visual Studio will now examine the database structure and present you with a screen similar to Figure 9 -4, where you select the items to generate

Ngày đăng: 01/07/2014, 21:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan