Web Service Reference in Visual Studio 2010

If you’ve used Visual Studio 2008 or 2010, you’ll notice that Web Reference is no longer there (like in VS 2005 and previous), instead, you see Service Reference – actually, it’s STILL there, just hidden!

So, what’s the difference?  Well, according to this post:

Add Web Reference is the old-style, deprecated ASP.NET webservices (ASMX) technology (using only the XmlSerializer for your stuff) – if you do this, you get an ASMX client for an ASMX web service. You can do this in just about any project (Web App, Web Site, Console App, Winforms – you name it).

Add Service Reference is the new way of doing it, adding a WCF service reference, which gives you a much more advanced, much more flexible service model than just plain old ASMX stuff.

So, how do you access it?  Simple — right-click on Service References in the Solution Explorer window and…

image

Click on “Advanced…”

image

Then click on “Add Web Reference…”

image

Voila!  You can now consume the web service like you did pre-Visual Studio 2008 era.  Happy coding!

Hash class using System.Security.Cryptography

Adding for archival purposes…

<span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.Web;
<span style="color: blue">using </span>System.Security.Cryptography;

<span style="color: blue">namespace </span>Classes
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Hash
    </span>{
        <span style="color: blue">public </span>Hash()
        { }

        <span style="color: blue">#region </span>Hash Choices
        <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">The wanted hash function.</span><span style="color: gray">&lt;/summary&gt;
        </span><span style="color: blue">public enum </span><span style="color: #2b91af">HashType </span>: <span style="color: blue">int
        </span>{
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">MD5 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>MD5,
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">SHA1 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>SHA1,
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">SHA256 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>SHA256,
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">SHA384 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>SHA384,
            <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">SHA512 Hashing</span><span style="color: gray">&lt;/summary&gt;
            </span>SHA512
        } <span style="color: green">/* HashType */
        </span><span style="color: blue">#endregion

        #region </span>Public Methods
        <span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">Generates the hash of a text.</span><span style="color: gray">&lt;/summary&gt;
        /// &lt;param name=&quot;strPlain&quot;&gt;</span><span style="color: green">The text of which to generate a hash of.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;param name=&quot;hshType&quot;&gt;</span><span style="color: green">The hash function to use.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;returns&gt;</span><span style="color: green">The hash as a hexadecimal string.</span><span style="color: gray">&lt;/returns&gt;
        </span><span style="color: blue">public static string </span>GetHash(<span style="color: blue">string </span>strPlain, <span style="color: #2b91af">HashType </span>hshType)
        {
            <span style="color: blue">string </span>strRet;
            <span style="color: blue">switch </span>(hshType)
            {
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.MD5: 
                    strRet = GetMD5(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.SHA1: 
                    strRet = GetSHA1(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.SHA256: 
                    strRet = GetSHA256(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.SHA384: 
                    strRet = GetSHA384(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">case </span><span style="color: #2b91af">HashType</span>.SHA512: 
                    strRet = GetSHA512(strPlain); 
                    <span style="color: blue">break</span>;
                <span style="color: blue">default</span>: 
                    strRet = <span style="color: #a31515">&quot;Invalid HashType&quot;</span>; 
                    <span style="color: blue">break</span>;
            }
            <span style="color: blue">return </span>strRet;
        } <span style="color: green">/* GetHash */

        </span><span style="color: gray">/// &lt;summary&gt;</span><span style="color: green">Checks a text with a hash.</span><span style="color: gray">&lt;/summary&gt;
        /// &lt;param name=&quot;strOriginal&quot;&gt;</span><span style="color: green">The text to compare the hash against.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;param name=&quot;strHash&quot;&gt;</span><span style="color: green">The hash to compare against.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;param name=&quot;hshType&quot;&gt;</span><span style="color: green">The type of hash.</span><span style="color: gray">&lt;/param&gt;
        /// &lt;returns&gt;</span><span style="color: green">True if the hash validates, false otherwise.</span><span style="color: gray">&lt;/returns&gt;
        </span><span style="color: blue">public static bool </span>CheckHash(<span style="color: blue">string </span>strOriginal, <span style="color: blue">string </span>strHash, <span style="color: #2b91af">HashType </span>hshType)
        {
            <span style="color: blue">string </span>strOrigHash = GetHash(strOriginal, hshType);
            <span style="color: blue">return </span>(strOrigHash == strHash);
        } <span style="color: green">/* CheckHash */
        </span><span style="color: blue">#endregion

        #region </span>Hashers
        <span style="color: blue">private static string </span>GetMD5(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">MD5 </span>md5 = <span style="color: blue">new </span><span style="color: #2b91af">MD5CryptoServiceProvider</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = md5.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetMD5 */

        </span><span style="color: blue">private static string </span>GetSHA1(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">SHA1Managed </span>SHhash = <span style="color: blue">new </span><span style="color: #2b91af">SHA1Managed</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = SHhash.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetSHA1 */

        </span><span style="color: blue">private static string </span>GetSHA256(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">SHA256Managed </span>SHhash = <span style="color: blue">new </span><span style="color: #2b91af">SHA256Managed</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = SHhash.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetSHA256 */

        </span><span style="color: blue">private static string </span>GetSHA384(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">SHA384Managed </span>SHhash = <span style="color: blue">new </span><span style="color: #2b91af">SHA384Managed</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = SHhash.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetSHA384 */

        </span><span style="color: blue">private static string </span>GetSHA512(<span style="color: blue">string </span>strPlain)
        {
            <span style="color: #2b91af">UnicodeEncoding </span>UE = <span style="color: blue">new </span><span style="color: #2b91af">UnicodeEncoding</span>();
            <span style="color: blue">byte</span>[] HashValue, MessageBytes = UE.GetBytes(strPlain);
            <span style="color: #2b91af">SHA512Managed </span>SHhash = <span style="color: blue">new </span><span style="color: #2b91af">SHA512Managed</span>();
            <span style="color: blue">string </span>strHex = <span style="color: #a31515">&quot;&quot;</span>;

            HashValue = SHhash.ComputeHash(MessageBytes);
            <span style="color: blue">foreach </span>(<span style="color: blue">byte </span>b <span style="color: blue">in </span>HashValue)
            {
                strHex += <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0:x2}&quot;</span>, b);
            }
            <span style="color: blue">return </span>strHex;
        } <span style="color: green">/* GetSHA512 */
        </span><span style="color: blue">#endregion
    </span>}
}

Fix to "The handle is invalid" error when ASP.NET writes to the Eventlogs

Have you ever come across the error below with one of your ASP.NET web apps that is trying to write to the Eventlogs? If so, read on…

image

By default the ASPNET user cannot access the existing eventlogs categories. To resolve this, you must set the permissions in the Eventlog key in the registry:

  1. Launch RegEdit.
  2. Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\.
  3. From the menu, select Edit->Permissions.
  4. Click the Add button and write ASPNET (if ASP.NET is running under a different User ID, use that ID instead).
  5. Click OK.
  6. Select the newly added user from the list (ASP.NET Machine User by default).
  7. Click on Full Control in the Allow column. 8. Click OK.

More info here.

Most Common ASP.NET Support issues

Below is a summary of the “two top things that cause trouble in production ASP.NET web sites,” per Scott Hanselman, which he obtained from deep within Microsoft Developer Support.  Go here to read the complete article.

#1 Issue – Configuration

Seems the #1 issue in support for problems with ASP.NET 2.x and 3.x is configuration. 

Symptoms Notes
OOM
Performance
High memory
Hangs
Deadlocks
There are more debug=true cases than there should be.

 

#2 Issue – Problems with an External (non-ASP.NET) Root Cause

Sometimes when you’re having trouble with an ASP.NET site, the problem turns out to not be ASP.NET itself.

Issue Product Description Symptoms Notes
Anti-virus software All Anti-virus software is installed onto Servers and causes all kinds of problems.
  • Application restarting
  • Slow performance
  • Session variable are null
  • Cannot install hotfix
  • Intermittent time outs
  • High memory
  • Session lost
  • IDE Hangs
  • Deadlocks

This consists of all AV software reported by our customers. All cases do not report the AV software that is being used so the manufacturer is not always known.

KB821438, KB248013, KB295375, KB817442

3rd party Vendors All This is a category of cases where the failure was due to a 3rd party manufacturer.
  • Crash
  • 100% CPU
  • High memory
  • Framework errors
  • Hang
The top culprits are 3rd party database systems, and 3rd party internet access management systems
Microsoft component All Microsoft software
  • Intermittent time outs
  • High memory
  • Deadlocks
  • 100% CPU
    Crash
Design issues that cause performance issues like sprocs, deadlocks, etc. Profile your applications and the database! (Pro tip: select * from authors doesn’t scale.) Pair up DBAs and programmers and profile from end to end