This article walks through creating a N-Tier ASP.NET Web API application and Data layer using design patterns.
What is N-Tier architecture
“N-Tier architecture refers to the architecture of an application that has at least 3 logical layers -- or parts -- that are separate. Each layer interacts with only the layer directly below, and has specific function that it is responsible for”. [ Source: http://www.developerfusion.com/article/3058/boosting-your-net-application-performance/2/ ]

Overview : N-Tier architecture Client Layer: Client application is the service consumer, implements business logic and responsible for presentation.
Service Layer: The Service provider, provides access to data as Web service, acts as communication channel between clients and data layer. Clients interact with data layer through service layer.
Data Layer: Persistent storage of data using any technology i.e., SQL Server, Oracle and MySQL
What are Design Patterns
Design patterns are solutions to software design problems that are found again and again in real world application development. Patterns are about reusable designs and interactions of objects. Design patterns are grouped into Creational, Structural and Behavioural categories.
In this N-Tier architecture below listed patterns are implemented.
Façade: Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Service layer is an implementation of the Facade design pattern (single point of entry). Service layer is responsible for calling and managing business objects, data access objects and processing the standard business logic and validating incoming requests.
Dependency Injection: IContactsManagerRepository (Interface) from data layer is injected in ContactsManagerController (Service layer) class through constructor. Inversion of Control container Unity is used to map concrete implementation to interface in Global.asax.
DI allows removing hard-coded dependencies and making it possible to change them, whether at run-time or compile-time. [Refer: http://www.martinfowler.com/articles/injection.html
Request-Response: Request/Response is the most basic and common of the client-service interaction patterns. It is used when the client must have an immediate response or wants the service to complete a task without delay. Request/Response begins when the client establishes a connection to the service. Once a connection has been established, the client sends its request and waits for a response. The service processes the request as soon as it is received and returns a response over the same connection. [source: http://servicedesignpatterns.com/ClientServiceInteractions/RequestResponse]
Data Transfer Object: DTOs are simple objects that hold data without any behaviour, responsible for storage and retrieval of own data. DTOs used in model folder of the Service layer [Refer: http://martinfowler.com/eaaCatalog/dataTransferObject.html]
Factory: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Implements the concept of factories and deals with the problem of creating objects (Employee) without specifying the exact class of object that will be created. This pattern is used in Data layer. Refer ContactsManagerFactory class in Entities folder.
Repository: Methods for retrieving domain objects should delegate to a specialized Repository object such that alternative storage implementations may be easily interchanged. Used in Data layer Repository and Implementation folders
Strategy: Allow algorithm selection at runtime. Encapsulate related algorithms and let the algorithm vary and evolve from the class using it. Separate the implementation from the delivery of its results.
In this architecture data layer is class library project and service layer i.e., ASP.NET Web API application that utilises data layer to interact with the underlying database.
N-Tier: Data layer implementation
Data layer is an implementation of Factory, Repository, DTO and Strategy patterns that consists of various layers as shown below.

In the Visual Studio Entities folder consists of ORM generated from database, POCOs generated using T4 template and Factory class that is responsible for creating a context to access entities with data.
Creating ORM
Creating the class library, adding folder is self explanatory. In order to add EF ORM model choose Entities folder context menu, then Add –> New Item from Add context menu, select ADO.NET Entity Data Model. This process is wizard driven where it is required to choose Generate from database, then database connection, connection string name, then database objects i.e., (Tables, Views, Stored procedures) and model namespace. Selecting Finish button creates ORM model. Refer video How Do I Get Started with the EDM Wizard?
Creating POCOs


From ORM model i.e., ContactsManagerModel.edmx choose Add Code Generation Item… context menu, add then choose ADO.NET Entity POCO Entity Generator Visual C# Items, name the POCO model and select Add button. This process generates storngly-typed ObjectContext class and entity classes with persistence ignorance.
Creating Factory
Factory class is responsible for establishing database connection and creates context for managing entities. ContactsManagerFactory factory class source is provided below.
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: using Contacts.Data.SqlServer.Entities;
7: using System.Configuration;
8:
9: namespace Contacts.Data.SqlServer
10: {
11: public class ContactsManagerFactory
12: {
13: private static string connectionString = string.Empty;
14:
15: #region Constructor
16: /// <summary>
17: ///
18: /// </summary>
19: static ContactsManagerFactory()
20: {
21: string connectionStringName = ConfigurationManager.AppSettings.Get("ContactManagerConnectionString");
22: connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
23: }
24: #endregion
25:
26: #region
27: /// <summary>
28: ///
29: /// </summary>
30: /// <returns></returns>
31: public static ContactManagerEntities CreateContext()
32: {
33: return new ContactManagerEntities(connectionString);
34: }
35:
36: #endregion
37: }
38: }
Above concludes the Entities folder creation.
Repository folder consists of interface with operation signatures to perform CRUD and/or custom operations.
Creating Repository
Add an interface and operation signatures to Repository folder as shown below.
1: namespace Contacts.Data.SqlServer.Repository
2: {
3: using System;
4: using System.Collections.Generic;
5: using System.Linq;
6: using System.Text;
7:
8: using Contacts.Data.SqlServer.Entities;
9:
10:
11: /// <summary>
12: /// Contains method signatures for ContactsManager repository
13: /// </summary>
14: public interface IContactsManagerRepository
15: {
16: /// <summary>
17: /// Gets all the contact reocrds from database
18: /// </summary>
19: /// <returns>list of case study objects</returns>
20: List<Contact> GetAllContacts();
21:
22: /// <summary>
23: /// Gets the contact record that matches with the id
24: /// </summary>
25: /// <param name="id">ID</param>
26: /// <returns>single contact record</returns>
27: Contact GetContactById(int id);
28:
29: /// <summary>
30: /// Inserts a new Contact record
31: /// </summary>
32: /// <param name="entity">Contact entity type that has to be inserted into the database</param>
33: void Insert(Contact entity);
34:
35: /// <summary>
36: /// Updates a single Contact record
37: /// </summary>
38: /// <param name="entity">Contact entity type that has to be updated in the database</param>
39: void Update(Contact entity);
40:
41: /// <summary>
42: /// Delete a single record from the table
43: /// </summary>
44: /// <param name="entity">ID of the row that has to be deleted from department table</param>
45: void Delete(int id);
46: }
47: }
Implementation folder consists of classes that inherit interfaces (contracts) from repository layer and implements the operations as shown below.
1: namespace Contacts.Data.SqlServer.Implementation
2: {
3: using System;
4: using System.Collections.Generic;
5: using System.Linq;
6: using System.Text;
7:
8: using Contacts.Data.SqlServer.Repository;
9: using Contacts.Data.SqlServer.Entities;
10:
11: /// <summary>
12: /// contains implementation of IContactManagerRepository
13: /// </summary>
14: public class ContactsManagerRepository : IContactsManagerRepository
15: {
16: /// <summary>
17: /// Returns a list of all Contacts in database
18: /// </summary>
19: /// <returns>List of contact record</returns>
20: public List<Contact> GetAllContacts()
21: {
22: using(var context = ContactsManagerFactory.CreateContext())
23: {
24: var contactslist = (from contact in context.Contacts orderby contact.ID descending select contact).ToList();
25: return contactslist;
26: }
27:
28: }
29: /// <summary>
30: /// Returns a contact record that matches with the id passed in
31: /// </summary>
32: /// <param name="id">single contact record</param>
33: /// <returns></returns>
34: public Contact GetContactById(int id)
35: {
36: using(var context = ContactsManagerFactory.CreateContext())
37: {
38: var contactrecord = (from contact in context.Contacts where contact.ID == id select contact).SingleOrDefault();
39: return contactrecord;
40: }
41: }
42:
43: /// <summary>
44: /// Inserts contact record into database
45: /// </summary>
46: /// <param name="entity">contact</param>
47: public void Insert(Contact entity)
48: {
49: throw new NotImplementedException();
50: }
51:
52: /// <summary>
53: /// Updates the contact record in database
54: /// </summary>
55: /// <param name="entity">contact record passed</param>
56: public void Update(Contact entity)
57: {
58: throw new NotImplementedException();
59: }
60:
61: /// <summary>
62: /// Deletes the contact record from database that matches the id passed
63: /// </summary>
64: /// <param name="id">ID</param>
65: public void Delete(int id)
66: {
67: throw new NotImplementedException();
68: }
69: }
70: }
Above concludes the Data layer implementation.
In Visual Studio the Data layer implementation is shown below.

N-Tier: Service Layer implementation
Service layer is an implementation of Facade, Dependency Injection, DTO and Request-Response patterns using ASP.NET Web API project. The project is shown as below in Visual Studio solution explorer.

Most of the above project folders and files are created by Visual Studio as part of ASP.NET Web API project creation.
Model folder consists of DTO with properties from Contact entity. These properties can be copied from Contact POCO in Data layer. ContactBO is shown below. Here ContactBO is business object where validation rules can be enforced using Data annotations.
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5:
6: using System.ComponentModel.DataAnnotations;
7:
8: namespace ContactsManager.Web.Api.Models
9: {
10: public class ContactBO
11: {
12: [Required]
13: public int Id { get; set; }
14: public string Name { get; set; }
15: public string Address { get; set; }
16: public string City { get; set; }
17: public string State { get; set; }
18: public string Zip { get; set; }
19: public string Email { get; set; }
20: public string Twitter { get; set; }
21: }
22: }
Messages folder consists of simple class with List of ContactBO and ContactBO that are returned to the client in service response. ContactResponse source is provided below.
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5:
6: using ContactsManager.Web.Api.Models;
7:
8: namespace ContactsManager.Web.Api.Messages
9: {
10: public class ContactResponse
11: {
12: public List<ContactBO> contacts;
13:
14: public ContactBO contact;
15:
16: }
17: }
Controller folder consists of API controller classes i.e., ContactsManagerController here. IContactManagerRepository from Data layer is injected through constructor, IOC container Microsoft Unity is utilised to map the interface to concrete implementation in Global.asax. It uses ScopeContainer.cs as well.
ScopeContainer and Global.asax source is provided below.
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5:
6: namespace ContactManager
7: {
8: using System;
9: using System.Collections.Generic;
10: using System.Web.Http;
11: using System.Web.Http.Dependencies;
12: using Microsoft.Practices.Unity;
13:
14: class ScopeContainer : IDependencyScope
15: {
16: protected IUnityContainer container;
17:
18: public ScopeContainer(IUnityContainer container)
19: {
20: if (container == null)
21: {
22: throw new ArgumentNullException("container");
23: }
24: this.container = container;
25: }
26:
27: public object GetService(Type serviceType)
28: {
29: if (container.IsRegistered(serviceType))
30: {
31: return container.Resolve(serviceType);
32: }
33: else
34: {
35: return null;
36: }
37: }
38:
39: public IEnumerable<object> GetServices(Type serviceType)
40: {
41: if (container.IsRegistered(serviceType))
42: {
43: return container.ResolveAll(serviceType);
44: }
45: else
46: {
47: return new List<object>();
48: }
49: }
50:
51: public void Dispose()
52: {
53: container.Dispose();
54: }
55: }
56:
57: class IoCContainer : ScopeContainer, IDependencyResolver
58: {
59: public IoCContainer(IUnityContainer container)
60: : base(container)
61: {
62: }
63:
64: public IDependencyScope BeginScope()
65: {
66: var child = container.CreateChildContainer();
67: return new ScopeContainer(child);
68: }
69: }
70: }
Global.asax
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Web.Http;
6: using System.Web.Mvc;
7: using System.Web.Optimization;
8: using System.Web.Routing;
9:
10: using ContactsManager.Web.Api.Controllers;
11: using Contacts.Data.SqlServer.Repository;
12: using Contacts.Data.SqlServer.Implementation;
13: using Microsoft.Practices.Unity;
14: using ContactManager;
15:
16: using Contacts.Data.SqlServer.Entities;
17:
18: namespace ContactsManager.Web.Api
19: {
20: // Note: For instructions on enabling IIS6 or IIS7 classic mode,
21: // visit http://go.microsoft.com/?LinkId=9394801
22:
23: public class WebApiApplication : System.Web.HttpApplication
24: {
25: protected void Application_Start()
26: {
27: AreaRegistration.RegisterAllAreas();
28:
29: GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
30:
31: WebApiConfig.Register(GlobalConfiguration.Configuration);
32: FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
33: RouteConfig.RegisterRoutes(RouteTable.Routes);
34: BundleConfig.RegisterBundles(BundleTable.Bundles);
35:
36: this.ConfigureApi(GlobalConfiguration.Configuration);
37: }
38: void ConfigureApi(HttpConfiguration config)
39: {
40: var unity = new UnityContainer();
41: unity.RegisterType<ContactsManagerController>();
42: unity.RegisterType<IContactsManagerRepository, ContactsManagerRepository>(new ContainerControlledLifetimeManager());
43:
44: config.DependencyResolver = new IoCContainer(unity);
45:
46: }
47: }
48: }
Refer Microsoft’s Mike article how to inject dependencies into ASP.NET Web API controller, using the Web API dependency resolver.
Automapper is used to map POCO <—> ContactBO as shown below.
1: Mapper.CreateMap<Contact, ContactBO>();
2: Mapper.CreateMap<ContactBO, Contact>();
Complete source for ContactManagerController is below.
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Net;
5: using System.Net.Http;
6: using System.Web.Http;
7: using Contacts.Data.SqlServer.Repository;
8: using AutoMapper;
9:
10: using ContactsManager.Web.Api.Models;
11: using ContactsManager.Web.Api.Messages;
12: using Contacts.Data.SqlServer.Entities;
13:
14: namespace ContactsManager.Web.Api.Controllers
15: {
16: public class ContactsController : ApiController
17: {
18: /// <summary>
19: /// Initialise a variable of IContactsManagerRepository from data layer
20: /// </summary>
21: private readonly IContactsManagerRepository Contactrepository;
22:
23: /// <summary>
24: /// Inject repository and Map POCO <----> BO
25: /// </summary>
26: /// <param name="_repository">IContactsManagerRepository</param>
27: public ContactsController(IContactsManagerRepository _repository)
28: {
29: if(_repository == null)
30: {
31: throw new ArgumentNullException("ContactManager Repository exception");
32: }
33:
34: this.Contactrepository = _repository;
35:
36: Mapper.CreateMap<Contact, ContactBO>();
37: Mapper.CreateMap<ContactBO, Contact>();
38:
39: Mapper.AssertConfigurationIsValid();
40: }
41:
42: /// <summary>
43: /// Return list of contact records
44: /// </summary>
45: /// <returns>list of contact DTO</returns>
46: public ContactResponse Get()
47: {
48: ContactResponse response = new ContactResponse();
49: response.contacts = Mapper.Map<List<Contact>, List<ContactBO>>(this.Contactrepository.GetAllContacts());
50:
51: return response;
52: }
53:
54: /// <summary>
55: /// Returns a single contact record that matches with the id
56: /// </summary>
57: /// <param name="id">contact id</param>
58: /// <returns>single contact DTO</returns>
59: public ContactResponse Get(int id)
60: {
61: ContactResponse response = new ContactResponse();
62: response.contact = Mapper.Map<Contact, ContactBO>(this.Contactrepository.GetContactById(id));
63:
64: return response;
65: }
66: }
67:
68:
69: }
Web Api Request processing
When the request is received from the client, Service creates an instance of ContactResponse, retrieves data through data layer interface(IContactManagerRepository) and returns either a List or single ContactBO (DTO) by mapping Contact POCO to ContactBO using Automapper.
Service URI Example:
http://localhost:51203/api/contacts/
When the above request is received from the client, list of ContactBO is returned to the client in Json format. WebApi routing maps the request to Get() operation (in ContactController) where an instance of ContactResponse is created, data is retrieved from data layer through IContactManagerRepository (ContactRepository) and object mapping is performed using Automapper.
http://localhost:51203/api/contacts/2
Above URI returns a single record where Contact ID is 2.
NOTE: Only Get methods source is provided in above controller as the process to implement POST, DELETE and PUT is similar. The only difference is mapping i.e., ContactBO to Contact to Create or Update the entity.
Refer Creating a Web API that Supports CRUD Operations
This concludes the N-Tier architecture for ASP.NET Web API application that utilises Data layer to interact with underlying database.
Testing: In order to test the application any HTTP tool can be used such as any browser, web debugging tool Fiddler by simply navigating to the above URIs. Note that IE asks to save or open the service response (Contact DTO). JSONView for Chrome or JSONView for Firefox can be installed to view the data in JSon format.
To implement API help page refer Creating Help Pages for ASP.NET Web API
API Help page provides all the operations with example data and the URI with xml comments from the operations.
Below video tutorial shows how to customize the Web API Help Pages as well as a number of tools that will help you develop and document your APIs.
References
Logging exceptions to persistent store or email alert with exception stack helps developer to troubleshoot issues without having to debug the application. This practice helps in exception trend monitoring and improve the application. In the applications where security is high priority it is feasible to trace and log security exceptions. Mostly all of the tracing and logging technologies provide configuration driven trace levels i.e., Information, Warning, Error etc.
This article discusses various tracing and logging technologies and walks through 'configuring ASP.NET Web API application to log unhandled exceptions to database and generate email alert with exception stack using
ELMAH'.
AppFabric provides two services for monitoring and caching services.
- Monitoring services
- Caching services
AppFabric is IIS extension that provides a dashboard to monitor and troubleshoot issues with in WCF and WF applications and services. Installation & configuring AppFabric creates database to store service trace as well. AppFabric installation and configuration process is discussed in another blog post.
AppFabric does not support ASP.NET Web API services to monitor and troubleshoot issues.
In order to log unhandled exceptions from ASP.NET Web API there are number of alternatives as outlined below.
Configuring Logging application block to email exception block is discussed in my blog article at http://weblogs.asp.net/sukumarraju/archive/2010/08/21/email-exception-stack-using-logging-application-block-email-trace-listener.aspx
Log4Net provides another mechanism to log trace to text file or to database or custom location.
Being built on top of ASP.NET run time WebForms, Web API, MVC, Single page and SignalR can utilise above alternative technologies in addition to ASP.NET Health monitoring to log exceptions or trace to desired location for applications troubleshooting.

Logging unhandled exceptions using ELMAH
ELMAH is an open source tool for ASP.NET services, exceptions that are thrown in ASP.NET applications trigger event handlers in ELMAH tool. It provides pluggable out of box implementation.
Note that all the frameworks that run on ASP.NET can take advantage of ELMAH to log unhandled exceptions.
Step 1: Get the assembly from Nuget
PM> Install-Package Elmah.Contrib.WebApi
Installing the assembly updates application Web.config SectionGroup, connection Strings and Http modules.
Now in memory exception logging for the application, which can be accessed by navigating to /elmah.axd">/elmah.axd">/elmah.axd">http://<port>/elmah.axd as shown below.

Note that when the application is stopped or app pool recycles the exception will be lost. Because these are in memory exceptions. Selecting the Details hyperlink in the above dashboard opens detailed exception stack with complete server variables and values.
Step 2: Log exceptions to SQL Server database
To store exceptions to SQL Server database get the assembly.
1: PM> Install-Package elmah.sqlserver
Adding the package adds App_Readme folder with SQL script to generate database tables and stored procedures for logging exceptions. Also adds <errorLog> element to Web.config and connection String as below.
1: <connectionStrings>
2: <add name="elmah-sqlserver"
3: connectionString="Data Source=****;
4: User ID=****;Password=****;
5: Initial Catalog=****;"
6: providerName="System.Data.SqlClient" />
7:
8: <!-- of course it is required to update above SQL connection-->
9:
10: <security allowRemoteAccess="false" />
11: <errorLog type="Elmah.SqlErrorLog, Elmah"
12: connectionStringName="elmah-sqlserver"
13: applicationName="Contact Manager" />
14:
15: <!-- applicationName added to distinguish the applications in the
16: centralised exceptions database -->
That is it! Unhandled from the application logged into database as shown below.

Step 3: Email exception stack
Provide SMTP (mail server), port, From and To email addresses in <errorMail> section as shown below.
1: <elmah>
2: <errorMail from="robot@elmah" to="dev@groupcom"
3: async="true" smtpServer="xxxx.xxx.xxxx.xx" smtpPort="25"/>
4: </elmah>
This generates email alerts with exception stack as shown below.

Note that running applications can take advantage of ELMAH error logging, all it requires is copying required assemblies and configuration into Web.config.
Conclusion:- Logging all web applications exceptions to database helps to troubleshoot, it is feasible to implement a dashboard with the exceptions from various applications that are utilising ELMAH. All it requires to configuring ELMAHR (Elmah + SignalR), real time web framework. This is for another blog article !!
Resources
Hello
I am not intending to provide an introduction for this topic, you can download the solution that I uploaded at MSDN code samples at http://code.msdn.microsoft.com/ASPNET-Web-API-NUnit-ac687169
Please note that in above solution the Web API service returns DTO in Json format to client.
To concise the solution consists of ASP.NET Web API project and a test project for testing service controllers and service Http response using HttpClient.
complete source is available at above URL from MSDN code sample. Here is the actual implementation of the same, which is replicated.
Controllers testing
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6:
7: using ContactManager.Controllers;
8: using ContactManager.Models.Repository;
9: using NUnit.Framework;
10: using System.Web.Http;
11: using ContactManager.Models.Entities;
12:
13: namespace ContactsNUnitTests
14: {
15: /// <summary>
16: /// Contains NUnit test cases for ContactsController
17: /// </summary>
18: [TestFixture]
19: public class ContactsControllerTests
20: {
21: ContactsController contactsController;
22: ContactRepository repository;
23:
24: int count;
25: int contactId;
26:
27: [SetUp]
28: public void Setup()
29: {
30: //create an instance of contactRepository
31: repository = new ContactRepository();
32:
33: //Create an instance of controller by passing repository
34: contactsController = new ContactsController(repository);
35:
36: //Number of records
37: count = contactsController.Get().contacts.Count;
38:
39: //Pass contact ID and store the retrieved contact ID
40: contactId = contactsController.Get(1).contact.Id;
41: }
42:
43: [Test]
44: public void GetAllContacts()
45: {
46: Assert.IsTrue(count > 0);
47: }
48:
49: [Test]
50: public void GetContact()
51: {
52: Assert.IsTrue(contactId.Equals(1));
53: }
54: }
55: }
Service response testing
Testing service Http response using HttpClient
1: namespace ContactsNUnitTests.ServiceHttpResponse
2: {
3: using System;
4: using System.Collections.Generic;
5: using System.Linq;
6: using System.Text;
7:
8: using NUnit.Framework;
9: using System.Net.Http;
10: using System.Configuration;
11: using System.Net;
12: using System.Net.Http.Headers;
13:
14: /// <summary>
15: /// Service Http Response tests
16: /// </summary>
17: public class HttpResponseTests
18: {
19: private HttpClient client;
20:
21: private HttpResponseMessage response;
22:
23: [SetUp]
24: public void SetUP()
25: {
26: client = new HttpClient();
27:
28: client.BaseAddress = new Uri(ConfigurationManager.AppSettings["serviceBaseUri"]);
29: response = client.GetAsync("contacts/get").Result;
30: }
31:
32: [Test]
33: public void GetResponseIsSuccess()
34: {
35: Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
36: }
37:
38:
39: [Test]
40: public void GetResponseIsJson()
41: {
42: client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
43:
44: Assert.AreEqual("application/json", response.Content.Headers.ContentType.MediaType);
45: }
46:
47: [Test]
48: public void GetAuthenticationStatus()
49: {
50: Assert.AreNotEqual(HttpStatusCode.Unauthorized,
51: response.StatusCode);
52:
53: }
54: }
55: }
References:
http://www.peterprovost.org/blog/2012/06/16/unit-testing-asp-dot-net-web-api
http://www.asp.net/web-api/overview/testing-and-debugging
http://blogs.msdn.com/b/youssefm/archive/2013/01/28/writing-tests-for-an-asp-net-webapi-service.aspx
Below video tutorial walks through the MVC pattern, similaries between MVC and Web Forms and then implements a MVC application from scratch.
Reference: MSDN Channel 9: TechEd 2010
Free eBook: Intro to asp.net mvc 4: https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CD0QFjAA&url=http%3A%2F%2Fdownload.microsoft.com%2Fdownload%2F0%2FF%2FB%2F0FBFAA46-2BFD-478F-8E56-7BF3C672DF9D%2FIntro%2520to%2520ASP.NET%2520MVC%25204%2520with%2520Visual%2520Studio%2520-%2520Beta.pdf&ei=nmUnUYWDF-Wd0QWW_IGQCg&usg=AFQjCNHUve5OvbugUO-yLtrNX7EDJzEnnA&bvm=bv.42768644,d.d2k
Best source: Readable online at http://ofps.oreilly.com/titles/9781449320317/ch_MvcForWebFormsDevs.html
Refer http://www.asp.net/mvc for tutorials, vieos, samples, books and more resources
After InfoPath form has been published and uploaded to the server from SharePoint admin site in order to add the content type with in SharePoint document library it is required to activate the form in SharePoint site as below.
1. Select Site Settings
2. Under Site Collection Administration tab select Site collection features hyper link
3. On the Site Collection Features page, choose the row for the form that needs to be activated or deactivated, then select Active or Deactivate button.

References:
http://office.microsoft.com/en-gb/sharepoint-server-help/activate-or-deactivate-a-form-template-for-a-site-collection-HA010167281.aspx
http://office.microsoft.com/en-gb/sharepoint-server-help/demo-deploy-an-administrator-approved-form-template-HA010205047.aspx?CTT=3
Hi All,
It is straight forward to use Regular expressions to extract text from a string. Below code snippet demos extracting year from a string and Text in between brackets from a string.
Extract Text between brackets
1: static void Main(string[] args)
2: {
3:
4: //Regular Expression pattern
5: string Pattern = @"\((.*?)\)";
6: //Regular Expression with pattern
7: Regex re = new Regex(Pattern);
8: string strSearch = "Microsoft (www.msdn.com)";
9:
10: //Loop through the string and output the matching text
11: foreach(Match m in re.Matches(strSearch))
12: {
13: Console.Write(m.Value.Replace("(",string.Empty).Replace(")",string.Empty).Trim());
14:
15: }
16: Console.ReadKey();
17: }
Extract Year from string
1: //Regular expression for year
2: string pattern = "([0-9]{4})";
3: Regex re = new Regex(pattern);
4: string txtYear = string.Empty;
5:
6: string strSearch = "SharePoint 2010";
7:
8: foreach (Match m in re.Matches(strSearch))
9: {
10: txtYear = m.Value;
11: Console.Write(txtYear);
12: }
13:
14: Console.ReadKey();
15: }
References
http://support.microsoft.com/kb/308252
http://msdn.microsoft.com/en-us/library/ms228595(v=VS.80).aspx
http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet
This article walks through the 'Enterprise Library: Logging application' configuration process to log data to database using WCF service.
Note that regardless of Web service type i.e., SOAP or RESTful the configuration process is same.
Technical environment:-
Visual Studio 2010 with .NET 4.0
Enterprise library 5.0
WCF
ASP.NET Web application on Client end.
Open Service Web.config using Enterprise library configuration tool
Open the Service Web configuration file using Enterprise library configuration tool, which can be found at C:\Program Files\Microsoft Enterprise Library 5.0\Bin [assuming that you chosen C: drive for Enterprise library installation process]
[Image: Enterprise library configuration tool location]
Note: Make sure that Service Web.config contains database connection string in connectionStrings section.
Step 1: Open Enterprise library configuration tool
Below screenshot shows the configuration tool after opening the service web.config file.
[Service web.configuration with in Enterprise Library configuration tool]
Step 2: _Choosing the Default database instance with in Database settings section
Choose the Default Database Instance from drop down list, in this example Logging is chosen as default one as shown below.
[Default Database Instance]
Step 3: Add Logging block block
Choose 'Logging Settings' to add logging application block from Blocks menu as shown below screenshot.
[Add_Logging_Block]
Step 4: Expand Logging Settings section
In order to configure Logging block, expand logging settings by clicking the icon on the left hand side as marked in red below image.
[Expand_Logging_Section_Image]
Step 5: Add Database Trace listener
From Logging Target Listeners selecting the + opens 'Add Logging Target Listeners'.
Choose 'Add Database Trace Listener' from context menu that is opened from
'Add Logging Target Listeners' as shown below screenshot.
[Add-database-trace-listener]
Step 6: Choose Database Trace Listener as default listener
Make sure that 'Database Trace listener' is chosen as default listener in Logging settings --> General section as shown in below image.
[Choose-db-trace-listener]
Note:- By selecting right mouse button on particular configuration section it is feasible to Validate particular section as shown in below image.
[Configuration-Sections-Validate]
Step 7: Enterprise library application block DLL references
Make sure that below mentioned references from Enterprise library API are added to
the service WCF service as shown in below image.
These DLLs are available from Enterprise Library installation at
C:\Program Files\Microsoft Enterprise Library 5.0\Bin

[Required DLLs]
Step 8: Add required 'using' statements
1: using Microsoft.Practices.EnterpriseLibrary.Logging;
2: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
3: using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
4: using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
5: using Microsoft.Practices.EnterpriseLibrary.Data;
6: using System.Data;
Step 9: Create a method that utilises Logging application block to log data to database.
Create a private LogWriter variable as below;
1: private LogWriter writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
Logger method can be similar to the one below or it can have more parameters.
1: ///Create a logEntry object and write to database
2: public void DbLogger(string _Title, string _Message, string _MachineName)
3: {
4: LogEntry logEntry = new LogEntry();
5:
6: logEntry.Title = _Title;
7: logEntry.Message = _Message;
8: logEntry.MachineName = _MachineName;
9: logEntry.TimeStamp = DateTime.Now;
10:
11: writer.Write(logEntry);
12: }
Step 10: Test the functionality with a client instance
1: protected void Page_Load(object sender, EventArgs e)
2: {
3: //Create an instance of client proxy
4: Service1Client client = new Service1Client();
5: client.Open();
6:
7: try
8: {
9: client.DbLogger("Logger service consumer", "Testing the Logger db functionality", "My machine");
10: }
11:
12: catch (Exception ex)
13: {
14: Response.Write(ex.InnerException.Message.ToString());
15: }
16: }
Running the client app with above should log data to Logging database, which can be installed from scripts provided with Hands-on-Labs referenced below.
Creating Logging database
Note that Logging database can be created by running the SQL script provided with Enterprise Library 5.0 Hands on labs, which can be downloaded at
http://www.microsoft.com/download/en/details.aspx?id=6932
Reference
http://msdn.microsoft.com/en-us/library/ff632023.aspx
http://www.microsoft.com/download/en/details.aspx?id=6932
http://msdn.microsoft.com/en-us/library/ff650510.aspx
More Posts
Next page »