75 lines
1.9 KiB
C#
75 lines
1.9 KiB
C#
using System;
|
|
using System.Web;
|
|
using GeneralApi.Core.Common;
|
|
using GeneralApi.Core.Infrastructure;
|
|
using GeneralApi.Core.Services;
|
|
using Microsoft.Practices.ServiceLocation;
|
|
|
|
namespace Taloyhtio.GeneralApi.Core.Infrastructure.Web
|
|
{
|
|
public class UnitOfWorkModule : IHttpModule
|
|
{
|
|
private ILogger logger = new Logger();
|
|
|
|
public void Init(HttpApplication context)
|
|
{
|
|
context.BeginRequest += context_BeginRequest;
|
|
context.EndRequest += context_EndRequest;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
|
|
}
|
|
|
|
private void context_BeginRequest(object sender, EventArgs e)
|
|
{
|
|
if (!shouldCreateUnitOfWork())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var instance = ServiceLocator.Current.GetInstance<IUnitOfWork>();
|
|
instance.Begin();
|
|
}
|
|
|
|
private void context_EndRequest(object sender, EventArgs e)
|
|
{
|
|
if (!shouldCreateUnitOfWork())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var instance = ServiceLocator.Current.GetInstance<IUnitOfWork>();
|
|
try
|
|
{
|
|
instance.Commit();
|
|
}
|
|
finally
|
|
{
|
|
instance.Dispose();
|
|
}
|
|
}
|
|
|
|
private bool shouldCreateUnitOfWork()
|
|
{
|
|
try
|
|
{
|
|
string url = HttpContext.Current.Request.RawUrl;
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
return false;
|
|
}
|
|
url = url.ToLower();
|
|
// current run only for aspx and asmx
|
|
return (url.Contains(".aspx") || url.Contains(".asmx") || url.Contains(".svc") || url.Contains(".ashx"));
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.logger.LogError(string.Format("{0}\n{1}", x.Message, x.StackTrace));
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|