Click here to Skip to main content
15,883,731 members
Articles / Desktop Programming / WPF

Extending WCF Part I

Rate me:
Please Sign up or sign in to vote.
4.80/5 (30 votes)
7 Nov 2009CPOL5 min read 85.7K   2.2K   87  
An article on using external configuration file at WCF client
using System;
using System.IO;
using System.Windows;
using System.ServiceModel;
using ExtendingWCFPartI.WcfExtentions;
using ExtendingWCFPartI.Common.Services;
using System.Text;

namespace ExtendingWCFPartI.Client
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private static readonly string ExternalConfigPath = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\WcfClientConfiguration.xml");

        public Window1()
        {
            InitializeComponent();
        }

        private void GetCustomerList()
        {
            try
            {
                var proxy = WcfClientHelper.GetProxy<ICustomerService>(ExternalConfigPath);
                DataContext = proxy.GetCustomers();
            }
            catch(EndpointNotFoundException enfe)
            {
                MessageBox.Show("Please run the service before. " + Environment.NewLine + enfe.Message);
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void btnGetCustomers_Click(object sender, RoutedEventArgs e)
        {
            GetCustomerList();
        }

        private void btnGetException_Click(object sender, RoutedEventArgs e)
        {
            GetExceptionFromService();
        }

        private void GetExceptionFromService()
        {
            try
            {
                var proxy = WcfClientHelper.GetProxy<ICustomerService>(ExternalConfigPath);
                proxy.GetException();
            }
            catch (Exception exception)
            {
                ShowMessage(GetExceptionDetail(exception));
            }
        }

        private string GetExceptionDetail(Exception exception)
        {
            var sb = new StringBuilder();
            sb.Append(string.Format("Exception type: {0}{1}", exception.GetType().FullName, Environment.NewLine));
            sb.Append(string.Format("Exception Message: {0}{1}", exception.Message, Environment.NewLine));
            var innerException = exception.InnerException;
            var counter = 1;
            while (null != innerException)
            {
                sb.Append(string.Format("Inner Exception {0} type: {1}{2}", counter, innerException.GetType().FullName, Environment.NewLine));
                sb.Append(string.Format("Inner Exception {0} message: {1}{2}", counter, innerException.GetType().FullName, Environment.NewLine));
                innerException = innerException.InnerException;
                counter++;
            }
            sb.Append(string.Format("Stacktrace: {0}", exception.StackTrace));
            return sb.ToString();
        }

        private void ShowMessage(string message)
        {
            txtMessage.Text = message;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer KAZ Software Limited
Bangladesh Bangladesh

Comments and Discussions