Click here to Skip to main content
15,895,462 members
Articles / Programming Languages / C#

Generic WCF Service Host and Client

Rate me:
Please Sign up or sign in to vote.
4.96/5 (21 votes)
13 May 2010CPOL5 min read 261.2K   4.7K   118  
A generic WCF Windows Service host and client with minimal configuration, with asynchronous support.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace WcfServiceCommon
{
	[DataContract]
	public class WcfServiceFault
	{
		public WcfServiceFault Inner { get; set; }

		[DataMember]
		public string Message { get; set; }

		[DataMember]
		public string Source { get; set; }

		[DataMember]
		public string Target { get; set; }

		public override string ToString()
		{
			if (null == Inner)
				return string.Format("Target: {0} / Source: {1} / Message: {2}", Target, Source, Message);
			else
				return string.Format("Target: {0} / Source: {1} / Message: {2}{3}/ Inner: {4}", Target, Source, Message, Environment.NewLine, Inner.ToString());
		}
	}

	public static class WcfServiceFaultFactory
	{
		public static WcfServiceFault CreateWcfServiceFault(Exception ex)
		{
			WcfServiceFault fault = new WcfServiceFault() { Message = ex.Message, Source = ex.Source, Target = ex.TargetSite.ToString() };
			if (null != ex.InnerException)
			{
				WcfServiceFault wrapper = fault;
				Exception te = ex.InnerException;
				while (null != te)
				{
					wrapper.Inner = new WcfServiceFault() { Message = te.Message, Source = te.Source, Target = te.TargetSite.ToString() };
					te = te.InnerException;
					wrapper = wrapper.Inner;
				}
			}
			return fault;
		}
	}
}

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
United States United States
Since 2001 I've been writing .NET applications in C# and architecting n-tier applications in the enterprise. Before that I worked as a tech writer for nine years. Don't bother doing the math. I'm old. Ever since I laid eyes on my first Commodore PET, I've been a technologist. I've worked in the software world for fifteen years. I started as a technical writer and learned to code from the best engineers as I worked with them in creating technical documentation. It was then that I learned that writing code was more fun and frankly easier than writing about code. I've been doing both ever since. You can visit my blog at http://www.tsjensen.com/blog.

Comments and Discussions