Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2030
Separated old DTO-Dao from new Dao. DTO-Dao should be replaced completely.
Heartbeat and UpdateTaskState uses new Dao.
DataContext is now closed on ServiceOperation end.

File size: 3.4 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.Diagnostics;
25using System.IO;
26using System.ServiceModel;
27using System.ServiceModel.Channels;
28using System.ServiceModel.Description;
29using System.ServiceModel.Dispatcher;
30using System.Text;
31using HeuristicLab.Services.Hive.DataAccess;
32
33namespace HeuristicLab.Services.Hive {
34  public class HiveOperationContext : IExtension<OperationContext> {
35    public static HiveOperationContext Current {
36      get {
37        return OperationContext.Current.Extensions.Find<HiveOperationContext>();
38      }
39    }
40
41    public HiveDataContext DataContext { get; private set; }
42
43    public void Attach(OperationContext owner) {
44      DataContext = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
45      //DataContext.Log = new DebugWriter();
46    }
47
48    public void Detach(OperationContext owner) {
49      DataContext.Dispose();
50    }
51  }
52
53  public class HiveOperationContextMessageInspector : IDispatchMessageInspector {
54    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) {
55      OperationContext.Current.Extensions.Add(new HiveOperationContext());
56      return request.Headers.MessageId;
57    }
58
59    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState) {
60      OperationContext.Current.Extensions.Remove(HiveOperationContext.Current);
61    }
62  }
63
64  public class HiveOperationContextBehaviorAttribute : Attribute, IServiceBehavior {
65    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
66      Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
67    }
68
69    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
70      foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers) {
71        foreach (EndpointDispatcher ed in cd.Endpoints) {
72          ed.DispatchRuntime.MessageInspectors.Add(new HiveOperationContextMessageInspector());
73        }
74      }
75    }
76
77    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
78    }
79  }
80
81  internal class DebugWriter : TextWriter {
82    public override Encoding Encoding {
83      get { return Encoding.UTF8; }
84    }
85
86    public override void Write(char value) {
87      Debug.Write(value);
88    }
89
90    public override void Write(string value) {
91      Debug.Write(value);
92    }
93
94    public override void WriteLine(string value) {
95      Debug.WriteLine(value);
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.