Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveOperationContext.cs @ 10934

Last change on this file since 10934 was 9444, checked in by pfleck, 12 years ago

#2030
Switched Text encoding to Mtom encoding for better performance for binary data.
Merged trunk into branch.

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    public void Attach(OperationContext owner) {
47    }
48
49    public void Detach(OperationContext owner) {
50      if (dataContext != null) {
51        dataContext.Dispose();
52      }
53    }
54  }
55
56  public class HiveOperationContextMessageInspector : IDispatchMessageInspector {
57    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) {
58      OperationContext.Current.Extensions.Add(new HiveOperationContext());
59      return request.Headers.MessageId;
60    }
61
62    public void BeforeSendReply(ref Message reply, object correlationState) {
63      OperationContext.Current.Extensions.Remove(HiveOperationContext.Current);
64    }
65  }
66
67  public class HiveOperationContextBehaviorAttribute : Attribute, IServiceBehavior {
68    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
69      Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
70    }
71
72    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
73      foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers) {
74        foreach (EndpointDispatcher ed in cd.Endpoints) {
75          ed.DispatchRuntime.MessageInspectors.Add(new HiveOperationContextMessageInspector());
76        }
77      }
78    }
79
80    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.