1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Net;
5using System.ServiceModel;
6using System.ServiceModel.Channels;
7using System.Xml.Xsl;
8using System.Xml;
9
10namespace SPSolutions.ServiceModel
11{
12 internal static class ServiceUtil
13 {
14 /// <summary>
15 ///
16 /// </summary>
17 /// <typeparam name="T"></typeparam>
18 /// <param name="action"></param>
19 internal static void UseServiceClient<T>(Action<T> action)
20 {
21 UseServiceClient<T>(action, null, null, null);
22 }
23
24 /// <summary>
25 ///
26 /// </summary>
27 /// <typeparam name="T"></typeparam>
28 /// <param name="action"></param>
29 /// <param name="domain"></param>
30 /// <param name="userName"></param>
31 /// <param name="password"></param>
32 internal static void UseServiceClient<T>(Action<T> action, string domain, string userName, string password)
33 {
34 ChannelFactory<T> factory = new ChannelFactory<T>("*");
35
36 // Otherwise use default credentials
37 if (!(string.IsNullOrEmpty(domain) && string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)))
38 {
39 factory.Credentials.Windows.ClientCredential.Domain = domain;
40 factory.Credentials.Windows.ClientCredential.UserName = userName;
41 factory.Credentials.Windows.ClientCredential.Password = password;
42 }
43
44 T client = factory.CreateChannel();
45
46 try
47 {
48 action(client);
49 ((IClientChannel)client).Close();
50 factory.Close();
51 }
52 catch
53 {
54 ((IClientChannel)client).Abort();
55 factory.Abort();
56 throw;
57 }
58 }
59 }
60}