Friday 20 April 2012

Visual Studio 2010 - MS Sql 2012 - ASP.NET - How to create server and client using WCF PART 1

In this example I will create simple WCF Data Services Server that will expose some data to Client.
More information you can find watching video below.

This example is very similar to the one posted here http://howtodomssqlcsharpexcelaccess.blogspot.ca/2012/03/visual-studio-2010-ms-sql-2012-aspnet.html

I have simple database called School with 2 tables Dpt and Crs.  Dpt is a list of departments and has only 2 columns DepartmentId and DepartmentName. DepartmentId has Identity Specification set to 1.


Firs I create Server - In visual studio add new project - ASP.NET Empty Web Application and call it Server

 Add new item to project - ADO.NET Entity Data Model and name it SchoolModel


Select Generate from database and click Next

 Select connection string (or create new one), name it SchoolEntities and click Next


Select 2 tables that I mentioned above (Dpt and Crs), name Model Namespace SchoolModel and click Finish.


 Entity has been created.

Add new item to the project - WCF Data Service and call it SchoolService.


Modify the code so it looks like this:

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace Server
{
    public class SchoolService : DataService<SchoolEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
    }
}

Make your SchoolService.svc default page

And run


Server is ready - now let's create client.

Part 1
Part 2
Part 3







No comments:

Post a Comment