Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceLocator.cs @ 5591

Last change on this file since 5591 was 5526, checked in by cneumuel, 14 years ago

#1233

  • fixed handling of StateLog in DataLayer
  • extended unit tests
  • changed style of service calls to OKB-like style (using delegates)
  • added possibility that parent jobs can be finished immediately when child jobs are finished
File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.ServiceModel;
24using HeuristicLab.Clients.Common;
25using HeuristicLab.Services.Hive.Common.ServiceContracts;
26
27namespace HeuristicLab.Clients.Hive {
28  public class ServiceLocator : IServiceLocator {
29    private static IServiceLocator instance = null;
30    public static IServiceLocator Instance {
31      get {
32        if (instance == null) {
33          instance = new ServiceLocator();
34        }
35        return instance;
36      }
37      set {
38        instance = value;
39      }
40    }
41
42    private string username;
43    public string Username {
44      get { return username; }
45      set { username = value; }
46    }
47
48    private string password;
49    public string Password {
50      get { return password; }
51      set { password = value; }
52    }
53   
54    public Disposable<ChannelFactory<IHiveService>> GetService() {
55      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password)) {
56        return ClientFactory.CreateChannelFactory<IHiveService>("wsHttpBinding_IHiveService");
57      } else {
58        return ClientFactory.CreateChannelFactory<IHiveService>("wsHttpBinding_IHiveService", null, username, password);
59      }
60    }
61
62    public void CallHiveService(Action<IHiveService> call) {
63      using (var channelFactory = this.GetService()) {
64        var service = channelFactory.Obj.CreateChannel();
65        call(service);
66      } // disposing the channelfactory is done by the disposable object; the channel gets disposed when the channelfactory is disposed
67    }
68
69    private T CallHiveService<T>(Func<IHiveService, T> call) {
70      using (var channelFactory = this.GetService()) {
71        var service = channelFactory.Obj.CreateChannel();
72        return call(service);
73      } // disposing the channelfactory is done by the disposable object; the channel gets disposed when the channelfactory is disposed
74    }
75
76    T IServiceLocator.CallHiveService<T>(Func<IHiveService, T> call) {
77      return CallHiveService<T>(call);
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.