15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using WCf Data services with the aDo.neT entity framework ❘ 895<br />

Now, use the Data Service template <strong>and</strong> create RestaurantDataService.svc . The .svc fi le contains the<br />

ASP.<strong>NET</strong> ServiceHost directive <strong>and</strong> uses the DataServiceHostFactory to instantiate the data service on<br />

request.<br />

< %@ ServiceHost Language="<strong>C#</strong>" Factory="System.Data.Services.DataServiceHostFactory,<br />

System.Data.Services, Version=4.0.0.0, Culture=neutral,<br />

PublicKeyToken=b77a5c561934e089"<br />

Service="Wrox.ProCSharp.DataServices.RestaurantDataService" % ><br />

code snippet RestaurantDataServiceWeb/RestaurantDataService.svc<br />

With the code - behind, you need to change the template parameter of the DataService < T > class to reference<br />

the previously created entity data service context class, <strong>and</strong> change the entity set access rule <strong>and</strong> service<br />

operations access rule to allow access:<br />

using System.Data.Services;<br />

using System.Data.Services.Common;<br />

namespace Wrox.ProCSharp.DataServices<br />

{<br />

public class RestaurantDataService : DataService < RestaurantEntities ><br />

{<br />

// This method is called only once to initialize service - wide policies.<br />

public static void InitializeService(DataServiceConfiguration config)<br />

{<br />

config.SetEntitySetAccessRule(“Menus”, EntitySetRights.All);<br />

config.SetEntitySetAccessRule(“Categories”, EntitySetRights.All);<br />

}<br />

}<br />

}<br />

config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;<br />

code snippet RestaurantDataServiceWeb/RestaurantDataService.svc.cs<br />

Now, you can use a web browser to invoke queries as before to this data service; for example, you can use<br />

http://localhost:13617/RestaurantDataService.svc/Menus to receive the AtomPub of all menus<br />

from the database. Next, you create a client application that makes use of the client part of Data Services.<br />

With a large database you shouldn’t return all the items with a query. Of course, the<br />

client can restrict the query to just request a certain limit of items. However, can you<br />

trust the client With confi guration options you can restrict limits on the server. For<br />

example, by setting config.MaxResultsPerCollection you can restrict the number<br />

of items that are returned from a collection to a maximum. You can also confi gure the<br />

maximum batch count, the maximum number of objects on an insert, <strong>and</strong> the maximum<br />

depth of objects in the tree. As an alternative, to allow any query you can also<br />

defi ne service operations as was shown in the section “Additional Service Operations.”<br />

.neT applications using system.data.service.Client<br />

Earlier in this chapter, you read how a .<strong>NET</strong> client application can be created that simply sends HTTP<br />

requests by using the HttpWebRequest class. The client part of Data Services, with the namespace<br />

System.Data.Services.Client , offers functionality for the client to build HTTP requests. The two most<br />

important classes with this namespace are DataServiceContext <strong>and</strong> DataServiceQuery < TElement > .<br />

DataServiceContext represents the state that is managed on the client. This state keeps track of objects<br />

that are loaded from the server as well as all the changes made on the client. DataServiceQuery < TElement ><br />

represents an HTTP query to a data service.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!