<?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>dotnet thoughts &#187; .Net 3.0 / 3.5</title>
	<atom:link href="http://www.dotnetthoughts.net/category/net-3-0-3-5/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:18:00 +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>The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail</title>
		<link>http://www.dotnetthoughts.net/2012/02/01/the-application-has-failed-to-start-because-its-side-by-side-configuration-is-incorrect-please-see-the-application-event-log-or-use-the-command-line-sxstrace-exe-tool-for-more-detail/</link>
		<comments>http://www.dotnetthoughts.net/2012/02/01/the-application-has-failed-to-start-because-its-side-by-side-configuration-is-incorrect-please-see-the-application-event-log-or-use-the-command-line-sxstrace-exe-tool-for-more-detail/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 03:18:00 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[sxstrace]]></category>
		<category><![CDATA[sxstrace.exe]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2332</guid>
		<description><![CDATA[Today I got a comment from Vitor, Vitor is getting an exception like this while starting application. The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe &#8230; <a href="http://www.dotnetthoughts.net/2012/02/01/the-application-has-failed-to-start-because-its-side-by-side-configuration-is-incorrect-please-see-the-application-event-log-or-use-the-command-line-sxstrace-exe-tool-for-more-detail/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I got a <a href="http://www.dotnetthoughts.net/2010/12/22/net-framework-initialization-error-%E2%80%93-unable-to-find-a-version-of-the-runtime-to-run-this-application/#comment-699">comment from Vitor</a>, Vitor is getting an exception like this while starting application.</p>
<blockquote><p>The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.</p></blockquote>
<p>So I thought of work around this issue. To reproduce the issue, I tampered the exe.config file of one of my application, by removing &lt;configuration&gt; tag from the file. And it started throwing the exception.</p>
<div id="attachment_2336" class="wp-caption aligncenter" style="width: 310px"><img src="http://www.dotnetthoughts.net/wp-content/uploads/2012/02/side_by_side-300x96.jpg" alt="The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail." title="The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail." width="300" height="96" class="size-medium wp-image-2336" /><p class="wp-caption-text">The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.</p></div>
<p>I verified the same with EventViewer, and I found a message like this, under Windows Logs > Application </p>
<blockquote><p>Activation context generation failed for &#8220;C:\CaptureItPlus\Binaries\CaptureItPlus.exe&#8221;. Error in manifest or policy file &#8220;C:\CaptureItPlus\Binaries\CaptureItPlus.exe.Config&#8221; on line 1. Invalid Xml syntax. </p></blockquote>
<p>The error message is pretty self explanatory. If you are not able to access the event viewer, the other option is using sxstrace.exe tool. You can enable run the sxstrace in trace mode, you can do this by following command.</p>
<pre class="brush: plain; title: ; notranslate">
sxstrace.exe Trace -logfile:C:\captureittrace.log
</pre>
<p>And execute the application. After getting exception, stop the tracing and you can parse the log file using following command.</p>
<pre class="brush: plain; title: ; notranslate">
sxstrace.exe Parse -logfile:C:\captureittrace.log -outfile:C:\captureittrace.txt
</pre>
<p>It is required because .log is a binary file. Now open the .txt file, you can see similar message,</p>
<blockquote><p>INFO: Parsing Application Config File C:\CaptureItPlus\Binaries\CaptureItPlus.exe.Config.<br />
ERROR: Line 1: XML Syntax error.<br />
ERROR: Activation Context generation failed.<br />
End Activation Context Generation.</p></blockquote>
<p>Now open the .exe.config and add the missing <configuration>, now double click on the application, this issue will be resolved. <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </configuration></p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2012/01/11/how-to-use-net-assembly-in-vbscript/" title="Permanent link to How to use .Net assembly in VBScript">How to use .Net assembly in VBScript</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/11/02/debugging-nunit-tests-in-visual-studio-2010/" title="Permanent link to Debugging NUnit Tests in Visual Studio 2010">Debugging NUnit Tests in Visual Studio 2010</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/06/30/how-to-use-taskdialog-api-in-c/" title="Permanent link to How to use TaskDialog API in C#">How to use TaskDialog API in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/08/03/windows-7-sdk-installation-success-or-error-status-1603/" title="Permanent link to Windows 7 SDK Installation success or error status: 1603">Windows 7 SDK Installation success or error status: 1603</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/01/18/how-to-unit-test-asynchronous-callbacks/" title="Permanent link to How to unit test asynchronous callbacks">How to unit test asynchronous callbacks</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2012/02/01/the-application-has-failed-to-start-because-its-side-by-side-configuration-is-incorrect-please-see-the-application-event-log-or-use-the-command-line-sxstrace-exe-tool-for-more-detail/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to use .Net assembly in VBScript</title>
		<link>http://www.dotnetthoughts.net/2012/01/11/how-to-use-net-assembly-in-vbscript/</link>
		<comments>http://www.dotnetthoughts.net/2012/01/11/how-to-use-net-assembly-in-vbscript/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 19:31:45 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[RegAsm]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2243</guid>
		<description><![CDATA[Few days before I got a requirement to use a .Net assembly in VBScript, but it was not working. But today I got the solution. It was related to my 64bit OS. In this post I am discussing how to &#8230; <a href="http://www.dotnetthoughts.net/2012/01/11/how-to-use-net-assembly-in-vbscript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Few days before I got a requirement to use a .Net assembly in VBScript, but it was not working. But today I got the solution. <strong>It was related to my 64bit OS</strong>. <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  In this post I am discussing how to create and use a .net assembly in VBScript or Javascript.</p>
<p style="text-align: justify;">First we need to create a class library. Set the ComVisible attribute to true in the AssemblyInfo.cs manually.</p>
<pre class="brush: csharp; title: ; notranslate">
[assembly: ComVisible(true)]
</pre>
<p style="text-align: justify;">Or you can do it using Properties &gt; Application Tab and select Assembly Information, from the Assembly Information dialog, Check the Make assembly COM-Visible check box.</p>
<div id="attachment_2249" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-2249" title="Make Assembly COM Visible option from Assembly Information dialog" src="http://www.dotnetthoughts.net/wp-content/uploads/2012/01/make_com_visible-300x140.jpg" alt="Make Assembly COM Visible option from Assembly Information dialog" width="300" height="140" /><p class="wp-caption-text">Make Assembly COM Visible option from Assembly Information dialog</p></div>
<p style="text-align: justify;">Build the assembly. Now we need register the assembly. We can do it using RegAsm.exe. <strong>If your OS in 64 bit, then you need to use Regasm command from Framework64 folder(Normally it will be C:\Windows\Microsoft.NET\Framework64\ folder).</strong> For running RegAsm command it is recommended to sign the assembly. You can Sign the assembly Properties &gt; Signing Tab.</p>
<div id="attachment_2250" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-2250" title="Sign Assembly option" src="http://www.dotnetthoughts.net/wp-content/uploads/2012/01/sign_assembly-300x97.jpg" alt="Sign Assembly option" width="300" height="97" /><p class="wp-caption-text">Sign Assembly option</p></div>
<p style="text-align: justify;">As RegAsm command add / update information to Windows Registry, you need to run the RegAsm command as Administrator, otherwise it will not work.</p>
<p>You can register the assembly using following command, like this</p>
<pre class="brush: plain; title: ; notranslate">
regasm /codebase assemblyname.dll
</pre>
<p>You can unregister the assembly using RegAsm command, like this</p>
<pre class="brush: plain; title: ; notranslate">
regasm /u assemblyname.dll
</pre>
<p>That&#8217;s it. Now you can create the object of the .net assembly in VBScript. Like this, it will add 10 and 20 using C# Add method and returns the value.</p>
<pre class="brush: vb; title: ; notranslate">
Dim calcLib
Set calcLib = CreateObject(&quot;CalcLib.Math&quot;)
Dim result
result = calcLib.Add (10 , 20)
msgbox result
</pre>
<p>And here is the C# Class, which we are invoked from VBScript.</p>
<pre class="brush: csharp; title: ; notranslate">
namespace CalcLib
{
    public class Math
    {
        public int Add(int number1, int number2)
        {
            return number1 + number2;
        }
    }
}
</pre>
<p>Happy Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2009/01/30/finding-types-in-a-running-program/" title="Permanent link to Finding types in a Running Program">Finding types in a Running Program</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2012/02/01/the-application-has-failed-to-start-because-its-side-by-side-configuration-is-incorrect-please-see-the-application-event-log-or-use-the-command-line-sxstrace-exe-tool-for-more-detail/" title="Permanent link to The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail">The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/09/21/how-measure-get-code-coverage-by-partcover-with-mstest/" title="Permanent link to How measure get code coverage by PartCover with MSTest">How measure get code coverage by PartCover with MSTest</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/11/12/how-redirect-output-from-a-console-application/" title="Permanent link to How redirect output from a console application">How redirect output from a console application</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/12/22/net-framework-initialization-error-%e2%80%93-unable-to-find-a-version-of-the-runtime-to-run-this-application/" title="Permanent link to .Net Framework Initialization Error – Unable to find a version of the runtime to run this application">.Net Framework Initialization Error – Unable to find a version of the runtime to run this application</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2012/01/11/how-to-use-net-assembly-in-vbscript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to compress JPEG image using C#</title>
		<link>http://www.dotnetthoughts.net/2012/01/07/how-to-compress-jpeg-image-using-c/</link>
		<comments>http://www.dotnetthoughts.net/2012/01/07/how-to-compress-jpeg-image-using-c/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 14:32:37 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Compress]]></category>
		<category><![CDATA[Image Compression]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2227</guid>
		<description><![CDATA[Here is a C# snippet which helps to compress JPEG image. Also displays how much size got reduced. Related content: Writing File with Non Cache mode in C# Upload multiple files using Silverlight Raven DB &#8211; Introduction How to compile &#8230; <a href="http://www.dotnetthoughts.net/2012/01/07/how-to-compress-jpeg-image-using-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here is a C# snippet which helps to compress JPEG image. Also displays how much size got reduced.</p>
<pre class="brush: csharp; title: ; notranslate">
public static void CompressJpeg(string path, int quality)
{
    if (quality &lt; 0 || quality &gt; 100)
    {
        throw new
            ArgumentOutOfRangeException(&quot;Quality must be between 0 and 100.&quot;);
    }
    //Creating temp. file and
    string tempFile = Path.GetTempFileName();
    File.Copy(path, tempFile, true);
    using (var image = Image.FromFile(tempFile))
    {
        // Encoder parameter for image quality
        var qualityParam =
            new EncoderParameter(Encoder.Quality, quality);
        // Jpeg image codec
        var jpegCodec = ImageCodecInfo.GetImageEncoders()
            .Where(imageCodecInfo =&gt; imageCodecInfo.MimeType == &quot;image/jpeg&quot;)
            .FirstOrDefault();
        var encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;
        //Save the compressed image.
        image.Save(path, jpegCodec, encoderParams);
        //Getting the file image sizes.
        var prevImageSize = new FileInfo(tempFile).Length;
        var nextImageSize = new FileInfo(path).Length;
        Console.WriteLine(&quot;Image compressed. Size saved :{0} bytes&quot;,
            prevImageSize - nextImageSize);
    }
    //Removing the temp. file.
    File.Delete(tempFile);
}
</pre>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/01/19/writing_file_with_non_cache_mode_in_c/" title="Permanent link to Writing File with Non Cache mode in C#">Writing File with Non Cache mode in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/07/17/upload-multiple-files-using-silverlight/" title="Permanent link to Upload multiple files using Silverlight">Upload multiple files using Silverlight</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/11/25/raven-db-introduction/" title="Permanent link to Raven DB &#8211; Introduction">Raven DB &#8211; Introduction</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/01/09/how-to-compile-c-code-snippet-in-runtime/" title="Permanent link to How to compile C# code snippet in runtime">How to compile C# code snippet in runtime</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/11/03/argumentexception-this-row-already-belongs-to-another-table/" title="Permanent link to ArgumentException &#8211; This row already belongs to another table">ArgumentException &#8211; This row already belongs to another table</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2012/01/07/how-to-compress-jpeg-image-using-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to configure a WCF service to use Port Sharing</title>
		<link>http://www.dotnetthoughts.net/2011/12/26/how-to-configure-a-wcf-service-to-use-port-sharing/</link>
		<comments>http://www.dotnetthoughts.net/2011/12/26/how-to-configure-a-wcf-service-to-use-port-sharing/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 02:46:15 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Port Sharing]]></category>
		<category><![CDATA[WCF Port sharing]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2162</guid>
		<description><![CDATA[Windows Communication Foundation (WCF) provides a new TCP-based network protocol (net.tcp://) for high-performance communication. WCF also introduces a new system component, the Net.TCP Port Sharing Service that enables net.tcp ports to be shared across multiple user processes. Yesterday I got &#8230; <a href="http://www.dotnetthoughts.net/2011/12/26/how-to-configure-a-wcf-service-to-use-port-sharing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Windows Communication Foundation (WCF) provides a new TCP-based network protocol (net.tcp://) for high-performance communication. WCF also introduces a new system component, the Net.TCP Port Sharing Service that enables net.tcp ports to be shared across multiple user processes. Yesterday I got a chance to work on the same, I thought it was complex. But it is very simple in WCF, only we need to modify the <strong>PortSharingEnabled</strong> property of the NetTcpBinding. Like the following</p>
<pre class="brush: csharp; title: ; notranslate">
string baseAddress = &quot;net.tcp://localhost:8080/MyService1&quot;;
Uri[] addresses = { new Uri(baseAddress) };
using (var serviceHost = new ServiceHost(typeof(Service), addresses))
{
    var nettcpbinding = new NetTcpBinding();
    nettcpbinding.PortSharingEnabled = true;
    serviceHost.AddServiceEndpoint(typeof(IService),
        nettcpbinding, &quot;IService&quot;);

    serviceHost.Open();
    Console.ReadKey(true);
    serviceHost.Close();
}
</pre>
<p style="text-align: justify;">Also even if set the PortSharingEnabled to true, we also require to start the <strong>Net.Tcp port sharing service</strong> from the control panel. By default it will be disabled. If the service not started, and your tried to listen two services in same port number, you may get an exception like this <em>AddressAlreadyInUseException &#8211; There is already a listener on IP endpoint 0.0.0.0:8080. This could happen if there is another application already listening on this endpoint or if you have multiple service endpoints in your service host with the same IP endpoint but with incompatible binding configurations</em>.</p>
<div id="attachment_2168" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.dotnetthoughts.net/2011/12/26/how-to-configure-a-wcf-service-to-use-port-sharing/dotnetthoughts_services/" rel="attachment wp-att-2168"><img class="size-medium wp-image-2168" title="Net.Tcp port sharing service in Services console." src="http://www.dotnetthoughts.net/wp-content/uploads/2011/12/dotnetthoughts_services-300x144.jpg" alt="Net.Tcp port sharing service in Services console." width="300" height="144" /></a><p class="wp-caption-text">Net.Tcp port sharing service in Services console.</p></div>
<p>You can also set the property via App.Config file also.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;bindings&gt;
    &lt;netTcpBinding&gt;
        &lt;binding portSharingEnabled=&quot;true&quot; /&gt;
    &lt;/netTcpBinding&gt;
&lt;/bindings&gt;
</pre>
<p>Now you are able to run two services with same port number, in this port number 8080.</p>
<p>Happy Programming <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2011/06/24/wcf-callbacks-%e2%80%93-a-quick-introduction/" title="Permanent link to WCF Callbacks – A quick introduction">WCF Callbacks – A quick introduction</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/03/20/how-to-create-restful-wcf-services/" title="Permanent link to How to create RESTful WCF services">How to create RESTful WCF services</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2012/01/22/how-to-add-mex-endpoints-programmatically/" title="Permanent link to How to add MEX endpoints programmatically">How to add MEX endpoints programmatically</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/" title="Permanent link to How to create a RSS feed using ASP.Net">How to create a RSS feed using ASP.Net</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/11/25/raven-db-introduction/" title="Permanent link to Raven DB &#8211; Introduction">Raven DB &#8211; Introduction</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/12/26/how-to-configure-a-wcf-service-to-use-port-sharing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to create a RSS feed using ASP.Net</title>
		<link>http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/</link>
		<comments>http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 19:36:18 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[.Net 4.0]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[ATOM]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[RSS Feed]]></category>
		<category><![CDATA[Syndication]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=2014</guid>
		<description><![CDATA[Another ASP.Net related post, this post helps to create a custom RSS feed using ASP.Net. There are many ways to create feed using ASP.Net, like using Xml Documents, String Builder and simple string concatenation. But none of them designed to &#8230; <a href="http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Another ASP.Net related post, this post helps to create a custom RSS feed using ASP.Net. There are many ways to create feed using ASP.Net, like using Xml Documents, String Builder and simple string concatenation. But none of them designed to create RSS feed, so it will be painful and complex. But Microsoft introduced Syndication namespace in .Net Framework 3.5, which helps to create RSS or ATOM feed easily.</p>
<p>Here is my sample Database design and which contains few of my blog posts.</p>
<div id="attachment_2015" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/Capture.jpg"><img class="size-medium wp-image-2015" title="Database Structure " src="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/Capture-300x109.jpg" alt="Database Structure " width="300" height="109" /></a><p class="wp-caption-text">Database Structure</p></div>
<p style="text-align: justify;">And here is the implementation using generic ashx handler, which helps to create RSS feeds. Following snippet will read all the values from the table and creating list of SyndicationItem using SqlDataReader.</p>
<pre class="brush: csharp; title: ; notranslate">
using (SqlCommand command = new SqlCommand(&quot;SELECT * FROM tblPosts&quot;, connection))
{
    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
    while (reader.Read())
    {
        feedList.Add(new SyndicationItem(
            reader[&quot;PostTitle&quot;].ToString(),
            reader[&quot;PostContent&quot;].ToString(),
            new Uri(string.Format(APP_URL,
                reader[&quot;PostId&quot;].ToString())),
            reader[&quot;PostId&quot;].ToString(),
            DateTimeOffset.Parse(reader[&quot;PostDate&quot;].ToString())));
    }
}
</pre>
<p style="text-align: justify;">And next snippet creating a SyndicationFeed which will help to create RSS using SyndicationItems. Based on the querystring, it is generating either ATOM or RSS feed.</p>
<pre class="brush: csharp; title: ; notranslate">
SyndicationFeed feeds =
    new SyndicationFeed(&quot;DOTNETTHOUGHTS&quot;,
        &quot;A DOTNET DEVELOPER'S TECHNICAL BLOG&quot;,
        new Uri(&quot;http://dotnetthoughts.net/feed&quot;), feedList);
feeds.Copyright =
    new TextSyndicationContent(&quot;Copyright (C) 2011 dotnetthoughts.net&quot;);
feeds.Authors.Add(
    new SyndicationPerson(&quot;anuraj@dotnetthoughts.net&quot;, &quot;Anuraj&quot;, &quot;dotnetthoughts.net&quot;));
feeds.Language = &quot;en-us&quot;;
string responseString;
using (var ms = new MemoryStream())
{
    using (XmlWriter xmlWriter = XmlWriter.Create(ms))
    {
        string rss = HttpContext.Current.Request.QueryString[&quot;rss&quot;];
        if (string.IsNullOrWhiteSpace(rss))
        {
            feeds.SaveAsAtom10(xmlWriter);
        }
        else
        {
            feeds.SaveAsRss20(xmlWriter);
        }
        xmlWriter.Flush();
        xmlWriter.Close();
    }
    responseString = System.Text.ASCIIEncoding.UTF8.GetString(ms.ToArray());
}

context.Response.ContentType = &quot;text/xml&quot;;
context.Response.Write(responseString);
context.Response.End();
</pre>
<p>And here is the screenshot of RSS feed in Internet Explorer.</p>
<div id="attachment_2016" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/Capture1.jpg"><img class="size-medium wp-image-2016" title="RSS Feed in Internet Explorer" src="http://www.dotnetthoughts.net/wp-content/uploads/2011/11/Capture1-300x175.jpg" alt="RSS Feed in Internet Explorer" width="300" height="175" /></a><p class="wp-caption-text">RSS Feed in Internet Explorer</p></div>
<p style="text-align: justify;">There are more useful tags which you can use to create the RSS feed, such as the category or an unique ID. You found more information on Creating RSS Feed in the <a href="http://feed1.w3.org/docs/rss2.html" target="_blank">RSS 2.0 specification page</a>.</p>
<p style="text-align: justify;">Happy Programming.</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/12/17/developing-a-simple-rss-reader-in-c-net/" title="Permanent link to Developing a simple RSS Reader in C#.Net">Developing a simple RSS Reader in C#.Net</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/03/20/how-to-create-restful-wcf-services/" title="Permanent link to How to create RESTful WCF services">How to create RESTful WCF services</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/12/26/how-to-configure-a-wcf-service-to-use-port-sharing/" title="Permanent link to How to configure a WCF service to use Port Sharing">How to configure a WCF service to use Port Sharing</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/11/25/raven-db-introduction/" title="Permanent link to Raven DB &#8211; Introduction">Raven DB &#8211; Introduction</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2011/11/18/how-to-create-a-rss-feed-using-asp-net/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

