1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using AopAlliance.Intercept;
|
---|
6 | using System.Threading;
|
---|
7 | using HeuristicLab.Hive.Server.LINQDataAccess;
|
---|
8 | using System.Transactions;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Hive.Server {
|
---|
11 | internal class ServiceCallInterception : IMethodInterceptor {
|
---|
12 |
|
---|
13 | private bool useTransactions = false;
|
---|
14 |
|
---|
15 | public object Invoke(IMethodInvocation invocation) {
|
---|
16 | Console.WriteLine(DateTime.Now + " - " + Thread.CurrentThread.ManagedThreadId + " - Entering Method " +
|
---|
17 | invocation.Method.Name);
|
---|
18 |
|
---|
19 | if(ContextFactory.Context != null) {
|
---|
20 | Console.WriteLine("Error - Not null context found - why wasn't this disposed?");
|
---|
21 | ContextFactory.Context = null;
|
---|
22 | }
|
---|
23 |
|
---|
24 | Object obj = null;
|
---|
25 |
|
---|
26 | if (invocation.Method.Name.Equals("SendStreamedJob") || invocation.Method.Name.Equals("StoreFinishedJobResultStreamed")) {
|
---|
27 | ContextFactory.Context.Connection.Open();
|
---|
28 | if(useTransactions)
|
---|
29 | ContextFactory.Context.Transaction = ContextFactory.Context.Connection.BeginTransaction();
|
---|
30 | try {
|
---|
31 | obj = invocation.Proceed();
|
---|
32 | Console.WriteLine("leaving context open for Streaming");
|
---|
33 | }
|
---|
34 | catch (Exception e) {
|
---|
35 | Console.WriteLine(e);
|
---|
36 | ContextFactory.Context.Dispose();
|
---|
37 | ContextFactory.Context = null;
|
---|
38 | }
|
---|
39 | } else {
|
---|
40 | if(useTransactions) {
|
---|
41 | using (TransactionScope scope = new TransactionScope()) {
|
---|
42 | try {
|
---|
43 | obj = invocation.Proceed();
|
---|
44 | scope.Complete();
|
---|
45 | }
|
---|
46 | catch (Exception e) {
|
---|
47 | Console.WriteLine("Exception Occured");
|
---|
48 | Console.WriteLine(e);
|
---|
49 | }
|
---|
50 | finally {
|
---|
51 | ContextFactory.Context.Dispose();
|
---|
52 | Console.WriteLine("setting old context null");
|
---|
53 | ContextFactory.Context = null;
|
---|
54 | Console.WriteLine("Disposing old Context");
|
---|
55 | }
|
---|
56 | }
|
---|
57 | } else {
|
---|
58 | try {
|
---|
59 | obj = invocation.Proceed();
|
---|
60 | }
|
---|
61 | catch (Exception e) {
|
---|
62 | Console.WriteLine("Exception Occured");
|
---|
63 | Console.WriteLine(e);
|
---|
64 | }
|
---|
65 | finally {
|
---|
66 | ContextFactory.Context.Dispose();
|
---|
67 | Console.WriteLine("setting old context null");
|
---|
68 | ContextFactory.Context = null;
|
---|
69 | Console.WriteLine("Disposing old Context");
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | Console.WriteLine(DateTime.Now + " - " + Thread.CurrentThread.ManagedThreadId + " - Leaving Method " +
|
---|
74 | invocation.Method.Name);
|
---|
75 |
|
---|
76 | return obj;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | |
---|