Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.Contracts/3.3/WcfServicePool.cs @ 5588

Last change on this file since 5588 was 5588, checked in by cneumuel, 13 years ago

#1260

  • fixed compatibility issue
  • updated hive binaries
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.Common;
26
27namespace HeuristicLab.Hive.Contracts {
28  public class WcfServicePool<T> where T : class {
29    private static object locker = new object();
30
31    private string hostAddress;
32    public string HostAddress {
33      get { return hostAddress; }
34      set { hostAddress = value; }
35    }
36    private string userName;
37    public string UserName {
38      get { return userName; }
39      set { userName = value; }
40    }
41    private string password;
42    public string Password {
43      get { return password; }
44      set { password = value; }
45    }
46
47    private string endpointName;
48
49    public WcfServicePool(string endpointName) {
50      this.endpointName = endpointName;
51    }
52    public WcfServicePool(string hostAddress, string endpointName) : this(endpointName) {
53      this.hostAddress = hostAddress;
54    }
55    public WcfServicePool(string endpointName, string userName, string password) : this(endpointName) {
56      this.userName = userName;
57      this.password = password;
58    }
59
60    public Disposable<T> GetService() {
61      try {
62        if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) {
63          var factory = ClientFactory.CreateChannelFactory<T>(endpointName, hostAddress, userName, password);
64          return new Disposable<T>(factory.Obj.CreateChannel());
65        } else {
66          var factory = ClientFactory.CreateChannelFactory<T>(endpointName, hostAddress);
67          return new Disposable<T>(factory.Obj.CreateChannel());     
68        }
69      }
70      catch (EndpointNotFoundException ex) {
71        OnExceptionOccured(ex);
72      }
73      return null;
74    }
75
76    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
77    private void OnExceptionOccured(Exception e) {
78      lock (locker) {
79        var handler = ExceptionOccured;
80        if (handler != null) handler(this, new EventArgs<Exception>(e));
81      }
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.