Building a Host for ADO.NET Data Service
Building a Host for ADO.NET Data Service
After you built your
data service you will
probably want to deploy
it. In the following post
I’m going to explain
how you can build
a simple host for an ADO.NET data service without the use of svc file.
Data Service Hosting
The ADO.NET data services are not autonomous server entities.
They live in the context of their service host such as WCF or ASP.NET.
The data service host is handling the direct interactions with the network
and supports caching, scalability and authentication modules for the data service.
DataServiceHost Class
The first thing to know if you want to deploy a data service is the DataServiceHost
class. The DataServiceHost derives from WCF WebServiceHost class and holds it
capabilities. The WebServiceHost class automatically configures address, binding and
contract for its underlining service and therefore eliminates the need for lots of
configurations. Using the DataServiceHost simplifies the deployment scenario.
A Simple Deployment Example
The following example is a console application the hosts a simple data service:
#region Classes
public class Course
{
#region Properties
public int CourseID { get; set; }
public string Title { get; set; }
#endregion
}
public class CourseContext
{
#region Members
private static List<Course> _courses;
#endregion
#region Properties
public IQueryable<Course> Courses
{
get
{
return _courses.AsQueryable();
}
}
#endregion
#region Ctor
public CourseContext()
{
_courses = new List<Course> {
new Course{ CourseID= 1, Title="Calculus"},
new Course{CourseID = 1, Title="Data Structures"}
};
}
#endregion
}
#endregion
#region Data Service
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class SimpleDataService : DataService<CourseContext>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
}
}
#endregion
class Program
{
static void Main(string[] args)
{
Uri baseAddress =
new Uri(ConfigurationManager.AppSettings["HostLocation"]);
DataServiceHost host =
new DataServiceHost(typeof(SimpleDataService),
new Uri[] { baseAddress });
host.Open();
Console.WriteLine(
string.Format("Host running - {0}",
ConfigurationManager.AppSettings["HostLocation"]));
Console.WriteLine("Press any key to shut down service...");
Console.ReadKey();
host.Close();
}
}
The example include an entity class that is called Course, a data context
that is called CourseContext, a data service that is called SimpleDataService
and the program itself. The program creates a new DataServiceHost and
gives it the URI to work with. The URI is located in the application settings of
the web.config file. The value of the HostLocation key is http://localhost:8090/.
That is all. The DataServiceHost will instantiate the data service and will host it
for you.
Summary
Lets sum up, it is very easy to build a WCF hosting environment for our data service
using the DataServiceHost class. In the post I showed a simple deploy example
of how to do it. The post didn’t discuss the regular web deployment scenarios
that you can use to deploy data services (such as deploy an ASP.NET
application/site that includes a data service into IIS for example).
CodeProject