Archives

.NET

Get Started with Amazon EC2, Run Your .NET MVC3 (Razor) Site in the Cloud with Linux Mono

I’ve recently been getting pretty excited about Amazon EC2 once I realized you can setup a micro linux server for free to learn and test out their services. I know, a couple years behind the curve right? I’ve just never really looked into it. Now that I’m familiarizing myself with the whole cloud concept with Amazon, it’s really cool to think of small business growth potential in the web market without the restraint of building a heavy infrastructure upfront. I realize after reading some other articles that in the long run, it is probably more expensive than hosting your own solution. But as a startup, it still seems to me that it’s a pretty good deal at least for short term events or just taking their infrastructure out for a spin to test some ideas. Pay for what you use; that’s Amazon’s big pitch with their Amazon Web Services (AWS).

So if you want to play around with AWS for free, you have to roll Linux. If you want to host ASP.NET MVC3 on AWS Linux, there’s a few steps you need to take. For this post, I’ll talk about some of the AWS services that can get you started along with how to install and configure your environment for hosting ASP.NET MVC3 applications with Linux and Mono.

Read more →

Running Mono MVC3 Razor and Solving the InvalidOperationException of ‘The view ‘name’ or its master was not found’

I’ve been fighting my system the past day or so trying to figure out why all the views in one of my controllers return this silly exception. The file indeed exists. Permissions are correct. Other controller views works. Other non-view (like json or file content) actions work within this controller. This works on Windows but not in Mono. Why doesn’t it work? To find this, I ended up using my source control to identify the commit that caused the break. Then line by line, I started toggling the changes within that commit until I found it.

System.InvalidOperationException: The view ‘Index’ or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Areas/Manage/Views/Dashboard/Index.aspx ~/Areas/Manage/Views/Dashboard/Index.ascx ~/Areas/Manage/Views/Shared/Index.aspx ~/Areas/Manage/Views/Shared/Index.ascx ~/Views/Dashboard/Index.aspx ~/Views/Dashboard/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Areas/Manage/Views/Dashboard/Index.cshtml ~/Areas/Manage/Views/Dashboard/Index.vbhtml ~/Areas/Manage/Views/Shared/Index.cshtml ~/Areas/Manage/Views/Shared/Index.vbhtml ~/Views/Dashboard/Index.cshtml ~/Views/Dashboard/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml

I’m saying this purely by observation. But MVC3 (or maybe the Mono runtime) seems to group compiled view code by controller. I was able to figure out that ALL ViewResult actions failed under this specific controller. It turns out I had one character out of place in my Razor syntax. With that one syntactical flaw, all the views under that controller could not compile and the only reasonable exception that could be thrown was the one above. The windows runtime seems to ignore this (maybe because it compiles them individually; I don’t know). In my case, I was trying to render one view when another view was causing the problem.

So if you see this exception and everything seems to be correct, start sifting through your recent View changes to see if anything you did broke the view syntax. So you can get a little laugh, here’s my goof up:

Extra @ symbol not needed when continuing a code block

The indicated ‘@’ symbol is not necessary when continuing additional statements.

Enjoy!

Install Mono 2.10.8 from Source on Ubuntu with a Bash Script

NOTE: Badgerports.org is now current w/ 2.10.5. This bash script is more useful when installing parallel Mono, when a new version is released and not yet published on badgerports, or if you simply want to compile it yourself from source. I highly recommend trying Badgerports first.

I just updated the 2.10 script to use the latest 2.10.8 Mono install.

Install

mkdir mono-2.10
cd mono-2.10
wget --no-check-certificate https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/ubuntu/install_mono-2.10.sh
chmod 755 install_mono-2.10.sh
./install_mono-2.10.sh

 

Enjoy!

Update 12/30/2011 – Updated Mono 2.10.6 to 2.10.8

Update 10/16/2011 – Updated Mono 2.10.5 to 2.10.6

Update 8/31/2011 – Updated Mono 2.10.4 to 2.10.5

Update 8/11/2011 – Updated Mono 2.10.3 to 2.10.4 for minor update.

July 2011 – My Application Tool Belt for .NET Web Applications

I really like to talk about tools I use to the people I work with. I especially like to push tools into environments where I think could be an improvement to their work flow.  So here it is, my current tool belt. I primarily focus on web applications, so there’s a bias toward those tools.

Read more →

Get MVC3 Razor Running on Mono

Updated 10-18-2011 – added some more common issues

I dug around a little to figure this out. Hopefully this will clarify all the steps required to get an MVC 3 Razor site running on Mono. The main pitfall I ran into was gathering the additional dependencies and excluding Microsoft.Web.Infrastructure assembly from my deployment. Overall though it’s not too difficult to deploy your MVC3 app.

In Summary:

  1. Install Mono
  2. BIN Deploy Website
  3. Configure Apache (or Xsp environment)
  4. Fire Up Your Server
  5. Errors You Might See

Read more →

Install Mono 2.10.2 and MonoDevelop 2.6 Beta 3 on Ubuntu With a Bash Script

After @migueldeicaza announced MonoDevelop Beta 3 a few days ago, I updated my scripts to install the new version. I’ve also included an upgrade script for those of you who used my Beta 2 script. The upgrade will uninstall the three Beta 2 projects and then download, compile and install the three new ones. I’ve only tested this so far on Ubuntu Natty (11.04), so let me know here if you have any problems with other versions.

 

 

Read more →

Install MonoDevelop 2.4.2 or 2.6 Beta on Ubuntu & Fedora

MonoDevelop 2.6 betaAlright!  You asked for it, so here it is!  Here’s a bash script that will download, compile and install MonoDevelop. I can’t say it’s a perfect solution, but it does work and it’s relatively clean. The main difference between this install and platform packages is that it will install to a prefix using the parallel environment recommendations. So launching it requires a little environment setup, which the script will provide for you. Otherwise, it should work just the same.

 

Read more →

Mono 2.10 Install Script for Ubuntu & Fedora

Update 10/4/2011 – I’m starting to consolidate the major version posts. This script refers to 2.10.1; however, it installs 2.10 latest. Refer to the current 2.10 post for more info.

I just updated the bash install script for Mono 2.10. Thanks to @Marv for the Ubuntu script and heads up that 2.10 was released. This script is mainly intended for use with linux/apache web servers. It will install Mono, GTK, OpenGDI, XSP, and Mod_Mono. For Mono Develop, read through the Building Mono Develop page [mono-project.com], which will require a few additional packages not included in this script.

To install for Ubuntu:

wget --no-check-certificate https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/ubuntu/install_mono-2.10.sh
chmod 755 install_mono-2.10.sh
./install_mono-2.10.sh

 

To install for Fedora:

yum install wget
wget --no-check-certificate https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/fedora/install_mono-2.10.sh
chmod 755 install_mono-2.10.sh
./install_mono-2.10.sh

 

Again, if setting up a web server, be sure to set the VirtualHost environment up correctly with PATH & LD_LIBRARY_PATH. (here, IOMAP is optional) See configuring virtual hosts. The following line should be included in your VirtualHost configuration.

MonoSetEnv site_name MONO_IOMAP=all;PATH=/opt/mono-2.10/bin:$PATH;LD_LIBRARY_PATH=/opt/mono-2.10/lib:$LD_LIBRARY_PATH;

 

Update – March 15, 2011

I’m refactoring some stuff, so bare with me. Today I’m introducing a totally re-written script that will hopefully make it easier to adapt to updates in the future. So far, I’ve treated these bash scripts like I do Windows batch files. But I took some time and tried to learn a little more about writing bash scripts.  The new scripts will stop if any errors are encountered. This is good and bad.  Good because now you know something went wrong, and the errors are much easier to find. Bad (maybe) because now you know something might not have worked. ;) Anyway, I hope the changes will help everyone.

I’ve changed all the prefixes to use the major/minor version. So 2.10.1, 2.10.2 etc, will use prefix /opt/mono-2.10. You’ll always have the option to change that if you wish to use a different prefix. There’s a single variable in the beginning of the script that sets the prefix.

Finally, there does seem to be a problem at the end of installing xsp. However, everything seems to go in okay.

Good luck!

 

UPDATE – March 8, 2011

Mono 2.10.1 was released Feb 25th. I updated the install script for 2.10 to use the Mono 2.10.1 package. (That was the only package updated in this version). I’ve also included an experimental upgrade script for 2.10 to 2.10.1 here. (fedora vs ubuntu use the same script). Download it to the same location of your script for 2.10 and run it.  It will uninstall just mono-2.10 and then download, compile, and re-install the new mono-2.10.1.

Enjoy!

Mono 2.8.2 Script Updated for Ubuntu and Fedora

 

I just committed the script for 2.8.2. This was literally a text replacement of 2.8.1. However, this one seems to work with asp.net membership out of the box; so that’s good. Anyways you can find these scripts at github.com or use the bash sequence below.

I ran through these scripts on Ubuntu 10.10 and Fedora 14 fresh installs.  I would discourage setting your default environment path to use this version and stick to using it just for Asp.Net. You can set the MonoPath in your Apache virtual host config to use this one specifically.  It will install to /opt/mono-2.8.2

Comment if you have any questions or problems.

Ubuntu

wget --no-check-certificate https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/ubuntu/install_mono-2.8.2.sh
chmod 755 install_mono-2.8.2.sh
./install_mono-2.8.2.sh

 

Fedora

wget --no-check-certificate https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/fedora/install_mono-2.8.2.sh
chmod 755 install_mono-2.8.2.sh
./install_mono-2.8.2.sh

 

Update 1/30/2011

Be sure and set the environment variables in the virtual host config to use the correct prefix. This script will install to /opt/mono-2.8.2;  So modify the MonoSetEnv line in your virtual host config: 

MonoSetEnv appName MONO_IOMAP=all;LD_LIBRARY_PATH=/opt/mono-2.8.2/lib:$LD_LIBRARY_PATH;PATH=/opt/mono-2.8.2/bin:$PATH

MONO_IOMAP=all is optional; but on by default usually. The important part is to include the other two so mod_mono knows how to find this 2.8.2 install.  Likewise, if you're running console or GUI apps w/ this version; you'll need to set your local environment to use the modified LD_LIBRARY_PATH and PATH as well. See http://www.mono-project.com/Parallel_Mono_Environments for more info. 

 

Update 2/20/2011

Mono 2.10 has been released and I added a new install script.

Using C# to Generate Random String with Character Filter

I haven’t posted anything for awhile, and today while toying around with a few different things I decided to tweak my random string generator.  So I thought I’d share.

So here we have two methods; one for Unicode and one for ASCII. My thought was that when generating a simple password for US English keyboards, I’d just use a simple ASCII generator using characters available on the keyboard. For for the rest of you folks who don’t have a US English keyboard, maybe there are some options to generate a random string with a custom range filter. 

public static string GetRandomUnicodeString(int length, int maxValue, Predicate<int> valueFilter)
{
    byte[] seedBuff = new byte[4];
    byte[] charBuff;

    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    rng.GetBytes(seedBuff); // The array is now filled with cryptographically strong random bytes.

    using (MemoryStream ms = new MemoryStream())
    {
        using (StreamWriter sw = new StreamWriter(ms, new UTF8Encoding(false, false)))
        {
            var random = new Random(BitConverter.ToInt32(seedBuff, 0));

            for (int i = 0; i < length; i++)
            {
                int temp = random.Next(maxValue); //should we cap? 

                while (!valueFilter(temp))
                    temp = random.Next(maxValue);

                sw.Write((char)temp);
            }
        }
        charBuff = ms.ToArray();
    }
    return new System.Text.UTF8Encoding(false, false).GetString(charBuff);
}

public static string GetRandomASCIIString(int length)
{
    return GetRandomUnicodeString(length, 0x7E, o => o >= 0x21 && o <= 0x7E);
    //return GetRandomUnicodeString(length, 126, o => o >= 33 && o <= 126); //could use integers instead
}

So when using the Unicode method, you have to specify a max value; otherwise, the random generator will choke if you want a small range of numbers when randomizing a pool of 65,535 values. So the cap will help performance. The filter expression should filter out the random results so you only get the characters you want.

The ASCII method is a pretty simple example of how this works.  I want a max value of 126, and I want characters in the range of 33 through 126. I used hex (because the Windows character map uses hex).

Hope you all find it useful.  Let me know how it goes.