AppFabric-enabled WCF Data Service Walkthrough (C#)
In this walkthrough I’ll show you how to use the AppFabric-enabled WCF Data Service (C#) template for Visual Studio 2010 to enhance your WCF Data Services by providing
- Monitoring events and errors to the AppFabric Data Store
- Eliminating the need to use the .svc extension in your URI
Requirements
Step 1 – Create a Web Application
- Start Visual Studio 2010
- Select File / New Project
- Choose a Web Application Template – for this example I used Empty Web Application
Step 2 – Add an ADO.NET Entity Data Model
- For my database I’m using the AdventureWorksLT sample database for SQL Server 2008R2
- Right click on your project and select Add / New Item…
- Select the ADO.NET Entity Data Model template
- Name the model AdventureWorks.edmx
- Select Generate From Database
- Connect to AdventureWorksLT and use the default settings
- Select all the tables by checking the Tables checkbox
Step 3 – Add an AppFabric-enabled WCF Data Service
Right click on your project and select Add / New Item…
Select Online Templates
In the search box type AppFabric
Select the AppFabric-enabled WCF Data Service (C#) template
Name it AdventureWorks.svc
Click Install to install the template on your machine
Step 4 – Modify the template code
We have added TODO tasks in the template to guide you through tasks you need to do to make your service ready
- Replace [[class name]] with AdventureWorksLTEntities
- Modify the SetEntitySetAccessRule code as shown for all entities (“*”)
- If you want to use a different route you can set that as well
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// Enable read only access to all entities
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
RouteTable.Routes.Add(
new ServiceRoute("AdventureWorks", new DataServiceHostFactory(), typeof(AdventureWorks)));
}
Step 5 – Modify the web.config
- In AdventureWorks.svc.cs expand #region Sample Config with End-To-End Monitoring enabled"
- Select the commented XML and uncomment it (Ctrl+K, Ctrl+U)
- Copy it to the clipboard
- Comment it again (Ctrl+K, Ctrl+C)
- Open web.config
- Paste the XML inside the <configuration> tag
</connectionStrings>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<diagnostics etwProviderId="830b12d1-bb5b-4887-aa3f-ab508fd4c8ba">
<endToEndTracing propagateActivity="true" messageFlowTracing="true" />
</diagnostics>
<behaviors>
<serviceBehaviors>
<behavior name="">
<etwTracking profileName="EndToEndMonitoring Tracking Profile" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="UrlRoutingModule"/>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<handlers>
<add
name="UrlRoutingHandler"
preCondition="integratedMode"
verb="*" path="UrlRouting.axd"
type="System.Web.HttpForbiddenHandler, System.Web,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
</handlers>
</system.webServer>
</configuration>
Step 6 – Run the Service in IIS
To see the events in Windows Server AppFabric you need to deploy the Web project to IIS or modify your project to host the solution in the local IIS Server. For this example I’m going to modify the project to host with the local IIS server. Note: This requires you to run Visual Studio as Administrator
If you are not running Visual Studio as Administrator, exit and restart Visual Studio as Administrator and reload your project. For more information see Using Visual Studio with IIS 7
Right click on the WebApplication and select properties
Go to the Web tab
Check Use Local IIS Web Server and click Create Virtual Directory
Press Ctrl+Shift+S to save your project settings (Debugging will not save them)
Open AdventureWorks.svc.cs
Press F5 to Debug
The Service will open in the debugger and show the service document
Remove the .svc extension from the URI and try it again. It will work without the .svc extension in the URI.
If this isn’t working for you see System.Web.Routing RouteTable not working with IIS?
If you want to disable the URI with the .svc extension (and eliminate the .svc file) see this postIf you see an error in the event log (Login failed for user 'IIS APPPOOL\DefaultAppPool'. Reason: Failed to open the explicitly specified database. [CLIENT: <local machine>]) You need to grant permission to IIS Users to access the database
USE [AdventureWorksLT]
GO
CREATE USER [IIS USERS] FOR LOGIN [BUILTIN\IIS_IUSRS]
GO
USE [AdventureWorksLT]
GO
EXEC sp_addrolemember N'db_datareader', N'IIS USERS'
GO
Step 7 – View Events in AppFabric
- Start IIS Manager
- Open the AppFabric Dashboard
- Click one of the links for WCF
- Right click on an event and select View All Related Events to see detailed events