Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16418 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 3.1 KB
RevLine 
[9381]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9381]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
[9391]22using System;
23using System.Collections.ObjectModel;
[9381]24using System.ServiceModel;
[9391]25using System.ServiceModel.Channels;
26using System.ServiceModel.Description;
27using System.ServiceModel.Dispatcher;
[9381]28using HeuristicLab.Services.Hive.DataAccess;
29
30namespace HeuristicLab.Services.Hive {
31  public class HiveOperationContext : IExtension<OperationContext> {
[9434]32
[9381]33    public static HiveOperationContext Current {
34      get {
[9634]35        return OperationContext.Current != null
36          ? OperationContext.Current.Extensions.Find<HiveOperationContext>()
37          : null;
[9381]38      }
39    }
40
[9393]41    private HiveDataContext dataContext;
42    public HiveDataContext DataContext {
43      get {
[9434]44        return dataContext ?? (dataContext = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString));
[9393]45      }
46    }
[9381]47
48    public void Attach(OperationContext owner) {
49    }
50
51    public void Detach(OperationContext owner) {
[9393]52      if (dataContext != null) {
53        dataContext.Dispose();
54      }
[9381]55    }
56  }
57
[9391]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
[9434]64    public void BeforeSendReply(ref Message reply, object correlationState) {
[9391]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  }
[9381]85}
Note: See TracBrowser for help on using the repository browser.