Tuesday, September 16, 2008

ADO.NET Data Services - Part - III

To work with ADO.NET Data Services, you should ensure that you have .NET 3.5 Framework SP1 installed. Apart from ADO.NET Data Services SP1 brings to the table a lot of new features. For a list of new features in SP1 refer to this link (VS 2008 and .NET 3.5 SP1 Features).


Once you have the SP1 installed, open VS 2008 and open a new web application. Right click on the solution and add a new ADO.NET Data Entity model. The extension of the file will be edmx. A wizard will guide you through the process of creating an entity model based on a selected database. I will not get into the details of the wizard for the sake of brevity. The wizard is quite self-explanatory. The only thing worth mentioning is to set support for MARS resultsets in your advanced window when you click new connection.



Once you have run through the wizard, depending on the tables you have selected you will see a data model like the one shown in the picture.


You can also create an entity model only for the tables you want, by selecting the appropriate tables in the wizard. We will now go on to add the ADO.NET Data Service. You can do this by opening  the add new item dialog box and selecting ADO.NET Data Service from it.


Once you have added the svc, you will see the code behind. Lets take a look at the only method that exists.


The method InitializeService is the first method that will be called when your service is invoked.  Any code that you write in this method is applicable to all the data models that you expose through your web service. One of the key things that you will do here is taking care of security. Though .NET gives us some built-in methods to set rights, more often than not we will be writing our code to monitor these accesses. The InitializeService gives us a single place where we can write code for access privileges for the whole service.


Coming back to the code behind, there are a couple of things you have to change in here to get it up and working. You will have to include the name of the datasource class in your servce name declaration. Replace the “/* TODO: put your data source class name here */ with the name of your entity class. You can find out the name of the entity class by looking the class name from the designer.cs file.


Build and run the solution. You will get a response like the one shown in the picture. This is because currently you have not set the rights. To view or access the exposed services, you should give rights explicitly.



Open your code editor. In your InitializeMethod uncomment the two statements


config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);

config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);


Replace the “MyEntitySet” with the name of the Entity and “MyServiceOPeration” with the name of the operation or the methods that you write. To provide access to all the entities and the operations use “*”. In my code for testing, this is what I have done.


             config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

             config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);


That’s it!! Your service is now ready to be accessed. Build and run the solution again. You will see something like below. Look at how the results have changed just by including those two lines in the InitializeService method.



One thing I would like you to note is the format of the data. In the above example, data is being returned as ATOM feeds.


Now that we have built a service, we next have to build a client to access this service. More of that in the next part. Happy Coding!!!