Home
Manage Your Code
Snippet: SPSolutions.ServiceModel.ServiceUtil (C#)
Title: SPSolutions.ServiceModel.ServiceUtil Language: C#
Description: WCF client utility class for calling web services. Views: 516
Author: Tony Bierman Date Added: 11/27/2007
Copy Code  
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}
Usage
UseServiceClient(delegate(MyApplication.Web_Reference.ListsSoap client)
{
    // Build the service request and get the service response
    MyApplication.Web_Reference.GetListItemsRequestBody reqBody =
        new MyApplication.Web_Reference.GetListItemsRequestBody(listName, viewName, query, viewFields, rowLimit, queryOptions, null);
    MyApplication.Web_Reference.GetListItemsRequest req = new MyApplication.Web_Reference.GetListItemsRequest(reqBody);
    MyApplication.Web_Reference.GetListItemsResponse resp = client.GetListItems(req);
    MyApplication.Web_Reference.GetListItemsResponseBody result = resp.Body;

    // Declare an XmlNode object and initialize it with the XML response from the GetListItems method. 
    XmlNode retval = result.GetListItemsResult;
});
Notes
Related snippets: http://www.codekeep.net/snippets/36635bf8-85cb-41d6-a04e-88ba8ca730ad.aspx