15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1108 ❘ ChaPTer 38 silverliGht<br />

using System.Collections.Generic;<br />

using System.Data.Services;<br />

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

using System.Linq;<br />

using System.ServiceModel.Web;<br />

namespace Wrox.ProCSharp.Silverlight.Web<br />

{<br />

public class EventRegistrationDataService :<br />

DataService<br />

{<br />

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

{<br />

config.SetEntitySetAccessRule("Events", EntitySetRights.AllRead);<br />

config.SetEntitySetAccessRule("Attendees", EntitySetRights.All);<br />

config.SetServiceOperationAccessRule("AddAttendee",<br />

ServiceOperationRights.All);<br />

config.DataServiceBehavior.MaxProtocolVersion =<br />

DataServiceProtocolVersion.V2;<br />

}<br />

code snippet SilverlightDemos.Web/EventRegistrationDataService.svc.cs<br />

To make an operation available for registration of an attendee using WCF Data Services that is<br />

more complex than just adding a new record, the method AddAttendee() is added to the class<br />

EventRegistrationDataService. The WebGet attribute specifies that the method can be accessed<br />

with an HTTP GET request. For POST, PUT, or DELETE requests, the WebInvoke attribute can be<br />

used. Within the InitializeService() method, access is given to this operation with the method<br />

SetServiceOperationAccessRule(). AddAttendee() receives attendee information with separate<br />

parameters <strong>and</strong> first verifies that the registrationCode exists before writing the attendee to the<br />

Attendees table that is mapped from the Entity Data Model. Using separate parameters with this method<br />

instead of using an Attendee object is required to call this method with an HTTP GET request.<br />

}<br />

}<br />

[WebGet]<br />

public bool AddAttendee(string name, string email, string company,<br />

string registrationCode, int eventId)<br />

{<br />

if ((from rc in this.CurrentDataSource.RegistrationCodes<br />

where rc.RegistrationCode1 == registrationCode<br />

select rc).Count() < 1)<br />

{<br />

return false;<br />

}<br />

else<br />

{<br />

var attendee = new Attendee {<br />

Name = name,<br />

Email = email,<br />

Company = company,<br />

RegistrationCode = registrationCode,<br />

EventId = eventId<br />

};<br />

this.CurrentDataSource.Attendees.AddObject(attendee);<br />

return !(this.CurrentDataSource.SaveChanges() < 1);<br />

}<br />

}<br />

Adding a service reference to the Silverlight control project EventRegistration makes it possible to use the<br />

WCF Data Services client classes to access the service. The method OnNavigatedTo() is now changed to use<br />

the generated proxy from the Data Services client instead of the pure WCF proxy used earlier.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!