Amazon.com Widgets ASP.Net

WilliaBlog.Net

I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Code Project Associate Logo
Go Daddy Deal of the Week: 30% off your order at GoDaddy.com! Offer expires 11/6/12

Recent comments

Archive

Authors

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.


Godaddy and HTML5 Video

Godaddy's shared hosting control panel does not include a tool to change or add Mime Types, so out of the box, your hosted site will probably not be able to serve .webm, .ogg, .ogv, or .oga content. In addition, by default, Godaddy servers are configured to use the mime type "video/mpeg" for the .mp4 extension rather than "video/mp4", which is why your mp4 video might be failing to play even in IE 10. However, if your site is hosted on IIS 7+ you can add some entries to your web.config file. Find the <system.webServer> element and look for a section called <staticContent>. If it is not there you should add it so that it looks something like this:

<system.webServer>
    <!-- Other elements -->
    <staticContent>
	  <mimeMap fileExtension=".m4v" mimeType="video/m4v" />
	  <mimeMap fileExtension=".ogg" mimeType="audio/ogg" />
	  <mimeMap fileExtension=".oga" mimeType="audio/ogg" />
	  <mimeMap fileExtension=".ogv" mimeType="video/ogg" />
	  <mimeMap fileExtension=".webm" mimeType="video/webm"/>
	  <remove fileExtension=".mp4" />
	  <mimeMap fileExtension=".mp4" mimeType="video/mp4" />
    </staticContent>
</system.webServer>

Note that because the .mp4 extension already has a mime type configured for it on the server, we have to remove it first.

Troubleshooting: If, after adding this section you start seeing Error 500 server errors on your site, first make certain that there aren't now 2 staticContent sections in your system.webServer section, then remove all of the mimeMap elements and see if the error goes away. If it does, simply add them back one at a time until you cause the error again. Add a remove element before the mimeMap(s) that cause the exception.

Save 25%

 


Categories: ASP.Net
Posted by Williarob on Friday, May 31, 2013 6:37 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Adding a Paypal Donate button to your Blogengine.Net blog or ASP.Net Page.

I recently had a request from a client to add a paypal donate button to their blog. So off I went to the Paypal site to get instructions for them on how to generate the button html for them to send to me.

  • Login to your paypal account (or create one).
  • Click on the Merchant Services tag.
  • Click on "Create Payment Buttons for your Website"
  • Choose "Donate" as the button type, and complete the form.
  • At the end of the process, you have the html you need to add the button to any site.

They did all this and sent me the html which consists of a simple form. Initially, I simlpy created a new text widget and pasted it in. The button looked as it should but when I clicked on it I got a 404 error. I viewed the source for the page and saw that the Form tag had been stripped, probalby by the text editor. No problem, I went to the Site Master page, and pasted it directly into the theme template, exactly where it was needed. Published the change, still goes to a 404 page. Then I realize what the problem is. I'm nesting forms. Blogengine.Net, like many traditional ASP.Net based sites, relies on the page being wrapped in a <form runat="server"> tag, which allows for all the postback stuff to work.

A quick Google search for "BlogEngine PayPal Donate button" returned a handy little Extension. But on closer inspection I could see that while this would add a button inside a post, it could not be relied upon to add it to the frame of the page itself, so that it could appear at all times, regardless of which post or page you were viewing. Of course, if it could add it to a post it must have found a way around the nested form issue, so I took a look at the code and discovered that it was creating a simple link, that is an image, wrapped in an anchor tag that looked something like this:

<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=[email address here]&item_name=[Description, e.g. "Donation to MySite.com" (UrlEncoded)]&no_shipping=0&no_note=1&tax=0¤cy_code=USD&lc=US&bn=PP%2dD onationsBF&charset=UTF%2d8"><img src="Path/To/PaypalDonateButton.jpg" /></a>

So I filled in my information and I finally had a working paypal donate button. However, I didn't like the fact that my email address was now so firmly embedded on the page, I rather liked the anonymity that the original paypal code offered. Any spider could skim my email from the page and start adding to the thousands of spam emails I already receive every day. So I kept looking for other solutions. As usual, Stack Overflow had a better solution.

Instead of using the anchor tag which makes a Get request and exposes your email address, paste in the full code from paypal, then delete the form tags. Replace the image input with a server side image button or regular button and have it post back to paypal for example If the paypal code looks like this:

 

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="xxx">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" 
alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

 

You paste it all in where you want it then modify it like this:

 

<input name="cmd" type="hidden" value="_s-xclick" /> 
<input name="hosted_button_id" type="hidden" value="xxx" /> 			
<asp:ImageButton ID="btnDonate" 
AlternateText="PayPal - The safer, easier way to pay online!" runat="server" 
ImageUrl="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" 
 PostBackUrl="https://www.paypal.com/cgi-bin/webscr" OnClick="btnPayNow_Click"/>
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" />

 

In your code behind, add an empty method to handle the OnClick specified above (btnPayNow_Click):

 

protected void btnPayNow_Click(object sender, EventArgs e)
{
        
}

 

That's it! Now you have a working Paypal donate button. If you want to test it out to see if it works, by all means click on the one you see on this page ;) Or, you can change the postback Url on your button to "https://www.sandbox.paypal.com/us/cgi-bin/webscr" which should allow you to test it without actually spending any money.


Categories: ASP.Net
Posted by Williarob on Friday, October 26, 2012 7:43 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Moving Files in Visual Studio and SVN

I'm writing this down because (1) I've had to figure it out a few times already and don't want to figure it out again, and (2) it may save you some snortin' and cussin' if you run across it yourself. It is astonishingly annoying.

If you have an MVC 3 project in Visual Studio 2010 and while refactoring you move a file - for example you move a view model to the shared area, you may suddenly encounter a compile error in some random temporary file like the one below:

The type or namespace name 'xxx' does not exist in the namespace 'xxx' (are you missing an assembly reference?)    c:\Users\[user name]\AppData\Local\Temp\Temporary ASP.NET Files\temp\eab6c63b\948de17e\App_Web_hadnllup.0.cs

Cleaning the solution and/or deleting the temporary files will not resolve the problem. This can happen whether you simply drag and drop the file to the new location, then change the namespace yourself, or if you right click on the file and choose Refactor > Move and have Visual Studio move the file and update the namespaces for you. The latter process (in theory) will update all references to the file within your project automatically, while with the former, you would typically try to compile the project and then fix all the broken items that now appear in the Error list as a result of the namespace change. However, unless you do a global find and replace, you will probably still end up getting the cryptic error above because chances are that the @model declaration in one or more of your Views is still pointing to the old namespace for that viewmodel file. Update the view(s) and the error will go away and all will be fine.

Now, if you use SVN, things are a little more complicated: If you move the file using either of the techniques described above, what will happen is SVN will delete the original file and create a new one for the new location, which is fine, unless you were hoping to preserve the full subversion history of that file. If you want to preserve the file history in SVN and move the file, this is how you do it: in Windows Explorer, right-click and drag the file from its old location to its new location, then select "SVN move versioned item" from the context menu. This will not only move the actual file itself, but it will also make sure that all the file history stays with it after you check in your changes. Back in Visual Studio, use the Solution Explorer in VS2010 to "exclude from project" the (now-missing) copy of the file in its old location, and then "include in project" the file in its new location. You may need to refresh the view in solution explorer and/or make sure you are viewing all the files by clicking the "Show all Files" icon at the top (next to the refresh icon) in order to see these files.

After you update the namespace to reflect the new location, I recommend using a global find and replace before you try to compile to save yourself a lot of trouble.

To summarize, if you find yourself getting obsolete, broken references in auto-generated files that you can't permanently delete, look in your Views folder for the bad references.


Posted by Williarob on Wednesday, May 25, 2011 7:33 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Override Configuration Manager

Recently I have been working on ways to solve configuration issues in large, multi environment solutions. In the beginning, I simply wanted to store shared app settings and connection strings with a class library so I didn't have to keep copying common configuration settings from project to project within the same solution. Taking that a step further, I thought it would be great to auto detect the runtime environment and use the right app settings and connection strings from that shared configuration file. This all works great, but it has two major drawbacks: firstly, third party tools such as Elmah, and built in tools such as the Membership, Profile and Role Providers look no further that the built in ConfigurationManager object for appSettings and connection strings which forces us to subclass (Dynamically setting the Elmah connection string at runtime) or override their initialization (Setting Membership-Profile-Role provider's connection string at runtime) in order for them to work with our new settings. Not all third party tools will be as easy to fix. Secondly, all the developers working on the project must be trained to use the new techniques and always remember to use Core.Configuration.AppSettings["key"] instead of ConfigurationManager because ConfigurationManager.AppSettings["key"] may be null or hold the wrong value.

With that in mind, the next logical step was to find a way to override the built in ConfigurationManager ensuring that the Core.Configuration settings are fully integrated. In short: any call to ConfigurationManager.AppSettings or ConfigurationManager.ConnectionStrings should return the correct setting, whether that setting comes from the local web/app.config or the Core.Config. In order to do this it is assumed that if a setting appears both in the local app/web.config and the Core.Config files, then the value in the Core.Config file will be the value returned.

Download the latest version of the Williablog.Core project:

Williablog.Core.zip (110.11 kb)

Add a reference to it from your project (either to the project or the dll in the bin folder) and the first line in void Main() of your console Application or (if a web application) Application_Start()  in Global.asax should be:

Williablog.Core.Configuration.ConfigSystem.Install();

This will reinitialize the Configuration forcing it to rebuild the static cache of values but this time we are in control, and as a result we are able to effectively override the ConfigurationManager. Here is the code:

namespace Williablog.Core.Configuration

{

    using System;

    using System.Collections.Specialized;

    using System.Configuration;

    using System.Configuration.Internal;

    using System.Reflection;

 

    using Extensions;

 

    public sealed class ConfigSystem : IInternalConfigSystem

    {

        private static IInternalConfigSystem clientConfigSystem;

 

        private object appsettings;

 

        private object connectionStrings;

 

        /// <summary>

        /// Re-initializes the ConfigurationManager, allowing us to merge in the settings from Core.Config

        /// </summary>

        public static void Install()

        {

            FieldInfo[] fiStateValues = null;

            Type tInitState = typeof(System.Configuration.ConfigurationManager).GetNestedType("InitState", BindingFlags.NonPublic);

 

            if (null != tInitState)

            {

                fiStateValues = tInitState.GetFields();

            }

 

            FieldInfo fiInit = typeof(System.Configuration.ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static);

            FieldInfo fiSystem = typeof(System.Configuration.ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static);

 

            if (fiInit != null && fiSystem != null && null != fiStateValues)

            {

                fiInit.SetValue(null, fiStateValues[1].GetValue(null));

                fiSystem.SetValue(null, null);

            }

 

            ConfigSystem confSys = new ConfigSystem();

            Type configFactoryType = Type.GetType("System.Configuration.Internal.InternalConfigSettingsFactory, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true);

            IInternalConfigSettingsFactory configSettingsFactory = (IInternalConfigSettingsFactory)Activator.CreateInstance(configFactoryType, true);

            configSettingsFactory.SetConfigurationSystem(confSys, false);

 

            Type clientConfigSystemType = Type.GetType("System.Configuration.ClientConfigurationSystem, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true);

            clientConfigSystem = (IInternalConfigSystem)Activator.CreateInstance(clientConfigSystemType, true);

        }

 

        #region IInternalConfigSystem Members

 

        public object GetSection(string configKey)

        {

            // get the section from the default location (web.config or app.config)

            object section = clientConfigSystem.GetSection(configKey);

 

            switch (configKey)

            {

                case "appSettings":

                    if (this.appsettings != null)

                    {

                        return this.appsettings;

                    }

 

                    if (section is NameValueCollection)

                    {

                        // create a new collection because the underlying collection is read-only

                        var cfg = new NameValueCollection((NameValueCollection)section);

 

                        // merge the settings from core with the local appsettings

                        this.appsettings = cfg.Merge(Core.Configuration.ConfigurationManager.AppSettings);

                        section = this.appsettings;

                    }

 

                    break;

                case "connectionStrings":

                    if (this.connectionStrings != null)

                    {

                        return this.connectionStrings;

                    }

 

                    // create a new collection because the underlying collection is read-only

                    var cssc = new ConnectionStringSettingsCollection();

 

                    // copy the existing connection strings into the new collection

                    foreach (ConnectionStringSettings connectionStringSetting in ((ConnectionStringsSection)section).ConnectionStrings)

                    {

                        cssc.Add(connectionStringSetting);

                    }

 

                    // merge the settings from core with the local connectionStrings

                    cssc = cssc.Merge(ConfigurationManager.ConnectionStrings);

 

                    // Cannot simply return our ConnectionStringSettingsCollection as the calling routine expects a ConnectionStringsSection result

                    ConnectionStringsSection connectionStringsSection = new ConnectionStringsSection();

 

                    // Add our merged connection strings to the new ConnectionStringsSection

                    foreach (ConnectionStringSettings connectionStringSetting in cssc)

                    {

                        connectionStringsSection.ConnectionStrings.Add(connectionStringSetting);

                    }

 

                    this.connectionStrings = connectionStringsSection;

                    section = this.connectionStrings;

                    break;

            }

 

            return section;

        }

 

        public void RefreshConfig(string sectionName)

        {

            if (sectionName == "appSettings")

            {

                this.appsettings = null;

            }

 

            if (sectionName == "connectionStrings")

            {

                this.connectionStrings = null;

            }

 

            clientConfigSystem.RefreshConfig(sectionName);

        }

 

        public bool SupportsUserConfig

        {

            get { return clientConfigSystem.SupportsUserConfig; }

        }

 

        #endregion

    }

}

The code to actually merge our collections is implemented as Extension methods:

namespace Williablog.Core.Extensions

{

    using System;

    using System.Collections.Generic;

    using System.Collections.Specialized;

    using System.Configuration;

    using System.Linq;

    using System.Linq.Expressions;

    using System.Text;

 

    public static class IEnumerableExtensions

    {

        /// <summary>

        /// Merges two NameValueCollections.

        /// </summary>

        /// <param name="first"></param>

        /// <param name="second"></param>

        /// <remarks>Used by <see cref="Williablog.Core.Configuration.ConfigSystem">ConfigSystem</c> to merge AppSettings</remarks>

        public static NameValueCollection Merge(this NameValueCollection first, NameValueCollection second)

        {

            if (second == null)

            {

                return first;

            }

 

            foreach (string item in second)

            {

                if (first.AllKeys.Contains(item))

                {

                    // if first already contains this item, update it to the value of second

                    first[item] = second[item];

                }

                else

                {

                    // otherwise add it

                    first.Add(item, second[item]);

                }

            }

 

            return first;

        }

 

        /// <summary>

        /// Merges two ConnectionStringSettingsCollections.

        /// </summary>

        /// <param name="first"></param>

        /// <param name="second"></param>

        /// <remarks>Used by <see cref="Williablog.Core.Configuration.ConfigSystem">ConfigSystem</c> to merge ConnectionStrings</remarks>

        public static ConnectionStringSettingsCollection Merge(this ConnectionStringSettingsCollection first, ConnectionStringSettingsCollection second)

        {

            if (second == null)

            {

                return first;

            }

 

            foreach (ConnectionStringSettings item in second)

            {

                ConnectionStringSettings itemInSecond = item;

                ConnectionStringSettings existingItem = first.Cast<ConnectionStringSettings>().FirstOrDefault(x => x.Name == itemInSecond.Name);

 

                if (existingItem != null)

                {

                    first.Remove(item);

                }

 

                first.Add(item);

            }

 

            return first;

        }

    }

}

If we create a console application to test with, complete with it's own app.config file that looks like this:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="WebServiceUrl" value="http://webservices.yourserver.com/YourService.asmx"/>

    <add key="SmtpServer" value="smtp.yourmailserver.com"/>

    <add key="LocalOnly" value="This is from the local app.config"/>

  </appSettings>

  <connectionStrings>

    <add name="AppData" connectionString="data source=Audi01;initial catalog=MyDB;User ID=User;Password=Password;" providerName="System.Data.SqlClient"/>

    <add name="ElmahDB" connectionString="Database=ELMAH;Server=Audi02;User=User;Pwd=Password;" providerName="System.Data.SqlClient"/>

  </connectionStrings>

</configuration>

And run it with the following code:

        static void Main(string[] args)

        {

            ConfigSystem.Install();

 

            Console.WriteLine(System.Configuration.ConfigurationManager.AppSettings["SmtpServer"]);

            Console.WriteLine(System.Configuration.ConfigurationManager.AppSettings["LocalOnly"]);

            Console.WriteLine(System.Configuration.ConfigurationManager.ConnectionStrings["AppData"]);

        }

 

The output is:

smtp.yourlocalmailserver.com

This is from the local app.config
data source=Ford01;initial catalog=MyDB;User ID=User;Password=Password;

With the exception of the middle one (LocalOnly) all of these settings come from Williablog.Core.Config, not the local app.config proving that the config files were successfully merged.

The ConfigSystem class could be modified to retrive the additional appsettings from the registry, from a database or from any other source you care to use.

I'd like to thank the contributers/authors of the following articles which I found very helpful:

http://stackoverflow.com/questions/158783/is-there-a-way-to-override-configurationmanager-appsettings

http://andypook.blogspot.com/2007/07/overriding-configurationmanager.html


Posted by Williarob on Monday, March 29, 2010 7:13 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Dynamically setting the Elmah connection string at runtime

If you have read my other articles about setting the SQL Membership provider's connection string at runtime, or automatically detecting the server name and using the appropriate connection strings then it will come as no surprise to see that I also had to find a way to set the Elmah connection string property dynamically too. If you are reading this, I'll assume that you already know what Elmah is and how to configure it. The problem then is simply that the connection string is supplied in the <elmah><errorLog> section of the web.config using a connection string name, and that while the name may the same in production as it is in development, chances are high that the connection string itself is different. The connection string property is readonly, so you can't change it at runtime. One solution is to create an elmah.config file, and use Finalbuilder or a web deployment project to change the path to that file when publishing, but if you like the AdvancedSettingsManager class I created and want to use that to set it you'll need to use a custom ErrorLog. Fortunately, Elmah is open source, so I simply downloaded the source, took a look at their SqlErrorLog class and then copied and pasted most of the code from that class into my own project, modifying it only slightly to suit my own needs.

In the end, the only changes I really needed to make were to pull the connectionstring by name from my AdvancedSettingsManager class and to copy a couple of helper functions locally into this class since they were marked as internal and therefore unavailable outside of the Elmah solution. I also removed the conditional compilation flags that only applied to .Net 1.x since this was a .Net 3.5 project.

namespace Williablog.Core.Providers

{

    #region Imports

 

    using System;

    using System.Configuration;

    using System.Data;

    using System.Data.SqlClient;

    using System.Diagnostics;

    using System.Threading;

    using System.Xml;

 

    using Elmah;

 

    using ApplicationException = System.ApplicationException;

    using IDictionary = System.Collections.IDictionary;

    using IList = System.Collections.IList;

 

    #endregion

 

    public class SqlErrorLog : ErrorLog

    {

        private readonly string _connectionString;

 

        private const int _maxAppNameLength = 60;

 

        private delegate RV Function<RV, A>(A a);

 

        ///<summary>

        /// Initializes a new instance of the <see cref="SqlErrorLog"/> class

        /// using a dictionary of configured settings.

        ///</summary>

 

        public SqlErrorLog(IDictionary config)

        {

            if (config == null)

                throw new ArgumentNullException("config");

 

// Start Williablog changes

 

            string connectionStringName = (string)config["connectionStringName"] jQuery1520895691458676146_1360618079128 string.Empty;

 

            string connectionString = string.Empty;

 

            if (connectionStringName.Length > 0)

            {

 

            //

            // Write your code here to get the connection string as a ConnectionStringSettings object

 

            //

                ConnectionStringSettings settings = Williablog.Core.Configuration.AdvancedSettingsManager.SettingsFactory().ConnectionStrings["ErrorDB"];

                if (settings == null)

                    throw new ApplicationException("Connection string is missing for the SQL error log.");

 

                connectionString = settings.ConnectionString ?? string.Empty;

            }

 

// End Williablog changes

 

            //

            // If there is no connection string to use then throw an

            // exception to abort construction.

            //

 

            if (connectionString.Length == 0)

                throw new ApplicationException("Connection string is missing for the SQL error log.");

 

            _connectionString = connectionString;

 

            //

            // Set the application name as this implementation provides

            // per-application isolation over a single store.

            //

 

            string appName = NullString((string)config["applicationName"]);

 

            if (appName.Length > _maxAppNameLength)

            {

                throw new ApplicationException(string.Format(

                    "Application name is too long. Maximum length allowed is {0} characters.",

                    _maxAppNameLength.ToString("N0")));

            }

 

            ApplicationName = appName;

        }

 

        ///<summary>

        /// Initializes a new instance of the <see cref="SqlErrorLog"/> class

        /// to use a specific connection string for connecting to the database.

        ///</summary>

 

        public SqlErrorLog(string connectionString)

        {

            if (connectionString == null)

                throw new ArgumentNullException("connectionString");

 

            if (connectionString.Length == 0)

                throw new ArgumentException(null, "connectionString");

 

            _connectionString = connectionString;

        }

 

        ///<summary>

        /// Gets the name of this error log implementation.

        ///</summary>

 

        public override string Name

        {

            get { return "Microsoft SQL Server Error Log"; }

        }

 

        ///<summary>

        /// Gets the connection string used by the log to connect to the database.

        ///</summary>

 

        public virtual string ConnectionString

        {

            get { return _connectionString; }

        }

 

        ///<summary>

        /// Logs an error to the database.

        ///</summary>

        ///<remarks>

        /// Use the stored procedure called by this implementation to set a

        /// policy on how long errors are kept in the log. The default

        /// implementation stores all errors for an indefinite time.

        ///</remarks>

 

        public override string Log(Error error)

        {

            if (error == null)

                throw new ArgumentNullException("error");

 

            string errorXml = ErrorXml.EncodeString(error);

            Guid id = Guid.NewGuid();

 

            using (SqlConnection connection = new SqlConnection(this.ConnectionString))

            using (SqlCommand command = Commands.LogError(

                id, this.ApplicationName,

                error.HostName, error.Type, error.Source, error.Message, error.User,

                error.StatusCode, error.Time.ToUniversalTime(), errorXml))

            {

                command.Connection = connection;

                connection.Open();

                command.ExecuteNonQuery();

                return id.ToString();

            }

        }

 

        ///<summary>

        /// Returns a page of errors from the databse in descending order

        /// of logged time.

        ///</summary>

 

        public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList)

        {

            if (pageIndex < 0)

                throw new ArgumentOutOfRangeException("pageIndex", pageIndex, null);

 

            if (pageSize < 0)

                throw new ArgumentOutOfRangeException("pageSize", pageSize, null);

 

            using (SqlConnection connection = new SqlConnection(this.ConnectionString))

            using (SqlCommand command = Commands.GetErrorsXml(this.ApplicationName, pageIndex, pageSize))

            {

                command.Connection = connection;

                connection.Open();

 

                XmlReader reader = command.ExecuteXmlReader();

 

                try

                {

                    ErrorsXmlToList(reader, errorEntryList);

                }

                finally

                {

                    reader.Close();

                }

 

                int total;

                Commands.GetErrorsXmlOutputs(command, out total);

                return total;

            }

        }

 

        ///<summary>

        /// Begins an asynchronous version of <see cref="GetErrors"/>.

        ///</summary>

 

        public override IAsyncResult BeginGetErrors(int pageIndex, int pageSize, IList errorEntryList,

            AsyncCallback asyncCallback, object asyncState)

        {

            if (pageIndex < 0)

                throw new ArgumentOutOfRangeException("pageIndex", pageIndex, null);

 

            if (pageSize < 0)

                throw new ArgumentOutOfRangeException("pageSize", pageSize, null);

 

            //

            // Modify the connection string on the fly to support async

            // processing otherwise the asynchronous methods on the

            // SqlCommand will throw an exception. This ensures the

            // right behavior regardless of whether configured

            // connection string sets the Async option to true or not.

            //

 

            SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(this.ConnectionString);

            csb.AsynchronousProcessing = true;

            SqlConnection connection = new SqlConnection(csb.ConnectionString);

 

            //

            // Create the command object with input parameters initialized

            // and setup to call the stored procedure.

            //

 

            SqlCommand command = Commands.GetErrorsXml(this.ApplicationName, pageIndex, pageSize);

            command.Connection = connection;

 

            //

            // Create a closure to handle the ending of the async operation

            // and retrieve results.

            //

 

            AsyncResultWrapper asyncResult = null;

 

            Function<int, IAsyncResult> endHandler = delegate

            {

                Debug.Assert(asyncResult != null);

 

                using (connection)

                using (command)

                {

                    using (XmlReader reader = command.EndExecuteXmlReader(asyncResult.InnerResult))

                        ErrorsXmlToList(reader, errorEntryList);

 

                    int total;

                    Commands.GetErrorsXmlOutputs(command, out total);

                    return total;

                }

            };

 

            //

            // Open the connenction and execute the command asynchronously,

            // returning an IAsyncResult that wrap the downstream one. This

            // is needed to be able to send our own AsyncState object to

            // the downstream IAsyncResult object. In order to preserve the

            // one sent by caller, we need to maintain and return it from

            // our wrapper.

            //

 

            try

            {

                connection.Open();

 

                asyncResult = new AsyncResultWrapper(

                    command.BeginExecuteXmlReader(

                        asyncCallback != null ? /* thunk */ delegate { asyncCallback(asyncResult); } : (AsyncCallback)null,

                        endHandler), asyncState);

 

                return asyncResult;

            }

            catch (Exception)

            {

                connection.Dispose();

                throw;

            }

        }

 

        ///<summary>

        /// Ends an asynchronous version of <see cref="ErrorLog.GetErrors"/>.

        ///</summary>

 

        public override int EndGetErrors(IAsyncResult asyncResult)

        {

            if (asyncResult == null)

                throw new ArgumentNullException("asyncResult");

 

            AsyncResultWrapper wrapper = asyncResult as AsyncResultWrapper;

 

            if (wrapper == null)

                throw new ArgumentException("Unexepcted IAsyncResult type.", "asyncResult");

 

            Function<int, IAsyncResult> endHandler = (Function<int, IAsyncResult>)wrapper.InnerResult.AsyncState;

            return endHandler(wrapper.InnerResult);

        }

 

        private void ErrorsXmlToList(XmlReader reader, IList errorEntryList)

        {

            Debug.Assert(reader != null);

 

            if (errorEntryList != null)

            {

                while (reader.IsStartElement("error"))

                {

                    string id = reader.GetAttribute("errorId");

                    Error error = ErrorXml.Decode(reader);

                    errorEntryList.Add(new ErrorLogEntry(this, id, error));

                }

            }

        }

 

        ///<summary>

        /// Returns the specified error from the database, or null

        /// if it does not exist.

        ///</summary>

        public override ErrorLogEntry GetError(string id)

        {

            if (id == null)

                throw new ArgumentNullException("id");

 

            if (id.Length == 0)

                throw new ArgumentException(null, "id");

 

            Guid errorGuid;

 

            try

            {

                errorGuid = new Guid(id);

            }

            catch (FormatException e)

            {

                throw new ArgumentException(e.Message, "id", e);

            }

 

            string errorXml;

 

            using (SqlConnection connection = new SqlConnection(this.ConnectionString))

            using (SqlCommand command = Commands.GetErrorXml(this.ApplicationName, errorGuid))

            {

                command.Connection = connection;

                connection.Open();

                errorXml = (string)command.ExecuteScalar();

            }

 

            if (errorXml == null)

                return null;

 

            Error error = ErrorXml.DecodeString(errorXml);

            return new ErrorLogEntry(this, id, error);

        }

 

// These utility functions were marked as internal, so I had to copy them locally

        public static string NullString(string s)

        {

            return s ?? string.Empty;

        }

 

        public static string EmptyString(string s, string filler)

        {

            return NullString(s).Length == 0 ? filler : s;

        }

 

// End

 

        private sealed class Commands

        {

            private Commands() { }

 

            public static SqlCommand LogError(

                Guid id,

                string appName,

                string hostName,

                string typeName,

                string source,

                string message,

                string user,

                int statusCode,

                DateTime time,

                string xml)

            {

                SqlCommand command = new SqlCommand("ELMAH_LogError");

                command.CommandType = CommandType.StoredProcedure;

 

                SqlParameterCollection parameters = command.Parameters;

 

                parameters.Add("@ErrorId", SqlDbType.UniqueIdentifier).Value = id;

                parameters.Add("@Application", SqlDbType.NVarChar, _maxAppNameLength).Value = appName;

                parameters.Add("@Host", SqlDbType.NVarChar, 30).Value = hostName;

                parameters.Add("@Type", SqlDbType.NVarChar, 100).Value = typeName;

                parameters.Add("@Source", SqlDbType.NVarChar, 60).Value = source;

                parameters.Add("@Message", SqlDbType.NVarChar, 500).Value = message;

                parameters.Add("@User", SqlDbType.NVarChar, 50).Value = user;

                parameters.Add("@AllXml", SqlDbType.NText).Value = xml;

                parameters.Add("@StatusCode", SqlDbType.Int).Value = statusCode;

                parameters.Add("@TimeUtc", SqlDbType.DateTime).Value = time;

 

                return command;

            }

 

            public static SqlCommand GetErrorXml(string appName, Guid id)

            {

                SqlCommand command = new SqlCommand("ELMAH_GetErrorXml");

                command.CommandType = CommandType.StoredProcedure;

 

                SqlParameterCollection parameters = command.Parameters;

                parameters.Add("@Application", SqlDbType.NVarChar, _maxAppNameLength).Value = appName;

                parameters.Add("@ErrorId", SqlDbType.UniqueIdentifier).Value = id;

 

                return command;

            }

 

            public static SqlCommand GetErrorsXml(string appName, int pageIndex, int pageSize)

            {

                SqlCommand command = new SqlCommand("ELMAH_GetErrorsXml");

                command.CommandType = CommandType.StoredProcedure;

 

                SqlParameterCollection parameters = command.Parameters;

 

                parameters.Add("@Application", SqlDbType.NVarChar, _maxAppNameLength).Value = appName;

                parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex;

                parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;

                parameters.Add("@TotalCount", SqlDbType.Int).Direction = ParameterDirection.Output;

 

                return command;

            }

 

            public static void GetErrorsXmlOutputs(SqlCommand command, out int totalCount)

            {

                Debug.Assert(command != null);

 

                totalCount = (int)command.Parameters["@TotalCount"].Value;

            }

        }

 

        ///<summary>

        /// An <see cref="IAsyncResult"/> implementation that wraps another.

        ///</summary>

 

        private sealed class AsyncResultWrapper : IAsyncResult

        {

            private readonly IAsyncResult _inner;

            private readonly object _asyncState;

 

            public AsyncResultWrapper(IAsyncResult inner, object asyncState)

            {

                _inner = inner;

                _asyncState = asyncState;

            }

 

            public IAsyncResult InnerResult

            {

                get { return _inner; }

            }

 

            public bool IsCompleted

            {

                get { return _inner.IsCompleted; }

            }

 

            public WaitHandle AsyncWaitHandle

            {

                get { return _inner.AsyncWaitHandle; }

            }

 

            public object AsyncState

            {

                get { return _asyncState; }

            }

 

            public bool CompletedSynchronously

            {

                get { return _inner.CompletedSynchronously; }

            }

        }

    }

}

Finally all you need to do is modify the web.config file to use this SqlErrorlog instead of the built in one:

  <elmah>  

    <errorLogtype="Williablog.Core.Providers.SqlErrorLog, Williablog.Core"

            connectionStringName="ErrorDB" />

<!--

            Other elmah settings ommitted for clarity

-->

  </elmah>

Note: You will still need to reference the Elmah dll in your project as all we have done here is subclass the ErrorLog type, all of the remaining Elmah goodness is still locked up inside the elmah dll. You could of course make these changes directly inside the elmah source code and recompile it to produce your own version of the elmah dll, but these changes were project specific and I didn't want to end up one day with dozens of project specific versions of the elmah dll. This way, the project specific code stays with the project and the elmah dll remains untouched.

Edit: As Stan Shillis points out on the Code project version of this article, there is a cleaner, simpler approach that will allow you to keep up with new versions of Elmah without editing the source of each release:

Instead of fully rewriting Elmah's SQLErrorLog you can inherit it and override just the ConnectingString property. This way you don't loose benefits of Elmah code updates.
 
Sample code:

public class CustomSqlErrorLog : Elmah.SqlErrorLog
{
	protected string connectionStringName;
	public CustomSqlErrorLog(IDictionary config) : base(config)
	{
		connectionStringName = (string)config["connectionStringName"];
	}
 
	public override string ConnectionString {
		get { return CustomConfigManager.ConnectionStrings[connectionStringName]; }
	}
}

 
The only caveat is that you still have to have that connection string entry in your web.config ConnectionStrings sections because SqlErrorLog base class checks for its existence. It won't actually use the connection string from config file but it needs to be there for it work properly.
 
Sample config:
 

<elmah>
<errorLog type="YourNameSpace.CustomSqlErrorLog, YourAssembly" connectionStringName="Elmah" applicationName="CustomApp" />
</elmah>
 
<connectionStrings>
    <add name="Elmah" connectionString="do.not.change.or.remove.this" providerName="System.Data.SqlClient" />
</connectionStrings>

Categories: ASP.Net | C# | CodeProject
Posted by Williarob on Thursday, March 18, 2010 12:10 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Auto detect the runtime environment and use the right app settings and connection strings

There are many ways to manage the problem of connection string and app settings substitution in the web.config / app.config files when publishing to different environments (e.g. QA and Production servers). In the past I have made use of the Web Deployment project's ability to replace the appsettings and connectionstrings sections, I have experimented with batch files, Build events, conditional compilation and used the extremely powerful FinalBuilder. However, my prefered solution is to have a single shared .config file with all the possible settings in it (so you only have to open one file to change any of the settings) then have the executing application automatically detect the environment and use the correct settings every time.

The technique dicussed below builds on that of an earlier article which described how to centralize your shared application settings and connection strings in a common class library. It also assumes that you know the machine names of your development, QA and production servers. Obviously servers get replaced from time to time and websites sometimes get moved from one server to another, but it has been my experience that there is usually some sort of common naming convention used on servers and web farms, and knowing that convention should be good enough. Even this is not the case, the Development, QA and Production server names are stored in an app setting so you can easily change them at any time if necessary. For this example, the assumption is that the development servers are all named something like "Squirrel01", "Squirrel02", the QA boxes are "Fox01", "Fox02", and the production (farm) boxes are "Rabbit01x", "Rabbit01y", "Rabbit02x", "Rabbit02y", etc. With this in mind, it is necessary only to look for the words "Rabbit", "Fox" or "Squirrel" in the machine name we are running on to identify the current environment and know which section of our config file to use. If none of these names is found, we shall assume the app is running on the localhost of a developer's computer, and use those settings. I should point out that it is possible to for a server to be configured in such a way as to prevent Environment.MachineName from returning a value, in which case this technique simply will not work, so before you start trying to integrate this code into your solution, I recommend you craete a quick test.aspx page or console app that simply does a Response.Write(Environment.MachineName)/Console.WriteLine(Environment.MachineName) and run it on your servers.

First, let's setup our .config file:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <sectionGroup name="Localhost" type="Williablog.Core.Configuration.EnvironmentSectionGroup, Williablog.Core">

      <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />

      <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />

    </sectionGroup>

 

    <sectionGroup name="Dev" type="Williablog.Core.Configuration.EnvironmentSectionGroup, Williablog.Core">

      <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />

      <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />

    </sectionGroup>

 

    <sectionGroup name="Qa" type="Williablog.Core.Configuration.EnvironmentSectionGroup, Williablog.Core">

      <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />

      <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />

    </sectionGroup>

 

    <sectionGroup name="Production" type="Williablog.Core.Configuration.EnvironmentSectionGroup, Williablog.Core">

      <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />

      <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />

    </sectionGroup>

  </configSections>

 

  <Localhost>

    <appSettings>

      <add key="WebServiceUrl" value="http://webservices.squirrel01.yourserver.com/YourService.asmx"/>

      <add key="SmtpServer" value="smtp.yourlocalmailserver.com"/>

    </appSettings>

    <connectionStrings>

      <add name="AppData" connectionString="data source=Ford01;initial catalog=MyDB;User ID=User;Password=Password;" providerName="System.Data.SqlClient"/>

      <add name="ElmahDB" connectionString="Database=ELMAH;Server=Ford02;User=User;Pwd=Password;" providerName="System.Data.SqlClient"/>

    </connectionStrings>

  </Localhost>

 

  <Dev>

    <appSettings>

      <add key="WebServiceUrl" value="http://webservices.squirrel01.yourserver.com/YourService.asmx"/>

      <add key="SmtpServer" value="smtp.yourlocalmailserver.com"/>

    </appSettings>

    <connectionStrings>

      <add name="AppData" connectionString="data source=Ford01;initial catalog=MyDB;User ID=User;Password=Password;" providerName="System.Data.SqlClient"/>

      <add name="ElmahDB" connectionString="Database=ELMAH;Server=Ford02;User=User;Pwd=Password;" providerName="System.Data.SqlClient"/>

    </connectionStrings>

  </Dev>

 

  <Qa>

    <appSettings>

      <add key="WebServiceUrl" value="http://webservices.Fox01.yourserver.com/YourService.asmx"/>

      <add key="SmtpServer" value="smtp.yourlocalmailserver.com"/>

    </appSettings>

    <connectionStrings>

      <add name="AppData" connectionString="data source=BMW01;initial catalog=MyDB;User ID=User;Password=Password;" providerName="System.Data.SqlClient"/>

      <add name="ElmahDB" connectionString="Database=ELMAH;Server=BMW02;User=User;Pwd=Password;" providerName="System.Data.SqlClient"/>

    </connectionStrings>

  </Qa>

 

  <Production>

    <appSettings>

      <add key="WebServiceUrl" value="http://webservices.yourserver.com/YourService.asmx"/>

      <add key="SmtpServer" value="smtp.yourmailserver.com"/>

    </appSettings>

    <connectionStrings>

      <add name="AppData" connectionString="data source=Audi01;initial catalog=MyDB;User ID=User;Password=Password;" providerName="System.Data.SqlClient"/>

      <add name="ElmahDB" connectionString="Database=ELMAH;Server=Audi02;User=User;Pwd=Password;" providerName="System.Data.SqlClient"/>

    </connectionStrings>

  </Production>

 

  <appSettings>

    <!-- Global/common appsettings can go here -->

    <add key="Test" value="Hello World"/>

 

    <add key="DevelopmentNames" value="SQUIRREL"/>

    <add key="ProductionNames" value="RABBIT"/>

    <add key="QANames" value="FOX"/>

    <add key="EnvironmentOverride" value=""/>

    <!-- /Dev | /Localhost | /Production | (blank)-->

 

  </appSettings>

</configuration>

As you can see, the first thing we do in the config file is declare four section groups, "LocalHost", "Dev", "Qa" and "Production". I chose to create a custom SectionGroup since this allowed me to strongly type the expected sections within it, greatly simplifying the code required to access those sections. All the EnvironmentSectionGroup class does, is inherit ConfigurationSectionGroup and declare two properties:

namespace Williablog.Core.Configuration

{

    using System.Configuration;

 

    public class EnvironmentSectionGroup : ConfigurationSectionGroup

    {

 

        #region Properties

 

        [ConfigurationProperty("appSettings")]

        public AppSettingsSection AppSettings

        {

            get

            {

                return (AppSettingsSection)Sections["appSettings"];

            }

        }

 

        [ConfigurationProperty("connectionStrings")]

        public ConnectionStringsSection ConnectionStrings

        {

            get

            {

                return (ConnectionStringsSection)Sections["connectionStrings"];

            }

        }

 

        #endregion

 

    }

}

Next, we create the sections for localhost, development, qa and production, each of which has its own appSettings and connectionStrings sections. These are of the same type as the connectionStrings and appSettings found in any .config file, meaning we don't need to write any additional code to fully utilise these sections - no traversing of primitive xmlNodes or anything like that to get the connectionstrings from that section. Finally we add the expected, normal appsettings section which in this case will provide the global or common appsettings that are shared by all environments. It is here that we store the server names that will help us identify where the app is currently executing. The EnvironmentOverride setting is an added bonus -it allows you to use all of qa or production settings while running on localhost which helps you debug those "well it works on my machine" situations without having to manually change all of the settings for localhost.

Building on the BasicSettingsManager we built earlier we simply add some code to determine the machine name we are running on and return the appSettings and connectionStrings sections appropriate to that environment:

namespace Williablog.Core.Configuration

{

    using System;

    using System.Collections.Specialized;

    using System.Configuration;

    using System.IO;

    using System.Linq;

 

    public class AdvancedSettingsManager

    {

        #region fields

 

        private const string ConfigurationFileName = "Williablog.Core.config";

 

        /// <summary>

        /// default path to the config file that contains the settings we are using

        /// </summary>

        private static string configurationFile;

 

        /// <summary>

        /// Stores an instance of this class, to cut down on I/O: No need to keep re-loading that config file

        /// </summary>

        /// <remarks>Cannot use system.web.caching since agents will not have access to this by default, so use static member instead.</remarks>

        private static AdvancedSettingsManager instance;

 

        /// <summary>

        /// Settings Environment

        /// </summary>

        private static string settingsEnvironment;

 

        private static EnvironmentSectionGroup currentSettingsGroup;

 

        #endregion

 

        #region Constructors

 

        private AdvancedSettingsManager()

        {

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

 

            fileMap.ExeConfigFilename = configurationFile;

 

            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

 

            settingsEnvironment = "Localhost"; // default to localhost

 

            // get the name of the machine we are currently running on

            string machineName = Environment.MachineName.ToUpper();

 

            // compare to known environment machine names

            if (config.AppSettings.Settings["ProductionNames"].Value.Split(',').Where(x => machineName.Contains(x)).Count() > 0)

            {

                settingsEnvironment = "Production";

            }

            else if (config.AppSettings.Settings["QANames"].Value.Split(',').Where(x => machineName.Contains(x)).Count() > 0)

            {

                settingsEnvironment = "Qa";

            }

            else if (config.AppSettings.Settings["DevelopmentNames"].Value.Split(',').Where(x => machineName.Contains(x)).Count() > 0)

            {

                settingsEnvironment = "Dev";

            }

 

            // If there is a value in the EnvironmentOverride appsetting, ignore results of auto detection and set it here

            // This allows us to hit production data from localhost without monkeying with all the config settings.

            if (!string.IsNullOrEmpty(config.AppSettings.Settings["EnvironmentOverride"].Value))

            {

                settingsEnvironment = config.AppSettings.Settings["EnvironmentOverride"].Value;

            }

 

            // Get the name of the section we are using in this environment & load the appropriate section of the config file

            currentSettingsGroup = config.GetSectionGroup(SettingsEnvironment) as EnvironmentSectionGroup;

        }

 

        #endregion

 

        #region Properties

 

        /// <summary>

        /// Returns the name of the current environment

        /// </summary>

        public string SettingsEnvironment

        {

            get

            {

                return settingsEnvironment;

            }

        }

 

        /// <summary>

        /// Returns the ConnectionStrings section

        /// </summary>

        public ConnectionStringSettingsCollection ConnectionStrings

        {

            get

            {

                return currentSettingsGroup.ConnectionStrings.ConnectionStrings;

            }

        }

 

        /// <summary>

        /// Returns the AppSettings Section

        /// </summary>

        public NameValueCollection AppSettings

        {

            get

            {

                NameValueCollection settings = new NameValueCollection();

                foreach (KeyValueConfigurationElement element in currentSettingsGroup.AppSettings.Settings)

                {

                    settings.Add(element.Key, element.Value);

                }

 

                return settings;

            }

        }

 

        #endregion

 

        #region static factory methods

 

        /// <summary>

        /// Public factory method

        /// </summary>

        /// <returns></returns>

        public static AdvancedSettingsManager SettingsFactory()

        {

            // If there is a bin folder, such as in web projects look for the config file there first

            if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\bin"))

            {

                configurationFile = string.Format(@"{0}\bin\{1}", AppDomain.CurrentDomain.BaseDirectory, ConfigurationFileName);

            }

            else

            {

                // agents, for example, won't have a bin folder in production

                configurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationFileName);

            }

 

            // If we still cannot find it, quit now!

            if (!File.Exists(configurationFile))

            {

                throw new FileNotFoundException(configurationFile);

            }

 

            return CreateSettingsFactoryInternal();

        }

 

        /// <summary>

        /// Overload that allows you to pass in the full path and filename of the config file you want to use.

        /// </summary>

        /// <param name="fullPathToConfigFile"></param>

        /// <returns></returns>

        public static AdvancedSettingsManager SettingsFactory(string fullPathToConfigFile)

        {

            configurationFile = fullPathToConfigFile;

            return CreateSettingsFactoryInternal();

        }

 

        /// <summary>internal Factory Method

        /// </summary>

        /// <returns>ConfigurationSettings object

        /// </returns>

        internal static AdvancedSettingsManager CreateSettingsFactoryInternal()

        {

            // If we havent created an instance yet, do so now

            if (instance == null)

            {

                instance = new AdvancedSettingsManager();

            }

 

            return instance;

        }

 

        #endregion

    }

}

As before you can then access the appSettings of the Core.Config from any of your projects like so:

Console.WriteLine(Williablog.Core.Configuration.AdvancedSettingsManager.SettingsFactory().AppSettings["Test"]);

To make this work, you will need to add a reference to System.Configuration. If the config file and Settings manager code is to be part of a class library, you will need to set the "Copy to Output Directory" property of your .config file to "Copy always"and add a reference to System.Configuration to each of your projects.

Download the Williablog.Core project: Williablog.Core.zip (100.77 kb)


Posted by Williarob on Thursday, March 18, 2010 9:00 AM
Permalink | Comments (0) | Post RSSRSS comment feed

How to store shared app settings and connection strings with your class library

When working on enterprise level, multi-tiered .Net applications it is not uncommon to want to create a shared class library, that may be used in multiple related projects. For example, let's suppose you are building a public website, a separate private intranet website used by company staff to manage the public site, and one or more console applications that may run as scheduled tasks related to both sites. You may have an console application that creates and emails reports about sales and other data, and another app that encodes video or audio that is uploaded to your site. Finally, you probably have another project for unit tests.

Since all of these projects will be working with the same database you also have a class library in your solution acting as your datalayer, and perhaps another Core library that contains other shared components. Each of these projects has it's own web.config or app.config file, and you had to copy and paste your connection string, smtp server data, and various other appSettings required by all the projects into every .config file. You may be inspired to add a new .config file to your Core library, and store all of the shared appsettings and connection strings in that one central location. If you then delete all of these settings from the other .config files you'll quickly realize that everything breaks. Even setting the "Copy to Output Directory" property of your Core.config file to "Copy always" doesn't fix this. The reason for this of course is that .Net always looks to the host application for the settings.

The solution is to add some code to your Core project that explicitly loads the Core.config file, reads in the data and makes the results available to all the other projects. That code might look something like this:

namespace Williablog.Core.Configuration

{

    using System;

    using System.Collections.Specialized;

    using System.Configuration;

    using System.IO;

 

    public class BasicSettingsManager

    {

        #region fields

 

        private const string ConfigurationFileName = "Williablog.Core.config";

 

        /// <summary>

        /// default path to the config file that contains the settings we are using

        /// </summary>

        private static string configurationFile;

 

        /// <summary>

        /// Stores an instance of this class, to cut down on I/O: No need to keep re-loading that config file

        /// </summary>

        /// <remarks>Cannot use system.web.caching since agents will not have access to this by default, so use static member instead.</remarks>

        private static BasicSettingsManager instance;

 

        private static Configuration config;

 

        #endregion

 

        #region Constructors

 

        private BasicSettingsManager()

        {

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

            fileMap.ExeConfigFilename = configurationFile;

            config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

        }

 

        #endregion

 

        #region Properties

 

        /// <summary>

        /// Returns the ConnectionStrings section

        /// </summary>

        public ConnectionStringSettingsCollection ConnectionStrings

        {

            get

            {

                return config.ConnectionStrings.ConnectionStrings;

            }

        }

 

        /// <summary>

        /// Returns the AppSettings Section

        /// </summary>

        public NameValueCollection AppSettings

        {

            get

            {

                NameValueCollection settings = new NameValueCollection();

                foreach (KeyValueConfigurationElement element in config.AppSettings.Settings)

                {

                    settings.Add(element.Key, element.Value);

                }

 

                return settings;

            }

        }

 

        #endregion

 

        #region static factory methods

 

        /// <summary>

        /// Public factory method

        /// </summary>

        /// <returns></returns>

        public static BasicSettingsManager SettingsFactory()

        {

            // If there is a bin folder, such as in web projects look for the config file there first

            if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\bin"))

            {

                configurationFile = string.Format(@"{0}\bin\{1}", AppDomain.CurrentDomain.BaseDirectory, ConfigurationFileName);

            }

            else

            {

                // agents, for example, won't have a bin folder in production

                configurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationFileName);

            }

 

            // If we still cannot find it, quit now!

            if (!File.Exists(configurationFile))

            {

                throw new FileNotFoundException(configurationFile);

            }

 

            return CreateSettingsFactoryInternal();

        }

 

        /// <summary>

        /// Overload that allows you to pass in the full path and filename of the config file you want to use.

        /// </summary>

        /// <param name="fullPathToConfigFile"></param>

        /// <returns></returns>

        public static BasicSettingsManager SettingsFactory(string fullPathToConfigFile)

        {

            configurationFile = fullPathToConfigFile;

            return CreateSettingsFactoryInternal();

        }

 

        /// <summary>internal Factory Method

        /// </summary>

        /// <returns>ConfigurationSettings object

        /// </returns>

        internal static BasicSettingsManager CreateSettingsFactoryInternal()

        {

            // If we havent created an instance yet, do so now

            if (instance == null)

            {

                instance = new BasicSettingsManager();

            }

 

            return instance;

        }

 

        #endregion

    }

}

You can then access the appSettings of Core.Config from any of your projects like so:

Console.WriteLine(Williablog.Core.Configuration.BasicSettingsManager.SettingsFactory().AppSettings["Key"]);

To make this work, you will need to set the "Copy to Output Directory" property of your Core.config file to "Copy always"and add a reference to System.Configuration to each of your projects.

We shall take this a step further next time and expand on this technique to enable your Core project to automatically detect wether it is running on localhost, a development environment, QA, or production, and to return the appropriate connection strings and settings for that environment.


Posted by Williarob on Thursday, March 18, 2010 8:08 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Use lambda expressions to aggregate values into a delimited string

Let's say you need to aggregate one value from each object in a list into a single string. For Example, you want to send an e-mail to a set of customers. This requires a string with the email addresses seperated by a semicolon (;). The following code will create a generic List of Books, and provide a method ListAllEmails() that will print the delimited list of emails to the console window:

 

namespace ConsoleApplication1

{

    using System;

    using System.Collections.Generic;

    using System.Linq;

 

    public class Lambdas

    {

        /// <summary>

        /// Define the Book Class

        /// </summary>

        public class Book

        {

            public string Title { get; set; }

            public string Author { get; set; }

            public double Price { get; set; }

            public string EmailAddress { get; set; }

        }

 

        public List<Book> Books { get; private set; }

 

        public Lambdas()

        {

            // Create a new list of Books

            Books = new List<Book> {

                new Book { Title = "Pro ASP.Net MVC Framework", Author = "Steven Sanderson", Price = 49.99, EmailAddress = "steve@nospam.com" },

                new Book{ Title = "Pro Silverlight 2 in C# 2008", Author = "Matthew MacDonald", Price = 49.99, EmailAddress = "Matthew@nospam.com" },

                new Book{ Title = "Pro VB 2008 and the .Net 3.5 Platform", Author = "Andrew Troelsen", Price = 59.99, EmailAddress = "Andrew@nospam.com" }

            };

        }

 

        /// <summary>

        /// Creates a semicolon (;) delimited list of email addresses

        /// </summary>

        public void ListAllEmails()

        {

            Console.WriteLine(this.Books.Select(b => b.EmailAddress).Aggregate((items, item) => items + "; " + item));

            // output= "steve@nospam.com; Matthew@nospam.com; Andrew@nospam.com"

        }

    }

}

 

The Select Method selects the EmailAddress for each Book. The Aggregate method builds a list of the items based on the lambda expression. Notice that this did not require any additional code to ensure there is no extra semi-colon at the beginning or end of the list, which is often required when using a loop to concatenate text.

 

Note: Be careful when using the Aggregate method because it is very inefficient on large numbers of strings. Consider using the String Join method instead.

 

In VB, the lambda would look like this:

 

   Console.WriteLine(Books.Select(Function(b) b.EmailAddress).Aggregate(Function(items, item) items & "; " & item))

 

You can also filter the list of email addresses. For Example, suppose you want to send an email to all the authors who sell their books for under $50, telling them that you think you can sell their next book for $59.99:

 

        /// <summary>

        /// Creates a semicolon (;) delimited list of email addresses where the price of the book is under $50

        /// </summary>

        public void ListSomeEmails()

        {

            Console.WriteLine(this.Books.Where(b => b.Price < 50).Select(b => b.EmailAddress).Aggregate((items, item) => items + ", " + item));

            // output= "steve@nospam.com, Matthew@nospam.com"

        }

 

Let's take this one step further. Suppose you wanted to create a comma separated list of values and replace the last comma with " and", so that a single item would be "item1", two items would be "item1 and item2", three items would be "item1, item2 and item3", etc.

 

        /// <summary>

        /// Creates a comma delimited list of email addresses and replaces the last comma with " and "

        /// </summary>

        public void ListEmailsAsSmartCsv()

        {

            string csv = this.Books.Select(b => b.EmailAddress).Aggregate((items, item) => items + ", " + item);

            Console.WriteLine(Regex.Replace(csv, @",\s([^,]+)$", " and $1"));

            // output= "steve@nospam.com, Matthew@nospam.com and Andrew@nospam.com"

        }

 

 


Tags:
Categories: ASP.Net | C# | VB
Posted by Williarob on Friday, February 26, 2010 9:44 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Simplify Your Code with Lambda Expressions

Most applications retain lists of things, and a common task is to find an item in that list. The following class illustrates three ways to find an item in a generic list:

 

namespace ConsoleApplication1

{

    using System;

    using System.Collections.Generic;

    using System.Linq;

 

    public class Lambdas

    {

        public class Book

        {

            public string Title { get; set; }

            public string Author { get; set; }

            public double Price { get; set; }

        }

 

        public List<Book> Books { get; private set; }

 

        public Lambdas()

        {

            Books = new List<Book> {

                new Book { Title = "Pro ASP.Net MVC Framework", Author = "Steven Sanderson", Price = 49.99 },

                new Book{ Title = "Pro Silverlight 2 in C# 2008", Author = "Matthew MacDonald", Price = 49.99},

                new Book{ Title = "Pro VB 2008 and the .Net 3.5 Platform", Author = "Andrew Troelsen", Price = 59.99 }

            };

        }

 

        /// <summary>

        /// Returns a book using a traditional loop

        /// </summary>

        private Book FindUsingTraditionalLoop(string title)

        {

            Book foundBook = null;

 

            foreach (var b in this.Books)

            {

                if (b.Title == title)

                {

                    foundBook = b;

                    break;

                }

            }

 

            return foundBook;

        }

 

        /// <summary>

        /// Returns the book using a Linq expression

        /// </summary>

        private Book FindUsingLinq(string title)

        {

            var query = from b in this.Books

                        where b.Title == title

                        select b;

 

            return query.Count() > 0 ? query.ToList()[0] : null;

        }

 

        /// <summary>

        /// Returns the book using a Lambda expression

        /// </summary>

        private Book FindUsingLambda(string title)

        {

            return this.Books.FirstOrDefault(b => b.Title == title);

        }

 

        public void Test()

        {

            Console.WriteLine("Found: {0}", this.FindUsingTraditionalLoop("Pro Silverlight 2 in C# 2008").Author);

            Console.WriteLine("Found: {0}", this.FindUsingLinq("Pro Silverlight 2 in C# 2008").Author);

            Console.WriteLine("Found: {0}", this.FindUsingLambda("Pro Silverlight 2 in C# 2008").Author);

        }

    }

}

As these examples show, you can save time reading and writing your code by using Lambda expressions to find items in a list.

For VB programmers, the syntax of the Lambda expression looks like this:

return Me.Books.FirstOrDefault(Function(b) b.Title = title)


Categories: ASP.Net | C# | VB
Posted by Williarob on Wednesday, January 06, 2010 8:10 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Mock a database repository using Moq

The concept of unit testing my code is still fairly new to me and was introduced when I started writing applications with the Microsoft MVC Framework in Visual Studio 2008.

Intimidated somewhat by the Moq library's heavy reliance on lambdas, my early tests used full Mock classes that I would write myself, and which implemented the same interface as my real database repositories. I'd only write the code for the methods I needed, all other methods would simply throw a "NotImplementedException". However, I quickly discovered that the problem with this approach is that whenever a new method was added to the interface, my test project would no longer build (since the new method was not implemented in my mock repository) and I would have to manually add a new method that threw another "NotImplementedException". After doing this for the 5th or 6th time I decided to face my fears and get to grips with using the Moq library instead. Here is a simple example, of how you can mock a database repository class using the Moq library.

Let's assume that your database contains a table called Product, and that either you or Linq, or LLBLGen, or something similar has created the following class to represent that table as an object in your class library:

The Product Class

namespace MoqRepositorySample

{

    using System;

 

    public class Product

    {

        public int ProductId { get; set; }

 

        public string Name { get; set; }

 

        public string Description { get; set; }

 

        public double Price { get; set; }

 

        public DateTime DateCreated { get; set; }

 

        public DateTime DateModified { get; set; }

    }

}

 

Your Product Repository class might implement an interface similar to the following, which offers basic database functionality such as retrieving a product by id, by name, fetching all products, and a save method that would handle inserting and updating products.

 

The IProductRepository Interface

 

namespace MoqRepositorySample

{

    using System.Collections.Generic;

 

    public interface IProductRepository

    {

        IList<Product> FindAll();

 

        Product FindByName(string productName);

 

        Product FindById(int productId);

 

        bool Save(Product target);

    }

}

 

The test class that follows demonstrates how to use Moq to set up a mock Products repository based on the interface above. The unit tests shown here focus primarily on testing the mock repository itself, rather than on testing how your application uses the repository, as they would in the real world.

 

Microsoft Unit Test Class

 

namespace TestProject1

{

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

 

    using Moq;

 

    using MoqRepositorySample;

 

    ///<summary>

    /// Summary description for UnitTest1

    ///</summary>

    [TestClass]

    public class UnitTest1

    {

        ///<summary>

        /// Constructor

        ///</summary>

        public UnitTest1()

        {

            // create some mock products to play with

            IList<Product> products = new List<Product>

                {

                    new Product { ProductId = 1, Name = "C# Unleashed", Description = "Short description here", Price = 49.99 },

                    new Product { ProductId = 2, Name = "ASP.Net Unleashed", Description = "Short description here", Price = 59.99 },

                    new Product { ProductId = 3, Name = "Silverlight Unleashed", Description = "Short description here", Price = 29.99 }

                };

 

            // Mock the Products Repository using Moq

            Mock<IProductRepository> mockProductRepository = new Mock<IProductRepository>();

 

            // Return all the products

            mockProductRepository.Setup(mr => mr.FindAll()).Returns(products);

 

            // return a product by Id

            mockProductRepository.Setup(mr => mr.FindById(It.IsAny<int>())).Returns((int i) => products.Where(x => x.ProductId == i).Single());

 

            // return a product by Name

            mockProductRepository.Setup(mr => mr.FindByName(It.IsAny<string>())).Returns((string s) => products.Where(x => x.Name == s).Single());

 

            // Allows us to test saving a product

            mockProductRepository.Setup(mr => mr.Save(It.IsAny<Product>())).Returns(

                (Product target) =>

                {

                    DateTime now = DateTime.Now;

 

                    if (target.ProductId.Equals(default(int)))

                    {

                        target.DateCreated = now;

                        target.DateModified = now;

                        target.ProductId = products.Count() + 1;

                        products.Add(target);

                    }

                    else

                    {

                        var original = products.Where(q => q.ProductId == target.ProductId).Single();

 

                        if (original == null)

                        {

                            return false;

                        }

 

                        original.Name = target.Name;

                        original.Price = target.Price;

                        original.Description = target.Description;

                        original.DateModified = now;

                    }

 

                    return true;

                });

 

            // Complete the setup of our Mock Product Repository

            this.MockProductsRepository = mockProductRepository.Object;

        }

 

        ///<summary>

        /// Gets or sets the test context which provides

        /// information about and functionality for the current test run.

        ///</summary>

        public TestContext TestContext { get; set; }

 

        ///<summary>

        /// Our Mock Products Repository for use in testing

        ///</summary>

        public readonly IProductRepository MockProductsRepository;

 

        ///<summary>

        /// Can we return a product By Id?

        ///</summary>

        [TestMethod]

        public void CanReturnProductById()

        {

            // Try finding a product by id

            Product testProduct = this.MockProductsRepository.FindById(2);

 

            Assert.IsNotNull(testProduct); // Test if null

            Assert.IsInstanceOfType(testProduct, typeof(Product)); // Test type

            Assert.AreEqual("ASP.Net Unleashed", testProduct.Name); // Verify it is the right product

        }

 

        ///<summary>

        /// Can we return a product By Name?

        ///</summary>

        [TestMethod]

        public void CanReturnProductByName()

        {

            // Try finding a product by Name

            Product testProduct = this.MockProductsRepository.FindByName("Silverlight Unleashed");

 

            Assert.IsNotNull(testProduct); // Test if null

            Assert.IsInstanceOfType(testProduct, typeof(Product)); // Test type

            Assert.AreEqual(3, testProduct.ProductId); // Verify it is the right product

        }

 

        ///<summary>

        /// Can we return all products?

        ///</summary>

        [TestMethod]

        public void CanReturnAllProducts()

        {

            // Try finding all products

            IList<Product> testProducts = this.MockProductsRepository.FindAll();

 

            Assert.IsNotNull(testProducts); // Test if null

            Assert.AreEqual(3, testProducts.Count); // Verify the correct Number

        }

 

        ///<summary>

        /// Can we insert a new product?

        ///</summary>

        [TestMethod]

        public void CanInsertProduct()

        {

            // Create a new product, not I do not supply an id

            Product newProduct = new Product

                { Name = "Pro C#", Description = "Short description here", Price = 39.99 };

 

            int productCount = this.MockProductsRepository.FindAll().Count;

            Assert.AreEqual(3, productCount); // Verify the expected Number pre-insert

 

            // try saving our new product

            this.MockProductsRepository.Save(newProduct);

 

            // demand a recount

            productCount = this.MockProductsRepository.FindAll().Count;

            Assert.AreEqual(4, productCount); // Verify the expected Number post-insert

 

            // verify that our new product has been saved

            Product testProduct = this.MockProductsRepository.FindByName("Pro C#");

            Assert.IsNotNull(testProduct); // Test if null

            Assert.IsInstanceOfType(testProduct, typeof(Product)); // Test type

            Assert.AreEqual(4, testProduct.ProductId); // Verify it has the expected productid

        }

 

        ///<summary>

        /// Can we update a prodict?

        ///</summary>

        [TestMethod]

        public void CanUpdateProduct()

        {

            // Find a product by id

            Product testProduct = this.MockProductsRepository.FindById(1);

 

            // Change one of its properties

            testProduct.Name = "C# 3.5 Unleashed";

 

            // Save our changes.

            this.MockProductsRepository.Save(testProduct);

 

            // Verify the change

            Assert.AreEqual("C# 3.5 Unleashed", this.MockProductsRepository.FindById(1).Name);

        }

    }

}

 

Download the Sample project and run the tests yourself:

MoqRepositorySample.zip (691.96 kb)


Categories: ASP.Net | C# | CodeProject | Moq | MVC | Unit Testing
Posted by Williarob on Tuesday, December 15, 2009 8:17 AM
Permalink | Comments (0) | Post RSSRSS comment feed