<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>typeof(Blog); &#187; wcf</title>
	<atom:link href="http://blog.reveille.org.uk/tag/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.reveille.org.uk</link>
	<description>.net development thoughts and others</description>
	<lastBuildDate>Sat, 11 Sep 2010 15:48:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>A generic WCF service client</title>
		<link>http://blog.reveille.org.uk/2010/03/a-generic-wcf-service-client/</link>
		<comments>http://blog.reveille.org.uk/2010/03/a-generic-wcf-service-client/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 14:18:36 +0000</pubDate>
		<dc:creator>Graham Clark</dc:creator>
				<category><![CDATA[coding problems]]></category>
		<category><![CDATA[wcf]]></category>

		<guid isPermaLink="false">http://blog.reveille.org.uk/?p=89</guid>
		<description><![CDATA[Once you&#8217;ve used svcutil to create proxy classes for a service (or created a Service Reference in Visual Studio), it&#8217;s often a bit of a pain to write the wrapper code that needs to instantiate the client, call the service operation, and close or abort the client as appropriate. So I wrote a generic ServiceClient [...]]]></description>
			<content:encoded><![CDATA[<p>Once you&#8217;ve used <code>svcutil</code> to create proxy classes for a service (or created a Service Reference in Visual Studio), it&#8217;s often a bit of a pain to write the wrapper code that needs to instantiate the client, call the service operation, and close or abort the client as appropriate. So I wrote a generic <code>ServiceClient</code> class which will hopefully help to eliminate some boiler-plate code &#8211; it&#8217;s shown below.</p>
<pre class="brush: csharp; collapse: true; light: false; title: ; toolbar: true; wrap-lines: true;">
/// &lt;summary&gt;
/// A WCF service client.
/// &lt;/summary&gt;
/// &lt;typeparam name=&quot;TContract&quot;&gt;The type of the service contract.&lt;/typeparam&gt;
internal class ServiceClient&lt;TContract&gt; : IDisposable
	where TContract : class
{
	/// &lt;summary&gt;
	/// The service client object.
	/// &lt;/summary&gt;
	private TContract client;

	/// &lt;summary&gt;
	/// Initializes a new instance of the ServiceClient class.
	/// &lt;/summary&gt;
	/// &lt;param name=&quot;endpointName&quot;&gt;The name of the endpoint to use.&lt;/param&gt;
	public ServiceClient(string endpointName)
	{
		ChannelFactory&lt;TContract&gt; f = new ChannelFactory&lt;TContract&gt;(endpointName);
		this.client = f.CreateChannel();
	}

	/// &lt;summary&gt;
	/// Calls an operation on the service.
	/// &lt;/summary&gt;
	/// &lt;typeparam name=&quot;TResult&quot;&gt;The type of the object returned by the service operation.&lt;/typeparam&gt;
	/// &lt;param name=&quot;operation&quot;&gt;The operation to call.&lt;/param&gt;
	/// &lt;returns&gt;The results of the service operation.&lt;/returns&gt;
	public TResult ServiceOperation&lt;TResult&gt;(Func&lt;TContract, TResult&gt; operation)
	{
		return operation(this.client);
	}

	/// &lt;summary&gt;
	/// Calls an operation on the service.
	/// &lt;/summary&gt;
	/// &lt;param name=&quot;operation&quot;&gt;The operation to call.&lt;/param&gt;
	public void ServiceOperation(Action&lt;TContract&gt; operation)
	{
		operation(this.client);
	}

	/// &lt;summary&gt;
	/// Disposes this object.
	/// &lt;/summary&gt;
	public void Dispose()
	{
		try
		{
			if (this.client != null)
			{
				IClientChannel channel = this.client as IClientChannel;
				if (channel.State == CommunicationState.Faulted)
				{
					channel.Abort();
				}
				else
				{
					channel.Close();
				}
			}
		}
		finally
		{
			this.client = null;
			GC.SuppressFinalize(this);
		}
	}
}
</pre>
<p>With this class, you can call an operation <code>SaveEmployee</code> on a service with an <code>IEmployeeService</code> interface like this:</p>
<pre class="brush: csharp; title: ;">
using (var client = new ServiceClient&lt;IEmployeeService&gt;(&quot;EmployeeEndpoint&quot;))
{
	var response = client.ServiceOperation&lt;EmployeeResponse&gt;(
		c =&gt; c.SaveEmployee(request));
}
</pre>
<p>Wrapping the call in a <code>using</code> block means the client object is correctly closed and disposed, using the <code>IDisposable</code> interface. And if you&#8217;re calling an operation that doesn&#8217;t return anything, you can use the overload of <code>ServiceOperation</code> that takes in an <code>Action</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.reveille.org.uk/2010/03/a-generic-wcf-service-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Extension Methods for WCF Service Translators</title>
		<link>http://blog.reveille.org.uk/2010/03/extension-methods-for-wcf-service-translators/</link>
		<comments>http://blog.reveille.org.uk/2010/03/extension-methods-for-wcf-service-translators/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 12:19:04 +0000</pubDate>
		<dc:creator>Graham Clark</dc:creator>
				<category><![CDATA[coding problems]]></category>
		<category><![CDATA[extension methods]]></category>
		<category><![CDATA[wcf]]></category>

		<guid isPermaLink="false">http://blog.reveille.org.uk/?p=78</guid>
		<description><![CDATA[A common task when consuming a WCF service is translating your business objects into the proxy objects used in a service operation call. As an example, say you&#8217;ve got a Person class, and you&#8217;re calling a WCF service operation called SaveEmployee(). When creating a reference to this service, the svcutil program will have created some [...]]]></description>
			<content:encoded><![CDATA[<p>A common task when consuming a WCF service is translating your business objects into the proxy objects used in a service operation call. As an example, say you&#8217;ve got a <code>Person</code> class, and you&#8217;re calling a WCF service operation called <code>SaveEmployee()</code>. When creating a reference to this service, the <a href="http://msdn.microsoft.com/en-us/library/aa347733.aspx"><code>svcutil</code></a> program will have created some proxy classes. To call the <code>SaveEmployee()</code> operation, you&#8217;ll need to translate your local <code>Person</code> object into the proxy object. Extension methods provide a nice clear way of doing this.</p>
<p>So, the local Person business class will be something like this:</p>
<pre class="brush: csharp; title: ;">
public class Person
{
    public string Forename { get; set; }

    public string Surname { get; set; }

    public int Age { get; set; }
}
</pre>
<p>We want to call a WCF service called <code>EmployeeService</code>, which has an operation <code>SaveEmployee()</code>. This operation takes a person to save as a parameter. The type of this parameter is <code>Employee</code>, which <code>svcutil</code> has created for us in the service proxy. This may look something like this:</p>
<pre class="brush: csharp; title: ;">
internal partial class Employee :
    object, System.Runtime.Serialization.IExtensibleDataObject
{
    private string FirstNameField;
    private string LastNameField;
    private int AgeField;

    [System.Runtime.Serialization.DataMemeberAttribute()]
    internal string FirstName
    {
        get { return this.FirstNameField; }
        set { this.FirstNameField = value; }
    }

    [System.Runtime.Serialization.DataMemeberAttribute()]
    internal string LastName
    {
        get { return this.LastNameField; }
        set { this.LastNameField = value; }
    }

    [System.Runtime.Serialization.DataMemeberAttribute()]
    internal int Age
    {
        get { return this.AgeField; }
        set { this.AgeField = value; }
    }
}
</pre>
<p>As the service operation <code>SaveEmployee</code> needs an <code>Employee</code> and we have a <code>Person</code>, we need to translate from the latter to the former. An extension object can do this for us nicely:</p>
<pre class="brush: csharp; title: ;">
internal static class PersonTranslator
{
    public static Employee TranslateToEmployee(this Person person)
    {
        return new Employee()
        {
            FirstName = person.Forename,
            LastName = person.Surname,
            Age = person.Age
        };
    }
}
</pre>
<p>Now the call to the service operation is nice and clear:</p>
<pre class="brush: csharp; title: ;">
public SaveEmployee(Person person)
{
    var client = new EmployeeServiceClient();
    client.SaveEmployee(person.TranslateToEmployee());
    client.Close();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.reveille.org.uk/2010/03/extension-methods-for-wcf-service-translators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WCF Configuration &#8211; Referencing certificates with long subjects</title>
		<link>http://blog.reveille.org.uk/2010/01/wcf-configuration-certificates/</link>
		<comments>http://blog.reveille.org.uk/2010/01/wcf-configuration-certificates/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 11:09:20 +0000</pubDate>
		<dc:creator>Graham Clark</dc:creator>
				<category><![CDATA[coding problems]]></category>
		<category><![CDATA[certificates]]></category>
		<category><![CDATA[wcf]]></category>

		<guid isPermaLink="false">http://blog.reveille.org.uk/?p=63</guid>
		<description><![CDATA[I was trying to set up a WCF net-tcp service on a test environment, but kept getting an error saying the certificate could not be found. The problem was down to a slight difference between the test certificates we were using locally, and the certificate we were given for the test environment. We&#8217;re using FindBySubjectDistinguishedName [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to set up a WCF net-tcp service on a test environment, but kept getting an error saying the certificate could not be found. The problem was down to a slight difference between the test certificates we were using locally, and the certificate we were given for the test environment. We&#8217;re using FindBySubjectDistinguishedName in the config.</p>
<p>The test certificate&#8217;s subject looked like this:</p>
<p><code>CN = TestCertName</code></p>
<p>Whereas the environment certificate&#8217;s subject looked like:</p>
<p><code>CN = CertName<br />
OU = Company Ltd.<br />
O = Company<br />
L = Town<br />
S = County<br />
C = Country Code</code></p>
<p>All the examples I&#8217;ve seen just cover certificates with the <code>CN</code> part. This is straightforward to reference in the WCF config:</p>
<pre class="brush: xml; light: true; title: ;">
&lt;serviceCredentials&gt;
   &lt;serviceCertificate
            storeLocation=&quot;LocalMachine&quot;
            storeName=&quot;My&quot;
            findValue=&quot;CN=TestCertName&quot;
            x509FindType=&quot;FindBySubjectDistinguishedName&quot; /&gt;
&lt;/serviceCredentials&gt;
</pre>
<p>However, when the certificate subject has multiple parts (i.e. more than just <code>CN</code>), you need to put all of them in the <code>findValue</code> attribute. But how to separate them? I tried several characters &#8211; space, comma, semicolon, colon &#8211; none worked. The certificate could not be found! Finally I noticed that in the top part of the certificate&#8217;s properties window, the values are separated by a comma <em>and</em> a space. Unbelievably, this also applies to the config! How intuitive. So for the &#8220;CertName&#8221; certificate above, here&#8217;s how to reference it in the WCF config:</p>
<pre class="brush: xml; light: true; title: ;">
&lt;serviceCredentials&gt;
   &lt;serviceCertificate
            storeLocation=&quot;LocalMachine&quot;
            storeName=&quot;My&quot;
            findValue=&quot;CN=TestCertName, OU=Company Ltd., O=Company, L=Town, S=County, C=Country Code&quot;
            x509FindType=&quot;FindBySubjectDistinguishedName&quot; /&gt;
&lt;/serviceCredentials&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.reveille.org.uk/2010/01/wcf-configuration-certificates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

