Objective In this article, I will show how
- WCF Service can be created from the scratch
- WCF Service can be hosted in Console Application
You can see
video of this article
here.
You can also follow my
blogLet us say, we
have a table called Student of below structure below in our
database
In
Student table RollNumber is a primary key.
Let us create WCF Data
Service which will expose the above table as REST service
Step
1Create a new project. Select the project template Console
Application from Windows tab.
Step
2: Create a Data Model We can create a Data Model, which can be
exposed as WCF Data Service in three ways
- Using ADO.Net Entity model.
- Using LINQ to SQL class.
- Custom Data Model.
For our purpose, I am going to use ADO.Net
Entity model to create the data model. So to create an entity model
- Right click on Console application and add a new item
- Select ADO.Net Entity model from Data tab.

- Since we have table in data base. So we are going to choose option, select
from database.

- Either choose the data base from drop down or create a new data
connection.

In
above connection string StudentDBEntities is name of the connection string. If
we want, we can change this connection string as per our requirement.
If
your required data base is not listed in drop down then, you can create a new
data connection. To create new data connection click on New
Connection

You
can give the data base server name and press refresh. After pressing Refresh,
you can choose the data base from the drop down. After selecting the data base
click on Test Connection to test connection established successfully or not?
- Select tables, views and stored procedure from data base you want to make as
the part of your data model. Since we are having only one table so we are
selecting one table.

If
you want you can change name of the data model. By default it is name of the
data base appended by the term model. Click on Finish button to complete and
create the data model.
- Now we can see that StudentDataModel.edmx has been created in the
designer.

Since
there is only one table, so there is only one table model at design
surface.
Now we have created the data model which can be exposed as WCF
Data Service. Now you can see in solution explorer, you have StudentModel.edmx
and StdentModel.Designer.cs files.

Step
3Add reference
of
System.ServiceModel;
System.ServiceModel.Web;
System.Data.Services;
System.Data.Services.Client
Step
4Right click and add a class in console application. I am giving
name here MyDataService to the class.
Step
5Add the below namespaces in the class
MyDataService
Step
6 In this step we will create WCF Data Service
class.
- Make the class as public.
- Inherit DataService class.
- Create a public method InitializeService
- Pass the input parameter of the type DataServiceConfiguration
- Set the protocol version to DataServiceProtocol version
2.0
MyDataService.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Data.Services;
using System.Data.Services.Common;
namespace
SelfHosted
{
public class MyDataService :
DataService<
StudentDBEntities>
{
public static void InitializeService(
DataServiceConfiguration config)
{
config.SetEntitySetAccessRule(
"*",
EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion =
DataServiceProtocolVersion.V2;
}
}
}
Step 7Host the WCF Data Service in Console
application. So to do that , write the below code in Program.cs
- Create instance of WebServiceHost.
- Create instance of WebHttpBinding.
- Add service end point with Contract IRequestHandler
- Open the host
Full source code for hosting program is as
below,
Programs.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Data.Services;
using System.Data.Services.Common;
namespace
SelfHosted
{
class Program {
static void
Main(
string[] args)
{
WebServiceHost host =
new WebServiceHost(
typeof(
DataService),
new Uri(
"http://localhost:9999/DataService"));
WebHttpBinding binding =
new WebHttpBinding();
host.AddServiceEndpoint(
typeof(
IRequestHandler),
binding,
"WebServiceHost");
host.Open();
Console.WriteLine(
"Service at http://localhost:9999/DataService
");
Console.WriteLine(
"Press any key to stop the Service ");
Console.Read();
host.Close();
}
}
}
Step 8Just
press F5 to run the WCF Data Service. Data Service will be hosted in the console
application.
On running you can see, one table is listed. That table
is Student
So,
we are able to run the WCF Data Service and this service Is hosted in console
application. We can consume the service in any client in the same way; we
consume WCF Data service hosted in any other web server.
You can also download sample project from below link
I hope this
post was useful, thanks for reading. Happy coding.