Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveOperationContext.cs @ 9434

Last change on this file since 9434 was 9434, checked in by pfleck, 11 years ago

#2030
Added SelfHost-Project
Renamed HiveDtoDao back to HiveDao and renamed the optimized HiveDao into OptimizedDao instead.
Optimized AddTask by using compiled queries.

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.ObjectModel;
24using System.ServiceModel;
25using System.ServiceModel.Channels;
26using System.ServiceModel.Description;
27using System.ServiceModel.Dispatcher;
28using HeuristicLab.Services.Hive.DataAccess;
29
30namespace HeuristicLab.Services.Hive {
31  public class HiveOperationContext : IExtension<OperationContext> {
32
33    public static HiveOperationContext Current {
34      get {
35        return OperationContext.Current.Extensions.Find<HiveOperationContext>();
36      }
37    }
38
39    private HiveDataContext dataContext;
40    public HiveDataContext DataContext {
41      get {
42        return dataContext ?? (dataContext = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString));
43      }
44    }
45
46
47    public void Attach(OperationContext owner) {
48    }
49
50    public void Detach(OperationContext owner) {
51      if (dataContext != null) {
52        dataContext.Dispose();
53      }
54    }
55  }
56
57  public class HiveOperationContextMessageInspector : IDispatchMessageInspector {
58    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) {
59      OperationContext.Current.Extensions.Add(new HiveOperationContext());
60      return request.Headers.MessageId;
61    }
62
63    public void BeforeSendReply(ref Message reply, object correlationState) {
64      OperationContext.Current.Extensions.Remove(HiveOperationContext.Current);
65    }
66  }
67
68  public class HiveOperationContextBehaviorAttribute : Attribute, IServiceBehavior {
69    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
70      Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
71    }
72
73    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
74      foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers) {
75        foreach (EndpointDispatcher ed in cd.Endpoints) {
76          ed.DispatchRuntime.MessageInspectors.Add(new HiveOperationContextMessageInspector());
77        }
78      }
79    }
80
81    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.