<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>C# - Tag - Sean Feldman's Blog</title><link>https://seanfeldman.com/tags/c%23/</link><description>C# - Tag - Sean Feldman's Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 03 Nov 2024 20:49:36 +0000</lastBuildDate><atom:link href="https://seanfeldman.com/tags/c%23/" rel="self" type="application/rss+xml"/><item><title>DateTime to String with Custom Formatting</title><link>https://seanfeldman.com/posts/datetime-to-string-with-custom-formatting/</link><pubDate>Sun, 03 Nov 2024 20:49:36 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/datetime-to-string-with-custom-formatting/</guid><description><![CDATA[<p>When formatting <code>DateTime</code> to a string, the format specifier provides access to the parts of the date and time we want to express as a string. E.g.</p>
<div class="code-block highlight is-open show-line-numbers  tw-group tw-my-2">
  <div class="
    
    tw-flex 
    tw-flex-row
    tw-flex-1 
    tw-justify-between 
    tw-w-full tw-bg-bgColor-secondary
    ">      
    <button 
      class="
        code-block-button
        tw-mx-2 
        tw-flex
        tw-flex-row
        tw-flex-1"
      aria-hidden="true">
          <div class="group-[.is-open]:tw-rotate-90 tw-transition-[transform] tw-duration-500 tw-ease-in-out print:!tw-hidden tw-w-min tw-h-min tw-my-1 tw-mx-1"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"/></svg></div>
          <p class="tw-select-none !tw-my-1">csharp</p>]]></description></item><item><title>Fixing NServiceBus default databus serializer in .NET 6</title><link>https://seanfeldman.com/posts/fixing-nservicebus-default-databus-serializer-in-net-6/</link><pubDate>Fri, 06 May 2022 17:31:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/fixing-nservicebus-default-databus-serializer-in-net-6/</guid><description><![CDATA[<p>Upgrading to .NET 6, updating all the packages, boosters turned on, launching testing.</p>
<p>Houston, we&rsquo;ve got a problem.</p>
<blockquote>
  <p>System.NotSupportedException:  BinaryFormatter serialization and
deserialization are disabled within this application. See
<a href="https://aka.ms/binaryformatter" target="_blank" rel="noopener noreferrer">https://aka.ms/binaryformatter</a> for more information.</p>
</blockquote><p>Ouch! What just happened? There were no warnings, no obsolete messages, nothing on to the autopsy.</p>
<p>NServiceBus has a <a href="https://docs.particular.net/nservicebus/messaging/databus/" target="_blank" rel="noopener noreferrer">data bus</a> (or a &lsquo;databus&rsquo;) feature. The feature implements the <a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/claim-check" target="_blank" rel="noopener noreferrer">Claim Check pattern</a> to allow messages to surpass the imposed maximum message size by the underlying messaging technology. The feature serializes the data internally, and the default <code>DefaultDataBusSerializer</code> uses <code>BinaryFormatter</code>. Nothing new; it has been <a href="https://github.com/Particular/NServiceBus/blame/a510c214806540d920de10ed81b50f191129fbed/src/databus/NServiceBus.Databus/DefaultDatabusSerializer.cs#L6" target="_blank" rel="noopener noreferrer">used for years</a>. Unfortunately, with .NET 5, <code>BinaryFormatter</code> was deprecated due to a <a href="https://docs.microsoft.com/en-ca/dotnet/standard/serialization/binaryformatter-security-guide" target="_blank" rel="noopener noreferrer">security risk</a> it poses. And while you could skip .NET 5 and live with .NET Core 3.1, .NET 6 is breathing down the neck, and an upgrade is imminent.</p>]]></description></item><item><title>Content Disposition Bug in HttpResponseMessage.Conent.Headers</title><link>https://seanfeldman.com/posts/content-disposition-bug-in-httpresponsemessage-conent-headers/</link><pubDate>Wed, 16 Apr 2014 21:24:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/content-disposition-bug-in-httpresponsemessage-conent-headers/</guid><description><![CDATA[<p>If you’re running into a <a href="https://connect.microsoft.com/VisualStudio/feedback/details/813392/httpresponsemessage-content-headers-contentdisposition-is-null" target="_blank" rel="noopener noreferrer">bug</a> like I did, there are good and bad news. Good – <a href="https://connect.microsoft.com/VisualStudio/feedback/details/813392/httpresponsemessage-content-headers-contentdisposition-is-null" target="_blank" rel="noopener noreferrer">Microsoft acknowledged</a> this bug and will fix it, bad – it will happen in the next release (not sure then). In MS favour I have to admit that they turn things really quick, so who knows, it might be already in the release notes of the next release :)</p>]]></description></item><item><title>Moq with NameValueCollection</title><link>https://seanfeldman.com/posts/moq-with-indexer/</link><pubDate>Wed, 11 Jan 2012 21:49:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/moq-with-indexer/</guid><description><![CDATA[<p>Mocking HttpRequest and HttpResponse is not complicated with HttpRequestBase and HttpResponseBase introduced in ASP.NET. I had to mock ServerVariables property of HttpRequest and run into an issue – I was accessing an indexer property and didn’t know how to do this. Gladly, ran into a post that mentioned that indexer is actually utilizing a virtual Get() method. Reflector has confirmed that.</p>
<p><a href="https://aspblogs.blob.core.windows.net/media/sfeldman/Media/image_305993F1.png" target="_blank" rel="noopener noreferrer"><figure><img  loading="lazy" src='https://aspblogs.blob.core.windows.net/media/sfeldman/Media/image_thumb_2427F3FD.png'   alt="image"  ><figcaption class="image-caption">image</figcaption>
</figure></a></p>
<p><a href="https://aspblogs.blob.core.windows.net/media/sfeldman/Media/image_5F0F26C6.png" target="_blank" rel="noopener noreferrer"><figure><img  loading="lazy" src='https://aspblogs.blob.core.windows.net/media/sfeldman/Media/image_thumb_4FF847EC.png'   alt="image"  ><figcaption class="image-caption">image</figcaption>
</figure></a></p>
<p>Great! Now indexer can be mocked.</p>]]></description></item><item><title>Writing a Custom ConfigurationSection</title><link>https://seanfeldman.com/posts/writing-a-custom-configurationsection/</link><pubDate>Mon, 09 Jan 2012 05:13:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/writing-a-custom-configurationsection/</guid><description>&lt;p>Writing a Custom ConfigurationSection is not a big task, but can be very challenging if you forget some details. I loved &lt;a href="http://www.abhisheksur.com/2011/09/writing-custom-configurationsection-to.html" target="_blank">this post&lt;/a> as it reminded me that you have to have both the Section tag and the collection within it tag… dah!&lt;/p></description></item><item><title>Stop Asserting Arguments by Name</title><link>https://seanfeldman.com/posts/stop-asserting-arguments-by-name/</link><pubDate>Fri, 28 Oct 2011 02:53:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/stop-asserting-arguments-by-name/</guid><description><![CDATA[<p>Asserting arguments is essentially a healthy practice. What I don’t like, is the fact that since day one <em><a href="http://msdn.microsoft.com/en-us/library/aa310842%28v=VS.71%29.aspx" target="_blank" rel="noopener noreferrer">ArgumentNullException</a></em> used a string for argument name, and now with .NET 4.5 almost knocking on the door, there’s still only string option.</p>
<div class="code-block highlight is-open show-line-numbers  tw-group tw-my-2">
  <div class="
    
    tw-flex 
    tw-flex-row
    tw-flex-1 
    tw-justify-between 
    tw-w-full tw-bg-bgColor-secondary
    ">      
    <button 
      class="
        code-block-button
        tw-mx-2 
        tw-flex
        tw-flex-row
        tw-flex-1"
      aria-hidden="true">
          <div class="group-[.is-open]:tw-rotate-90 tw-transition-[transform] tw-duration-500 tw-ease-in-out print:!tw-hidden tw-w-min tw-h-min tw-my-1 tw-mx-1"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"/></svg></div>
          <p class="tw-select-none !tw-my-1">text</p>]]></description></item><item><title>Curing Singletonitis</title><link>https://seanfeldman.com/posts/curing-singletonitis/</link><pubDate>Fri, 16 Sep 2011 03:56:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/curing-singletonitis/</guid><description><![CDATA[<p>A few years ago I have blogged about <a href="http://weblogs.asp.net/sfeldman/archive/2008/10/22/singletonitis.aspx" target="_blank" rel="noopener noreferrer">Singletonitis</a>. Another place, different people, yet the same problem appears again – singletonitis. The issues I have outlined back then where</p>
<ol>
<li>
<p>Testing</p>
</li>
<li>
<p>Difficulty to replace implementation</p>
</li>
<li>
<p>Requirement to track all the location where Singleton is utilized once we need to replace it.</p>
</li>
</ol>
<p>I’d like to look into these again and re-address this ‘disease’ so to speak.</p>
<h2 id="testing-for-design-verification" class="headerLink">
    <a href="#testing-for-design-verification" class="header-mark"></a>Testing (for Design Verification)</h2><p>Testing is important. Maybe back then (2008) some would argue, but not today. But what this testing is for? To confirm that implementation adheres to the desired design. And, as a side effect, to have a safety net. The why not to test singleton implementation and be done? The headache with singleton is not about singleton itself, but other code/components that rely on it. Yes, these days you could use tools like Moles and Typemock, but why would you if you could DESIGN your code properly to begin with?</p>]]></description></item><item><title>Creating collection with no code (almost)</title><link>https://seanfeldman.com/posts/creating-collection-with-no-code-almost/</link><pubDate>Sat, 15 Jan 2011 05:56:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/creating-collection-with-no-code-almost/</guid><description><![CDATA[<p>When doing testing, I tend to create an object mother for the items generated multiple times for specifications. Quite often these objects need to be a part of a collection. A neat way to do so is to leverage .NET params mechanism:</p>
<div class="code-block highlight is-open show-line-numbers  tw-group tw-my-2">
  <div class="
    
    tw-flex 
    tw-flex-row
    tw-flex-1 
    tw-justify-between 
    tw-w-full tw-bg-bgColor-secondary
    ">      
    <button 
      class="
        code-block-button
        tw-mx-2 
        tw-flex
        tw-flex-row
        tw-flex-1"
      aria-hidden="true">
          <div class="group-[.is-open]:tw-rotate-90 tw-transition-[transform] tw-duration-500 tw-ease-in-out print:!tw-hidden tw-w-min tw-h-min tw-my-1 tw-mx-1"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"/></svg></div>
          <p class="tw-select-none !tw-my-1">text</p>]]></description></item><item><title>MbUnit–Hidden Gem</title><link>https://seanfeldman.com/posts/mbunit-hidden-gem/</link><pubDate>Sat, 13 Nov 2010 05:16:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/mbunit-hidden-gem/</guid><description><![CDATA[<p>Row tests are fun. You can simulate various inputs with expected outputs and execute them all with a single observation (test). The problem for myself was always the fact that when this was done, the only way to accomplish it was to merge the logical <em>because</em> which operates on system under test behaviour and the observation itself (assertion). This leads to a slightly less readable code IMO.</p>
<div class="code-block highlight is-open show-line-numbers  tw-group tw-my-2">
  <div class="
    
    tw-flex 
    tw-flex-row
    tw-flex-1 
    tw-justify-between 
    tw-w-full tw-bg-bgColor-secondary
    ">      
    <button 
      class="
        code-block-button
        tw-mx-2 
        tw-flex
        tw-flex-row
        tw-flex-1"
      aria-hidden="true">
          <div class="group-[.is-open]:tw-rotate-90 tw-transition-[transform] tw-duration-500 tw-ease-in-out print:!tw-hidden tw-w-min tw-h-min tw-my-1 tw-mx-1"><svg class="icon"
    xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M285.476 272.971L91.132 467.314c-9.373 9.373-24.569 9.373-33.941 0l-22.667-22.667c-9.357-9.357-9.375-24.522-.04-33.901L188.505 256 34.484 101.255c-9.335-9.379-9.317-24.544.04-33.901l22.667-22.667c9.373-9.373 24.569-9.373 33.941 0L285.475 239.03c9.373 9.372 9.373 24.568.001 33.941z"/></svg></div>
          <p class="tw-select-none !tw-my-1">text</p>]]></description></item><item><title>Creating BizTalk Message in Custom .NET Component</title><link>https://seanfeldman.com/posts/creating-biztalk-message-in-custom-net-component/</link><pubDate>Thu, 30 Sep 2010 04:26:00 +0000</pubDate><author><name>Sean Feldman</name></author><guid>https://seanfeldman.com/posts/creating-biztalk-message-in-custom-net-component/</guid><description><![CDATA[<p>In my previous blog about <a href="http://weblogs.asp.net/sfeldman/archive/2010/09/21/consuming-wcf-service-from-biztalk.aspx" target="_blank" rel="noopener noreferrer">consuming WCF service from BizTalk orchestration</a> I wrote about how we’d invoke a service based on a document coming in and mapping it into the service request schema.</p>
<p>This time I had the input for the service coming from a context, and not necessarily from the document itself, and needed to be able to perform same service invocation, yet constructing request document dynamically with-in the orchestration. To simplify the process, I still pass in a document, and distinguish a field with the value I want to pass into the service. This way I don’t actually have to waste time on property promotion. The key here is to invoke a custom .NET component (GAC-ed singleton) to construct the message with the value(s) coming from the original message context.</p>]]></description></item></channel></rss>