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 |
|
---|
22 | using System;
|
---|
23 | using System.ServiceModel;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Hive.Tracing;
|
---|
26 | using HeuristicLab.Clients.Common;
|
---|
27 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Hive.Contracts {
|
---|
30 | public class WcfServicePool<T> where T : class {
|
---|
31 | private static object locker = new object();
|
---|
32 |
|
---|
33 | private string hostAddress;
|
---|
34 | public string HostAddress {
|
---|
35 | get { return hostAddress; }
|
---|
36 | set { hostAddress = value; }
|
---|
37 | }
|
---|
38 | private string userName;
|
---|
39 | public string UserName {
|
---|
40 | get { return userName; }
|
---|
41 | set { userName = value; }
|
---|
42 | }
|
---|
43 | private string password;
|
---|
44 | public string Password {
|
---|
45 | get { return password; }
|
---|
46 | set { password = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private string endpointName;
|
---|
50 |
|
---|
51 | public WcfServicePool(string endpointName) {
|
---|
52 | this.endpointName = endpointName;
|
---|
53 | }
|
---|
54 | public WcfServicePool(string hostAddress, string endpointName) : this(endpointName) {
|
---|
55 | this.hostAddress = hostAddress;
|
---|
56 | }
|
---|
57 | public WcfServicePool(string endpointName, string userName, string password) : this(endpointName) {
|
---|
58 | this.userName = userName;
|
---|
59 | this.password = password;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public Disposable<T> GetService() {
|
---|
63 | try {
|
---|
64 | if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) {
|
---|
65 | return ClientFactory.CreateClient<T>(endpointName, hostAddress, userName, password);
|
---|
66 | } else {
|
---|
67 | return ClientFactory.CreateClient<T>(endpointName, hostAddress);
|
---|
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 | }
|
---|