Showing posts with label WCF Data Services. Show all posts
Showing posts with label WCF Data Services. Show all posts

Tuesday, 13 November 2012

What is WCF Data Service?

Yes, WCF Data Service is used to expose data to clients/consumers like a way of REST nature.



menas REST uses Http vebs like: GET,PUT,DELETE,SET , as well as WCF Data Services using OData

Protocol to exchange data between Service and Clients.

OData Protol uses the simple verbs to expose Entities/Resoures/data to clients in REST based nature.

ADO.NET Entity Framework allows u work with OData Protol communication using WCF Data Service.

once our Entity Data Model is prepared then which ready to Consume using OData Protol communication.

Ex :


yes, just i created one console appliation(best take Class Library) which the name of EntityFrameworkDemo  then i add ADO.NET
Entity Model temple ---->
Using Northwind database.

Now I am created one asp.net Empty asp.net website to expose our service to clients --->ExposeWcfDataService
then add the Refernce of my Data/Entities/Resoures using Add Reference.---

EntityFrameworkDemo to our ExposeWcfDataService. then everythis is ready. so Know we have prepare

wcf Data Service --> RightClick ---ExposeWcfService(asp.netempty webseit). then add WCF Data Service templet to project.

now we added a service with the name is :WcfNorthwindDataService
asdf
we know host the service in wcf using : ServiceHost class.  like in

DataServiceHost also but here use host the service for selfHosting.

DataService<T> is a genic type which can host the service.which entry point for our
ADO.net Data Services.

like here



public class WcfNorthwindDataService : DataService<EntityFrameWorkDemo.NorthwindEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
          
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
    }

Here :
setEntitySetAccessRule--> * means i want expose all entities/resoues to client. Entity


EntitySetRights: clients can perform all operartion on my entities/resources.

 now add the Cofing from our library/northwind config from our app.config then added in our
WcfNorthwindDataService in web.config

<connectionStrings>

<add name="NorthwindEntities" connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=GM-VHOP-KASI;Initial Catalog=NORTHWND;Persist Security Info=True;User ID=sa;Password=zolt123$;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

</connectionStrings>

now Run our wcf Data service----> WcfNorthwindDataService (.svc)



Internet Explorer: Internet Options--Content---Feed and web Slice Setting to 

disable  :  Turn on feed Reading view // to view the contet---> unchek if it is checked mode.


   now my WCF Data Service is running then Client can consume the this Service.

we have many commands called as $ Commands to query the data in Auto xml format in browser support

like
http://localhost:3790/WcfNorthwindDataService.svc/Customers-----> all customers

http://localhost:3790/WcfNorthwindDataService.svc/Customers

for specific we have top(1),skip()....methods and overby, desc,...ect $ command available to check the data.

Now Consume the Service.

create the Client(WCFDataServiceClient) the add the above Service reference. 



sdfa

yes, service Reference is added at client.then expand ServiceReferenc1---find the service.edmx file at client.
means the Sehema/Conceptual model is available to client to work with service.

so as per our 
Conceptual Sechma to Client


onfig.SetEntitySetAccessRule("*"EntitySetRights.All);---at service hosting 

client can perform any operation at every entitiy.[categories,Products,...ect].

Ex:



class Program
    {
        static void Main(string[] args)
        {

           Uri uri=new Uri("http://localhost:3790/WcfNorthwindDataService.svc/");

            NorthwindEntities proxy = new NorthwindEntities(uri);
 MyPerson person = new MyPerson { ssname = "WCF Data Service", abcd = "Excellent" };
            proxy.AddObject("MyPersons", person);
            try
            {
                proxy.SaveChanges();
                Console.Write("Inserted succefully....");
            }
            catch (Exception)
            {

                Console.Write("Error Occured");
            }

            Console.Read();


        }
    }

output 


Few points regarding WCF Data Services:

using WDS we can query(retrive) the data  URI based.  // REST style

using WDS we can sent/retrive the data in Auto-XML based/ JSON based.

using OData Model browser we can able view Designer view also.// tables with their relations.

using New Versions we can able
DataServiceProtocolVersion.v1/v2  ...retriveal command in browser also improved.