Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9426 was 9393, checked in by pfleck, 12 years ago

#2030
Changed Linq.Binary to byte array to avoid hash computation.
DataContext in HiveOperationContext is now lazy initialized.
Added missing HiveDao from last commit failure.

File size: 3.6 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    private HiveDataContext dataContext;
42    public HiveDataContext DataContext {
43      get {
44        if (dataContext == null) {
45          dataContext = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
46          //dataContext.Log = new DebugWriter();
47        }
48        return dataContext;
49      }
50    }
51
52
53    public void Attach(OperationContext owner) {
54    }
55
56    public void Detach(OperationContext owner) {
57      if (dataContext != null) {
58        dataContext.Dispose();
59      }
60    }
61  }
62
63  public class HiveOperationContextMessageInspector : IDispatchMessageInspector {
64    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) {
65      OperationContext.Current.Extensions.Add(new HiveOperationContext());
66      return request.Headers.MessageId;
67    }
68
69    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState) {
70      OperationContext.Current.Extensions.Remove(HiveOperationContext.Current);
71    }
72  }
73
74  public class HiveOperationContextBehaviorAttribute : Attribute, IServiceBehavior {
75    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
76      Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
77    }
78
79    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
80      foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers) {
81        foreach (EndpointDispatcher ed in cd.Endpoints) {
82          ed.DispatchRuntime.MessageInspectors.Add(new HiveOperationContextMessageInspector());
83        }
84      }
85    }
86
87    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
88    }
89  }
90
91  internal class DebugWriter : TextWriter {
92    public override Encoding Encoding {
93      get { return Encoding.UTF8; }
94    }
95
96    public override void Write(char value) {
97      Debug.Write(value);
98    }
99
100    public override void Write(string value) {
101      Debug.Write(value);
102    }
103
104    public override void WriteLine(string value) {
105      Debug.WriteLine(value);
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.