<?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>Stefano Ricciardi &#187; WCF</title>
	<atom:link href="http://stefanoricciardi.com/category/net/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://stefanoricciardi.com</link>
	<description>On Software Development and Thereabouts</description>
	<lastBuildDate>Tue, 15 Nov 2011 07:57:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>File Transfer with WCF: Part III</title>
		<link>http://stefanoricciardi.com/2010/09/02/file-transfer-with-wcf-part-iii/</link>
		<comments>http://stefanoricciardi.com/2010/09/02/file-transfer-with-wcf-part-iii/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 12:08:55 +0000</pubDate>
		<dc:creator>stefanoricciardi</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://stefanoricciardi.com/?p=1099</guid>
		<description><![CDATA[This is the third post on a small series about transferring large files with WCF using streaming : File Transfer With WCF File Transfer With WCF: Part II File Transfer With WCF: Part III I am closing this series of posts by sharing a skeleton for the client side handling of the uploaded and downloaded [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstefanoricciardi.com%2F2010%2F09%2F02%2Ffile-transfer-with-wcf-part-iii%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstefanoricciardi.com%2F2010%2F09%2F02%2Ffile-transfer-with-wcf-part-iii%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><em>This is the third post on a small series about transferring large files with WCF using streaming</em> :</p>
<ul>
<li><a href="http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/">File Transfer With WCF</a></li>
<li><a href="http://stefanoricciardi.com/2009/10/02/file-transfer-with-wcf-part-ii/">File Transfer With WCF: Part II</a></li>
<li><a href="http://stefanoricciardi.com/2010/09/02/file-transfer-with-wcf-part-iii/">File Transfer With WCF: Part III</a></li>
</ul>
<p>I am closing this series of posts by sharing a skeleton for the client side handling of the uploaded and downloaded files. Please note that the following are excerpt from a larger context and so will not compile in your environment <em>as is</em>: they are only intended to give a rough idea of how the files can be handled.</p>
<h3>Download</h3>
<pre class="brush:csharp">
 private void IssueDownloadRequest(string localFile, string serviceUrl, FileDownloadMessage request)
{
    this.service = this.proxy.OpenProxy(serviceUrl);

    try
    {
        using (FileDownloadReturnMessage response = this.service.DownloadFile(request))
        {
            if (response != null &#038;&#038; response.FileByteStream != null)
            {
                SaveFile(response.FileByteStream, localFile);
            }
        }

        this.proxy.CloseProxy(this.service);
    }
    catch (Exception e)
    {
        throw new FileTransferProxyException("Error while downloading the file", e);
    }
    finally
    {
        // we expect the stream returned from the server to be closed by the
        // server itself so nothing to be done with it here. Just abort
        // the proxy if needed.
        this.proxy.AbortProxyIfNeeded(this.service);
    }
}

private static void SaveFile(Stream saveFile, string localFilePath)
{
    const int bufferSize = 65536; // 64K

    using (FileStream outfile = new FileStream(localFilePath, FileMode.Create))
    {
        byte[] buffer = new byte[bufferSize];
        int bytesRead = saveFile.Read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
            outfile.Write(buffer, 0, bytesRead);
            bytesRead = saveFile.Read(buffer, 0, bufferSize);
        }
    }
}
</pre>
<h3>Upload</h3>
<pre class="brush:csharp">
public void UploadFile(string localFileName, string serviceUrl, FileTypeEnum fileType)
{
    this.service = this.proxy.OpenProxy(serviceUrl);
    try
    {
        using (Stream fileStream = new FileStream(localFileName, FileMode.Open, FileAccess.Read))
        {
            var request = new FileUploadMessage();
            string remoteFileName = null;
            if (fileType == FileTypeEnum.Generic)
            {
                // we are using the service as a "FTP on WCF"
                // give the remote file the same name as the local one
                remoteFileName = Path.GetFileName(localFileName);
            }

            var fileMetadata = new FileMetaData(localFileName, remoteFileName, fileType);
            request.MetaData = fileMetadata;
            request.FileByteStream = fileStream;

            this.service.UploadFile(request);
            this.proxy.CloseProxy(this.service);
        }
    }
    catch (IOException)
    {
        throw new FileTransferProxyException("Unable to open the file to upload");
    }
    catch (Exception e)
    {
        throw new FileTransferProxyException(e.Message);
    }
    finally
    {
        this.proxy.AbortProxyIfNeeded(this.service);
    }
 }
</pre>
<p><a href="http://www.dotnetkicks.com/kick/?url=http://stefanoricciardi.com/2010/09/02/file-transfer-with-wcf-part-iii/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://stefanoricciardi.com/2010/09/02/file-transfer-with-wcf-part-iii/" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<p><a rev="vote-for" href="http://dotnetshoutout.com/File-Transfer-with-WCF-Part-III-Stefano-Ricciardi"><img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fstefanoricciardi.com%2F2010%2F09%2F02%2Ffile-transfer-with-wcf-part-iii%2F" style="border:0px"/></a></p>

]]></content:encoded>
			<wfw:commentRss>http://stefanoricciardi.com/2010/09/02/file-transfer-with-wcf-part-iii/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Making WCF Serializer Work with Circular References</title>
		<link>http://stefanoricciardi.com/2009/10/22/making-wcf-serializer-work-with-circular-references/</link>
		<comments>http://stefanoricciardi.com/2009/10/22/making-wcf-serializer-work-with-circular-references/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 12:07:43 +0000</pubDate>
		<dc:creator>stefanoricciardi</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://stefanoricciardi.net/?p=465</guid>
		<description><![CDATA[The Problem of Circular References Recently I had to model a tree-like structure using a variation of the GoF composite design pattern and to pass this class to a WCF service for further processing. The class has circular reference as described by the following drawing: Therefore off I went to naively create my DataContract along [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F10%2F22%2Fmaking-wcf-serializer-work-with-circular-references%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F10%2F22%2Fmaking-wcf-serializer-work-with-circular-references%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h1>The Problem of Circular References</h1>
<p>Recently I had to model a tree-like structure using a variation of the GoF <a title="Composite Design Pattern" href="http://en.wikipedia.org/wiki/Composite_pattern">composite design pattern</a> and to pass this class to a WCF service for further processing.</p>
<p>The class has circular reference as described by the following drawing:</p>
<p><img class="aligncenter size-full wp-image-474" title="Example of a Node Recursion" src="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/10/noderecursion.gif" alt="Example of a Node Recursion" width="219" height="117" /></p>
<p>Therefore off I went to naively create my <strong>DataContract</strong> along the following lines:</p>
<pre class="brush:csharp">
[DataContract(Namespace = "http://schemas.acme.it/2009/10"]
public class Node
{
	[DataMember(Name="Children", Order=2)]
    private IList&lt;Node&gt; children;

    [DataMember(Name="Id", Order=0)]
    public string Id { get; set; }

    [DataMember(Name="Parent", Order=1)]
    public Node Parent { get; set; }

    public Node()
    {
        children = new List&lt;Node&gt;()
    }

    public void AddChild(Node node)
    {
        this.children.Add(node);
        node.Parent = this;
    }

    public IList&lt;Node&gt; Children
    {
        get
        {
            IList&lt;Node&gt; result = new List(this.children);
            foreach (Node child in this.children)
            {
                foreach (Node innerChild in child.Children)
                {
                result.Add(innerChild);
                }
            }

            return result;
        }
    }
}
</pre>
<p>Unfortunately at run time I was greeted by the following exception:<br />
<code>Object graph for type 'System.Collections.Generic.List`1[[Node ...]]' contains cycles and cannot be serialized if reference tracking is disabled.</code>.</p>
<h1>The Solution</h1>
<p>Reading around I realized that up until some time ago you had to implement your own <strong>DataContractSerializer</strong> (see for example this <a href="http://blogs.msdn.com/sowmy/archive/2006/03/26/561188.aspx">post </a> by Sowmy Srinivasan).</p>
<p>Luckily, it turns out that with NET 3.5 SP1 everything is much simpler: you just have to use the <strong>IsReference</strong> parameter in the <strong>DataContract</strong> attribute, as follows:</p>
<pre class="brush:csharp">
[DataContract(Namespace = "http://schemas.acme.it/2009/10", IsReference=true)]
public class Node
{
    // everything same as in the example above.
}
</pre>
<h1>The Catch</h1>
<p>For some reason, if you turn on the <strong>IsReference</strong> attribute, then you <em>cannot </em>set the <strong>IsRequired</strong> attribute to <code>true</code> on <em>any</em> <strong>DataMember</strong> of the DataContract. The reason is somehow explained <a href="http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/e5d38e14-31b0-4991-a988-d578c564e92d">here</a>.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F10%2F22%2Fmaking-wcf-serializer-work-with-circular-references"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F10%2F29%2Fa-singleton-service-locator-pattern" border="0" alt="kick it on DotNetKicks.com" /></a><br />
<!-- Kick it END --></p>
<p><a rev="vote-for" href="http://dotnetshoutout.com/Submit?url=http://stefanoricciardi.com/2009/10/22/making-wcf-serializer-work-with-circular-references/"><br />
<img src="http://dotnetshoutout.com/image.axd?url=http://stefanoricciardi.com/2009/10/22/making-wcf-serializer-work-with-circular-references/" alt="Shout it" /><br />
</a></p>
<p><!-- AddThis Button BEGIN --></p>
<p><a href="http://www.addthis.com/bookmark.php?v=250"><img style="border: 0;" src="http://s7.addthis.com/static/btn/lg-share-en.gif" alt="Bookmark and Share" width="125" height="16" /></a></p>
<p><!-- AddThis Button END --></p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e6ceeafa-fe5c-4bd6-a48d-325da43e5abc" class="wlWriterEditableSmartContent" style="display: inline; float: none; margin: 0; padding: 0;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/Programming">Programming</a>, <a rel="tag" href="http://technorati.com/tags/C%23">C#</a>, <a rel="tag" href="http://technorati.com/tags/wcf">WCF</a>, <a rel="tag" href="http://technorati.com/tags/soa">SOA</a></div>

]]></content:encoded>
			<wfw:commentRss>http://stefanoricciardi.com/2009/10/22/making-wcf-serializer-work-with-circular-references/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>File Transfer with WCF: Part II</title>
		<link>http://stefanoricciardi.com/2009/10/02/file-transfer-with-wcf-part-ii/</link>
		<comments>http://stefanoricciardi.com/2009/10/02/file-transfer-with-wcf-part-ii/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 12:23:22 +0000</pubDate>
		<dc:creator>stefanoricciardi</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://stefanoricciardi.net/?p=385</guid>
		<description><![CDATA[This is the second post on a small series about transferring large files with WCF using streaming : File Transfer With WCF File Transfer With WCF: Part II File Transfer With WCF: Part III A few readers of my previous post on File Transfer with WCF have asked to share the implementation of the UploadFile [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F10%2F02%2Ffile-transfer-with-wcf-part-ii%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F10%2F02%2Ffile-transfer-with-wcf-part-ii%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><em>This is the second post on a small series about transferring large files with WCF using streaming</em> :</p>
<ul>
<li><a href="http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/">File Transfer With WCF</a></li>
<li><a href="http://stefanoricciardi.com/2009/10/02/file-transfer-with-wcf-part-ii/">File Transfer With WCF: Part II</a></li>
<li><a href="http://stefanoricciardi.com/2010/09/02/file-transfer-with-wcf-part-iii/">File Transfer With WCF: Part III</a></li>
</ul>
<p>A few readers of my previous post on <a href="http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/">File Transfer with WCF</a> have asked to share the implementation of the UploadFile and DownloadFile methods.</p>
<p>While I can&#8217;t share the complete implemention of the service for obvious Intellectual Property reasons, I will outline the basic steps involved in implementing the service. Please refer to my previous <a href="http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/">post</a> for the details on the DataContracts and OperationContracts involved here.</p>
<p><em>NOTE</em>: I have extracted these portions from the actual code. I cannot verify this to work as-is, but it should give you a head-start to complete your own service. You should also add your own error checking, and any pre or post-download and/or pre or post-upload processing as required by the business.</p>
<h3>Download</h3>
<pre class="brush:csharp">
public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)
{
    // parameters validation omitted for clarity
    string localFileName = request.MetaData.LocalFileName;

    try
    {
        string basePath = ConfigurationSettings.AppSettings[&quot;FileTransferPath&quot;];
        string serverFileName = Path.Combine(basePath, request.MetaData.RemoteFileName);

        Stream fs = new FileStream(serverFileName, FileMode.Open);

        return new FileDownloadReturnMessage(new FileMetaData(localFileName, serverFileName), fs);
    }
    catch (IOException e)
    {
        throw new FaultException&lt;IOException&gt;(e);
    }
}
</pre>
<h3>Upload</h3>
<pre class="brush:csharp">
public void UploadFile(FileUploadMessage request)
{
    // parameters validation omitted for clarity
    try
    {
        string basePath = ConfigurationSettings.AppSettings[&quot;FileTransferPath&quot;];
        string serverFileName = Path.Combine(basePath, request.MetaData.RemoteFileName);

        using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
        {
            const int bufferSize = 65536; // 64K

            Byte[] buffer = new Byte[bufferSize];
            int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);

            while (bytesRead &gt; 0)
            {
                outfile.Write(buffer, 0, bytesRead);
                bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
            }
        }
    }
    catch (IOException e)
    {
        throw new FaultException&lt;IOException&gt;(e);
    }
}
</pre>
<p><!-- AddThis Button BEGIN --></p>
<p><a href="http://www.addthis.com/bookmark.php?v=250"><img style="border:0;" src="http://s7.addthis.com/static/btn/lg-share-en.gif" alt="Bookmark and Share" width="125" height="16" /></a></p>
<p><!-- AddThis Button END --></p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e6ceeafa-fe5c-4bd6-a48d-325da43e5abc" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/Programming">Programming</a>, <a rel="tag" href="http://technorati.com/tags/C%23">C#</a>, <a rel="tag" href="http://technorati.com/tags/wcf">WCF</a>, <a rel="tag" href="http://technorati.com/tags/soa">SOA</a></div>

]]></content:encoded>
			<wfw:commentRss>http://stefanoricciardi.com/2009/10/02/file-transfer-with-wcf-part-ii/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>File Transfer with WCF</title>
		<link>http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/</link>
		<comments>http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 12:43:14 +0000</pubDate>
		<dc:creator>stefanoricciardi</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://stefanoricciardi.net/?p=318</guid>
		<description><![CDATA[This is the first post on a small series about transferring large files with WCF using streaming : File Transfer With WCF File Transfer With WCF: Part II File Transfer With WCF: Part III WCF streaming is helpful in all those cases when you need to move a lot of data between the client and [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F08%2F28%2Ffile-transfer-with-wcp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F08%2F28%2Ffile-transfer-with-wcp%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><em>This is the first post on a small series about transferring large files with WCF using streaming</em> :</p>
<ul>
<li><a href="http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/">File Transfer With WCF</a></li>
<li><a href="http://stefanoricciardi.com/2009/10/02/file-transfer-with-wcf-part-ii/">File Transfer With WCF: Part II</a></li>
<li><a href="http://stefanoricciardi.com/2010/09/02/file-transfer-with-wcf-part-iii/">File Transfer With WCF: Part III</a></li>
</ul>
<p>WCF streaming is helpful in all those cases when you need to move a lot of data between the client and the service in a way that keeps memory consumption under control (see <a href="http://msdn.microsoft.com/en-us/library/ms733742.aspx">Large Data and Streaming</a> on MSDN for more details if you are not familiar with the topic).</p>
<p>Streaming does impose some restrictions. Among them is that your operations can only have one input and/or output parameter and they can only be only one of the following:</p>
<ul>
<li>a Stream</li>
<li>a Message</li>
<li>a class implementing IXmlSerializable</li>
</ul>
<p>I wanted to expose a very simple service to upload and download files through WCF. And because I wanted to be able to pass both the file content as a stream <em>and</em> at least the file name, I could not just use a Stream but had to resort to using <a href="http://msdn.microsoft.com/en-us/library/ms734675.aspx">Messages</a>: the headers would convey the file information (such as the file type) and the body the content of the file itself through a stream.</p>
<p>I therefore defined the service as follows:</p>
<pre class="brush:csharp">
    [ServiceContract(Namespace = &quot;http://schemas.acme.it/2009/04&quot;)]
    public interface IFileTransferService
    {
        [OperationContract(IsOneWay = true)]
        void UploadFile(FileUploadMessage request);
        [OperationContract(IsOneWay = false)]
        FileDownloadReturnMessage DownloadFile(FileDownloadMessage request);
    }

    [MessageContract]
    public class FileUploadMessage
    {
        [MessageHeader(MustUnderstand = true)]
        public FileMetaData Metadata;
        [MessageBodyMember(Order = 1)]
        public Stream FileByteStream;
    }

    [MessageContract]
    public class FileDownloadMessage
    {
        [MessageHeader(MustUnderstand = true)]
        public FileMetaData FileMetaData;
    }

   [MessageContract]
    public class FileDownloadReturnMessage
    {
        public FileDownloadReturnMessage(FileMetaData metaData, Stream stream)
        {
            this.DownloadedFileMetadata = metaData;
            this.FileByteStream = stream;
        }

        [MessageHeader(MustUnderstand = true)]
        public FileMetaData DownloadedFileMetadata;
        [MessageBodyMember(Order = 1)]
        public Stream FileByteStream;
    }

   [DataContract(Namespace = &quot;http://schemas.acme.it/2009/04&quot;)]
    public class FileMetaData
    {
        public FileMetaData(
            string localFileName,
            string remoteFileName)
        {
            this.LocalFileName = localFileName;
            this.RemoteFileName = remoteFileName;
            this.FileType = FileTypeEnum.Generic;
        }

        public FileMetaData(
            string localFileName,
            string remoteFileName,
            FileTypeEnum fileType)
        {
            this.LocalFileName = localFileName;
            this.RemoteFileName = remoteFileName;
            this.FileType = fileType;
        }

        [DataMember(Name=&quot;FileType&quot;, Order=0, IsRequired=true)]
        public FileTypeEnum FileType;&lt;/p&gt;
        [DataMember(Name=&quot;localFilename&quot;, Order=1, IsRequired=false)]
        public string LocalFileName;
        [DataMember(Name = &quot;remoteFilename&quot;, Order = 2, IsRequired = false)]
        public string RemoteFileName;
    }

    public class FileTransferService : IFileTransferService
    {
        public void UploadFile(FileUploadMessage request)
        {
             // implementation here
        }

        public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)
        {
             // implementation here
        }
    }
</pre>
<p>I am omitting the details of the implementation, they are pretty straightforward (if you are interested, I will gladly share it. <em><span style="color:#ff0000;">UPDATE</span>: I have put the basic scheleton of the server implementation is a separate <a href="http://stefanoricciardi.com/2009/10/02/file-transfer-with-wcf-part-ii/">post</a></em>).</p>
<p>And this is how the service was configured on the server side within the web.config.</p>
<pre class="brush:xml">
&lt;system.serviceModel&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior name=&quot;serviceBehavior&quot;&gt;
          &lt;serviceMetadata httpGetEnabled=&quot;true&quot;/&gt;
          &lt;serviceDebug includeExceptionDetailInFaults=&quot;true&quot;
                              httpHelpPageEnabled=&quot;true&quot; /&gt;
          &lt;dataContractSerializer maxItemsInObjectGraph=&quot;2147483647&quot;/&gt;
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
    &lt;services&gt;
      &lt;service behaviorConfiguration=&quot;serviceBehavior&quot;
                  name=&quot;FileTransferService&quot;&gt;
        &lt;endpoint address=&quot;&quot;
                  name=&quot;basicHttpStream&quot;
                  binding=&quot;basicHttpBinding&quot;
                  bindingConfiguration=&quot;httpLargeMessageStream&quot;
                  contract=&quot;IFileTransferService&quot; /&gt;
        &lt;endpoint address=&quot;mex&quot;
                      binding=&quot;mexHttpBinding&quot;
                      contract=&quot;IMetadataExchange&quot;/&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;bindings&gt;
      &lt;basicHttpBinding&gt;
        &lt;binding name=&quot;httpLargeMessageStream&quot;
                    maxReceivedMessageSize=&quot;2147483647&quot;
                    transferMode=&quot;Streamed&quot;
                    messageEncoding=&quot;Mtom&quot; /&gt;
      &lt;/basicHttpBinding&gt;
    &lt;/bindings&gt;
  &lt;/system.serviceModel&gt;
</pre>
</p>
<p>This is the base of the plumbing that it&#8217;s needed to create the service.</p>
<p>While I was developing it, I have lost quite some time for an error which was really obscure to decipher:<br />
<em>Server Error in &#8216;/&#8217; Application. Operation &#8216;UploadFile&#8217; in contract &#8216;IFileTransferService&#8217; uses a MessageContract that has SOAP headers. SOAP headers are not supported by the None MessageVersion.</em></p>
<p>Googling and binging around I found almost no relevant information on the topic, so I was pretty on my own.</p>
<p>I thought the problem was in the HTTP binding that I was using (maybe it was not compatible with SOAP headers?). Unfortunately the HTTP binding did not have any way to specify a different <em>MessageVersion</em>. So I ended up creating a <a href="http://msdn.microsoft.com/en-us/library/aa347793.aspx">custom binding</a>, like the following, but that didn&#8217;t help either:</p>
<pre class="brush:xml">
      &lt;customBinding&gt;
        &lt;binding name=&quot;customHttpBindingStream&quot;&gt;
          &lt;textMessageEncoding messageVersion=&quot;Default&quot;/&gt;
          &lt;httpTransport
               transferMode=&quot;Streamed&quot;
               maxReceivedMessageSize=&quot;2147483647&quot; /&gt;
        &lt;/binding&gt;
      &lt;/customBinding&gt;
</pre>
<p>I made several other attempts with no luck. I then tried self hosting the service and that did work. Then I was certain that it was something with the web hosting. Tried IIS6, IIS7, etc&#8230; Same exception all the time.</p>
<p>To make a long story short, at the end, while reducing my web.config to the bare bones, I found the culprit: it was the name of the service in the service configuration.</p>
<p>Instead of writing:</p>
<p>
<pre class="brush:xml">
      &lt;service behaviorConfiguration=&quot;serviceBehavior&quot;
                  name=&quot;FileTransferService&quot;&gt;
</pre>
</p>
<p>I had written by mistake:</p>
<p>
<pre class="brush:xml">
      &lt;service behaviorConfiguration=&quot;serviceBehavior&quot;
                  name=&quot;FileTransfer&quot;&gt;
</pre>
</p>
<p>Talk about meaningful error messages!</p>
<p>See more at <a href="http://stefanoricciardi.com/2009/10/02/file-transfer-with-wcf-part-ii/">File Transfer with WCF II</a></p>
<p><!-- AddThis Button BEGIN --></p>
<p><a href="http://www.addthis.com/bookmark.php?v=250"><img style="border:0;" src="http://s7.addthis.com/static/btn/lg-share-en.gif" alt="Bookmark and Share" width="125" height="16" /></a></p>
<p><!-- AddThis Button END --></p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e6ceeafa-fe5c-4bd6-a48d-325da43e5abc" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/Programming">Programming</a>, <a rel="tag" href="http://technorati.com/tags/C%23">C#</a>, <a rel="tag" href="http://technorati.com/tags/wcf">WCF</a>, <a rel="tag" href="http://technorati.com/tags/soa">SOA</a></div>

]]></content:encoded>
			<wfw:commentRss>http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/feed/</wfw:commentRss>
		<slash:comments>94</slash:comments>
		</item>
	</channel>
</rss>

