Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.Hive/3.3/HiveOperationContext.cs @ 17097

Last change on this file since 17097 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 != null
36          ? OperationContext.Current.Extensions.Find<HiveOperationContext>()
37          : null;
38      }
39    }
40
41    private HiveDataContext dataContext;
42    public HiveDataContext DataContext {
43      get {
44        return dataContext ?? (dataContext = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString));
45      }
46    }
47
48    public void Attach(OperationContext owner) {
49    }
50
51    public void Detach(OperationContext owner) {
52      if (dataContext != null) {
53        dataContext.Dispose();
54      }
55    }
56  }
57
58  public class HiveOperationContextMessageInspector : IDispatchMessageInspector {
59    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) {
60      OperationContext.Current.Extensions.Add(new HiveOperationContext());
61      return request.Headers.MessageId;
62    }
63
64    public void BeforeSendReply(ref Message reply, object correlationState) {
65      OperationContext.Current.Extensions.Remove(HiveOperationContext.Current);
66    }
67  }
68
69  public class HiveOperationContextBehaviorAttribute : Attribute, IServiceBehavior {
70    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
71      Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
72    }
73
74    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
75      foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers) {
76        foreach (EndpointDispatcher ed in cd.Endpoints) {
77          ed.DispatchRuntime.MessageInspectors.Add(new HiveOperationContextMessageInspector());
78        }
79      }
80    }
81
82    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.