|In Step 1 I have structured my Visual Studio Solution. In Step 2 I have designed and implemented my Entities and Business Services. Now I need to make sure I have basic set of utilities I might be needing. These are the cross cutting utilities I will be using in any of the layers. At minimum I need to have utilities related to security, logging & monitoring. I might be further adding more utilities as I go.||
Quick Resource Box
In this post I will be implementing Logging & Instrumentation and Input & Data Validation security related utilities which I will be using in presentation, business logic, and data tiers.
Logging & Instrumentation
General guidelines from Chapter 15: Web Application Archetype:
The summary of my logging and instrumentation strategy:
Liability: Web Events designed to work in ASP.NET Web applications or WCF Services hosted in IIS. If one day I decide to move my business logic into separate physical tier not hosted in IIS and not exposed as WCF service then Web Events might be unsuitable. On other hand, I hardly can see that day coming any soon, if any… In any case the logging mechanism is abstracted so the implementation is not visible to its consumers.
Right click on Crosscutting folder in Visual Studio and add two new Class Library Projects – WebEvents and Instrumentation:
Right click on Instrumentation project and add new Class. Name it Tracing. Add implementation similar to the one outlined in Tracing & Instrumenting ASP.NET Application For Performance. At this moment you should be able to report entry and exit to the functions or any arbitrary messages.
public class Tracing
The output is published to default Trace Listener which is added by default. Use either DebugView or Procmon to collect and display the trace messages as described in Tracing & Instrumenting ASP.NET Application For Performance, Sysinternals ProcMon New & Improved – Captures Both System & Application Events.
Next I will implement logging using Web Events and ASP.NET Health Monitoring mechanism.
Right click on WebEvents project and add new class. Name it ExceptionEvent. Add reference to System.Web, add System.Web and
class ExceptionEvent : WebBaseEvent
Right click on WebEvents project and add new class. Name it EeventServices. This is the abstraction of the event publishing services. Anytime I will be unsatisfied with Web Events [which I doubt] I’d go and change the implementation without breaking other layers:
public class EventsServices
Configuring web events in Web.Config will be covered further when designing presentation layer, stay tuned.
This section covers basic but critical functions related to security. Specifically HTML encoding, URL encoding, and integrity check. These utilities invaluable when dealing with injection attacks. More info here: How To: Prevent Cross-Site Scripting in ASP.NET.
Right click on Crosscutting and add new Visual Studio project. Name it Security.
Right click on the Security project and add new class. Name it EncodingServices. Add the following implementation to the class:
public class EncodingServices
Such abstraction might look redundant but there is a reason. In previous versions of .Net framework HtmlEncode function was encoding only <>&” characters which is insufficient. But recent implementation seem to work just fine. I will be testing it later via Unit Test.
Unit Testing My Solution
Right click on any method in any class in any project under Crosscutting folder. Choose “Create Unit Tests…”. Select all methods of our interest as depicted below. Choose to create new Test project and name it CrosscuttingTests. Click Create, click OK.
Drag CrosscuttingTests project under Crosscutting folder.
Unit Testing Tracing Services
Change TracingTest.cs implementation so it looks as follows:
Open DebugView. Configure to filter messages that include TRC string to avoid massive unrelated noise:
Run trace related unit tests:
As a result you should see your messages displayed in DebugView as depicted below [notice lovely Time column]:
Unit Testing Events Services
Change EventsServicesTest.cs implementation so it looks as follows:
Run event publishing related tests [make sure to run the test under Administrator’s privileges at least once in order to create your apps’ source]:
The outcome of the run should look as following in the Application Event Log:
Unit Testing Encoding Services
Change EncodingServicesTests.cs so it looks as follows:
Run your encoding related tests:
You should pass the test:
At this point I have created and tested basic crosscutting services I will be using in the rest of the layers. The services include Tracing, Event Publishing, and Input Sanitation [HTML and URL encoding].