Asp.Net web services on FreeBSD and Apache using Mono

Asp.Net is cross platform using mono. Novell SUSE and Microsoft and others companies are dedicated to making .NET Framework a cross platform solution. Asp.Net web services are limited to running on windows, but can also run on other platforms, such as FreeBSD.

FreeBSD has ports for mono and mod_mono and can easily run web services build with Asp.Net. Here is a tutorial to make this happen.

Preparing a FreeBSD system for Asp.Net on Apache using Mono

Information

A good place to start reading is the getting started page on the mono project’s web site.:
http://mono-project.com/Start

This has the resources you need for most things, however, the documentation is designed mostly for SUSE Linux, so be prepared for slight differences on FreeBSD.

Step 1 – Install Apache

Install Apache on FreeBSD as follows.

  1. Change to the ports directory for Apache and run make install.
    #
    #
    cd /usr/ports/www/apache22
    make install
  2. Configure Apache to load on start up.
    # echo ‘apache22_enable=”YES”‘ >> /etc/rc.conf
  3. Leave Apache stopped. We will start it later.

Step 2 – Install mod_mono

Installing mod_mono will also install mono and xsp. Install mod_mono as follows.

  1. Change to the ports directory for Apache and run make install.
    #
    #
    cd /usr/ports/mod_mono
    make install
  2. Because on FreeBSD these packages work a little differently, you don’t need to do some of the steps listed on the mono website because they are done for you. Here are some key paths and differences in the packages on FreeBSD that you should know about.
    • The mod_mono port installs a mod_mono.conf file to /usr/local/etc/apache22/Includes which is automatically includes in the httpd.conf, so you don’t have to add an include manually.
    • The xsp port adds sample ASP.NET web services to /usr/local/lib/xsp.
    • The apache root directory is /usr/local/www/apache22/data
  3. Here are the contents of the mod_mono.conf file. Notice that this Apache include file loads a couple modules, adds a bunch of types, and adds a few files as DirectoryIndex options.
    # mod_mono.conf
    
    # Achtung! This file may be overwritten
    # Use 'include mod_mono.conf' from other configuration file
    # to load mod_mono module.
    
    <IfModule !mod_mono.c>
        LoadModule mono_module /usr/local/libexec/apache22/mod_mono.so
    </IfModule>
    
    <IfModule mod_headers.c>
        Header set X-Powered-By "Mono"
    </IfModule>
    
    AddType application/x-asp-net .aspx
    AddType application/x-asp-net .asmx
    AddType application/x-asp-net .ashx
    AddType application/x-asp-net .asax
    AddType application/x-asp-net .ascx
    AddType application/x-asp-net .soap
    AddType application/x-asp-net .rem
    AddType application/x-asp-net .axd
    AddType application/x-asp-net .cs
    AddType application/x-asp-net .vb
    AddType application/x-asp-net .master
    AddType application/x-asp-net .sitemap
    AddType application/x-asp-net .resources
    AddType application/x-asp-net .skin
    AddType application/x-asp-net .browser
    AddType application/x-asp-net .webinfo
    AddType application/x-asp-net .resx
    AddType application/x-asp-net .licx
    AddType application/x-asp-net .csproj
    AddType application/x-asp-net .vbproj
    AddType application/x-asp-net .config
    AddType application/x-asp-net .Config
    AddType application/x-asp-net .dll
    DirectoryIndex index.aspx
    DirectoryIndex Default.aspx
    DirectoryIndex default.aspx
    

Step 3 – Add the “test” web service to the Apache root directory

In /usr/local/lib/xsp, added by the mod_mono port, is a “test” folder that contains sample web services. Ther

  1. Copy /usr/local/lib/xsp/test to /usr/local/www/apache22/data.
    # cp -fR /usr/local/lib/xsp/test /usr/local/www/apache22/data/

Step 4 – Start Apache

  1. Start Apache with this command:
    # service apache22 start

You should now have Apache configured to run Asp.net.

Step 5 – Open the “test” folder in a browser

  1. Open your favorite browser on a workstation that has access to the server you just finished installing.
  2. Go to the URL of your server. For example, the url for my test server is this:
    http://192.168.0.43/test
  3. Browse around and test the web services.

Step 6 – Install Libraries as needed

    1. Determine if you need additional libraries.

With the bare minimum installed, you can almost guarantee that a web service is going to require a library you do not have installed. In fact, clicking on the second link in the “test” site, code-render.aspx, shows us this error.

Server Error in '/test' Application

gdiplus.dll

Description: HTTP 500. Error processing request.

Stack Trace:

System.DllNotFoundException: gdiplus.dll
  at (wrapper managed-to-native) System.Drawing.GDIPlus:GdiplusStartup (ulong&,System.Drawing.GdiplusStartupInput&,System.Drawing.GdiplusStartupOutput&)
  at System.Drawing.GDIPlus..cctor () [0x00000] in <filename unknown>:0

Version information: Mono Runtime Version: 2.6.7 (tarball Tue Mar 1 06:10:28 MST 2011); ASP.NET Version: 2.0.50727.1433

This library can be found and installed.

  1. Go to the directory for the port and run make install.
    #
    #
    cd /usr/ports/x11-toolkits/libgdiplus
    make BATCH=yes install

    This has some Xorg dependencies so compiling it could take a while. Notice the BATCH=yes parameter passed to make above. This will prevent any prompts and accept the defaults for every port this command compiles.

6 Comments

  1. John Garrett says:

    This works. Thanks!

  2. Stin says:

    Just now I run an ASP.NET webpage,
    I write it by VB.NET code...
    OH! It's always appear "can not find this page..."
    WHY?
    Does it only support C#.NET?
    Is there any possible to let it run the VB.NET page?
    Thank you.

  3. Stin says:

    Thank you for this post!
    YOU SAVE MY LIFE!!
    It's Works Perfect!
    Thanks again!

  4. Roman Zaks says:

    Rhyous,

    This is a great article. After setting up the initial system I have encountered a problem that I can't seem to solve.
    I am getting the following - "Unable to connect to any of the specified MySQL hosts.". The same exact code seems to work fine under mod_mono on openSuse.
    Do you have any suggestions where to start digging?

    Thanks.
    Roman

  5. Djóni says:

    Thank you for this post. I should have read this post to the end, instead of googling for an answer to the missing libgdiplus when i encountered it.
    regards
    Djóni

Leave a Reply

How to post code in comments?