10 April 2012

C# and Mono on a Mac


[UPDATED] Since posting this I've updated it to include using MVC2.

I'm a long time C# developer on Windows but I was interested to see if it's possible to develop on a Mac using Mono. Here are my notes on the setup:

Prerequisites
  1. Mac 32 or 64 bithttp://support.apple.com/kb/HT3696
  2. Java SDK: included in Mac OsX
  3. Monohttp://www.go-mono.com/mono-downloads/download.html
  4. MonoDevelop (Xamarin studio): http://monodevelop.com/Download
  5. A development IDE: e.g. Mono Develop: http://monodevelop.com/Download
    or Eclipse: http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigor 
  6. Check  MD5 checksums:
    1. Open Terminal: Applications, Utilities, Terminal
    2. Type: md5 downloads/eclipse[version and build name].tar.gz
  7. Install XCode (so you can compile mod mono): Either from your install disk or from http://developer.apple.com/xcode/ NB: To download you'll need Lion (10.7+) and to sign up as a developer first.
  8. Install the XCode developer tools: 
    In Terminal: xcode-select --install
    Older versions: Open XCode then go to Preferences, Downloads, and install Command Line Tools.
Make a directory for your Mono projects
I'm going to use /Users/[username]/Mono/
  1. mkdir /Users/[username]/Mono
    Also make a TestMono directory under this directory
    mkdir /Users/[username]/Mono/TestMono
Create a Test Project
  1. Open MonoDevelop and create a new C# Web MVC2 project, I called it TestMono.
    Note: The default location for projects is [user folder]/Projects which I found a bit generic so I changed it to [user folder]/Mono.
  2. Run it to test (Alt, Command key and Enter). It should load http://127.0.0.1:8080/ and show an MVC generated page.

To run Your Application under Apache
For external testing e.g. from your mobile to the Mac you'll need Apache or equivalent:

Open Terminal and Switch to root
  1. Terminal.app
  2. sudo su -
Activate in-built Apache Web server (and Mac firewall)
UPDATE: see separate post
  1. If you are using Mountain Lion+ web sharing has been removed so activate apache via the command line:
    1. Start Apache: /usr/sbin/apachectl start
    If you are using an earlier Mac OS you can use the process below:
    1. Apple, System Preferences
    2. Sharing, tick Web Sharing
  2. Mac Firewall
    Apple, System Preferences, Security, activate Firewall (ensure Web Sharing allowed).
  3. Check it runs: http://localhost
Get Mod mono
The connector between Mono and your web server
  1. Download and compile it. In Terminal:
    • Make directory for the files:
      mkdir ~/modmono
    • Change to that directory:
      cd ~/modmono
    • Make a link to the mono framework command:
      sudo ln -s /Library/Frameworks/Mono.framework/Commands/pkg-config /usr/bin/pkg-config
    • Download the source:
      curl -O http://download.mono-project.com/sources/mod_mono/mod_mono-3.8.tar.gz
    • Extract it:
      tar xzf mod_mono-3.8.tar.gz
    • Change into that directory:
      cd mod_mono-2.10
    • Run the configure script:
      ./configure --prefix=/usr
    • Make the source:
      make
      sudo make install
    • Copy the conf file to the apache2 folder:
      sudo cp /etc/apache2/mod_mono.conf /etc/apache2/other
  2. Edit /etc/apache2/httpd.conf to tie the two together
    Copy the original:
    cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.bak

    Insert the reference to mod_mono.conf:
    vi /etc/apache2/httpd.conf"
    Press Shift G to go to the end, i to enter insert mode and paste in the following at the end of the file:
    # mod_mono_configuration                                                
    Include /etc/apache2/mod_mono.conf
    MonoServerPath /Library/Frameworks/Mono.framework/Versions/Current/bin/mod-mono-server4
    AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd
    MonoAutoApplication disabled
Add an Apache configuration file for mono
  1. In Terminal:
    1. vi /private/etc/apache2/other/mono.conf
    2. Press i to begin insert then paste (replace [username] with your username:

      AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd
      MonoAutoApplication disabled

      MonoServerPath /Library/Frameworks/Mono.framework/Commands/mod-mono-server4

      Alias /testmono "/Users/[username]/Mono/TestMono/"
      <Directory "/Users/[username]/Mono/TestMono/">
          Options Indexes FollowSymLinks MultiViews
          AllowOverride None
          Order allow,deny
          Allow from all
      </Directory>

      Alias /testmono2 "/Users/[username]/Mono/TestMono2/"<Directory "/Users/[username]/Mono/TestMono2/">
          Options Indexes FollowSymLinks MultiViews
          AllowOverride None
          Order allow,deny
          Allow from all
      </Directory>

      AddMonoApplications default "/testmono:/Users/[username]/Mono/TestMono/,/testmono2:/Users/[username]/Mono/TestMono2/"
      <Location /testmono>
       SetHandler mono
      </Location>
      <Location /testmono2>
       SetHandler mono
      </Location>
    3. Press Esc, :wq Enter to escape insert mode, save the file (:w) and quit (:q)
  2. Create a test index.aspx
    echo "<center>mod_mono is working:<%=
    System.DateTime.Now.ToString()%></center>" > /Users/[username]/Mono/TestMono/index.aspx
  3. Restart Apache: sudo /usr/sbin/apachectl restart
  4. Test it:http://localhost/testmono/
    http://localhost/mvc2/
Thanks to Michael Buen:
http://www.ienablemuch.com/2010/10/aspnet-on-mac-os-x-snow-leopard-at-one.html
http://www.ienablemuch.com/2010/10/troubleshooting-aspnet-mvc-4-app-in.html

MVC3 with Razor in Xamarin Studio 

  1. Create new MVC3 project
    Notice broken references to System.Web.Helpers and System.Web.Webpages
  2. Add Nuget:
    1. Xamarin Add in Manager
    2. Gallery tab, Repositoy, select Manage Repositories
    3. Add unofficial Nuget Add-in http://mrward.github.com/monodevelop-nuget-addin-repository/4.0/main.mrep
    4. Install the NugetPackage from the new repository: IDE extensions, NuGet
  3. Manage Project Nuget Packages
  4. Add Microsoft ASPNet Web Pages 2

Troubleshooting
  • Apache logs: vi /var/log/apache2/error_log
  • Mac Console: find using the search in top right: this will list all errors so look for those starting org.apache...
  • Lion: after upgrading to Lion I found I had an error in Console:
    org.apache.httpd: httpd: Syntax error on line 499 of /private/etc/apache2/httpd.conf: Syntax error on line 8 of /private/etc/apache2/other/mod_mono.conf: Cannot load /usr/libexec/apache2/mod_mono.so into server: dlopen(/usr/libexec/apache2/mod_mono.so, 10): image not found. This post on stackoverflow.com helped me fix it.

No comments:

Post a Comment