Target Mono from Visual Studio

Targetting Mono in Visual Studio is possible. This way your assemblies are build using the Mono framework (which should be .NET compatible). Change the target to the Mono profile to get immediate feedback of incompatible references and other Mono things. This can be done in the Properties > Application tab of your project.

To get this working you’ll need to register mono as a profile, just like the client profiles from Microsoft.

  1. Copy the contents of C:\Program Files\Mono-2.10.8\lib\mono\4.0 to
    C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Mono.
  2. Create the folder RedistList and add to the new folder a file called FrameworkList.xml with the following content:
    <?xml version="1.0" encoding="UTF-8"?>
    <FileList ToolsVersion="4.0" RuntimeVersion="4.0" Name="Mono 2.10.8 Profile" Redist="Mono_2.10.8"> 
    </FileList>
    
  3. Add the following key to your registry
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319\SKUs\.NETFramework,Version=v4.0,Profile=Mono
    

Below are links to the files created / used. The Registration only contains the FrameworkList.xml file, two registry files and a readme.txt. The Complete adds the Mono files which makes the file big(ger).
mono 2.10.8 profile registration only (12Kb)
mono 2.10.8 profile complete (13Mb)

References

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , , , , , . Bookmark the permalink.

20 Responses to Target Mono from Visual Studio

  1. erictummers says:

    Use symbolic links in stead op copy of step 1 to save some space. http://en.wikipedia.org/wiki/NTFS_symbolic_link

  2. Grant says:

    This did not work for me. (Note: I tried it for Mono 3.2.3.)

    When I attempt to load my solution after choosing “Mono 3.2.3 Profile”, I get “This application requires […] .NetFramework,Version=v4.0,Profile=Mono”.

    I double- and triple-checked what I did. My registry key appears correct. I copied my files as directed.

    Any idea?

  3. Yura says:

    Hi, thanks for this manual i do all steps, select Mono in new project settings, write simple code:
    Gtk.Application.Init();
    Window window = new Window(“helloworld”);
    window.Show();
    Gtk.Application.Run();
    and have exception:
    The type initializer for ‘Gtk.Application’ threw an exception.
    Can you help me?
    VS2010 + XP 64

    • erictummers says:

      Yura, I’m not a gui developer on the mono framework, but I’ve got the same exception you have. I fixed it by changing the Debug settings in the project properties.
      Start action: start external program = C:\Program Files (x86)\Mono-3.2.3\bin\mono.exe
      Start options: command line arguments = ‘name of your assembly’
      working directory = ‘build location of you assembly’
      Now I’m lookin at the “helloworld” window.

  4. gregsdennis says:

    Eric, I have a client who is using Xamarin.iOS, which apparently requires .Net 3.5. What registry key would I need to add a 3.5 Mono project?

  5. Chris Yeates says:

    Hi thanks for the info, it has helped me get VS working with Mono although now I am stuck and was wondering if you could help?

    I’ve only been an owner of a Raspberry Pi for a short time and do not have a background in Linux. My background is primarily in embedded C but more recently C# and .NET.

    I have tried to build an application that sends a SOAP request to a webservice and then processes the reply in Visual Studio 2013. I have tried the 3 following approaches with no success:

    1) Just built and tested the application in VS as normal and then copied to my PI running the executable with mono.
    2) Set VS project properties to target Mono rather than .NET, built and tested the application and then copied to my PI running the executable with mono.

    Both of the above methods generated the exceptions shown below in A.

    3) Within VS, I changed the properties of the ServiceModel reference to copy locally and moved the resultant dll with the executable to my PI then running the executable with mono.

    This 3rd approach generated the exceptions as shown in B.

    A typical project for me would be controlling HW and therefore not utilising ASP.NET where I have very little experience. The idea of moving to the PI from PIC based designs / Arduinos and so on was to start to play with more web oriented projects hence this attempt to communicate over HTTPS with an existing service.

    I would really appreciate it if anyone could explain what may be happening.

    Regard,

    Christian

    Exceptions A
    Unhandled Exception:
    System.IO.FileNotFoundException: Could not load file or assembly ‘System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ or one of its dependencies.
    File name: ‘System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’
    at pitest.Program.Main (System.String[] args) [0x00000] in :0
    [ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly ‘System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ or one of its dependencies.
    File name: ‘System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’
    at pitest.Program.Main (System.String[] args) [0x00000] in :0

    Exceptions B

    Unhandled Exception:
    System.TypeLoadException: Could not load type ‘System.ServiceModel.BasicHttpBinding’ from assembly ‘System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.
    at pitest.Program.Main (System.String[] args) [0x00000] in :0
    [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could not load type ‘System.ServiceModel.BasicHttpBinding’ from assembly ‘System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.
    at pitest.Program.Main (System.String[] args) [0x00000] in :0

    • erictummers says:

      Hi Christian,
      My first guess would be the wrong version of Mono installed. Can you run a simple “hello world” compiled in VS on your raspberry?
      I don’t have a raspberry pi but can test this scenario on my synology NAS as it supports some version of mono.
      Eric

      • Chris Yeates says:

        Hi Eric,

        Thanks for such a fast reply! The version of Mono I am using is 3.2.8. That’s just what I ended up with when I installed it on the Pi yesterday.

        I have successfully tried a Hello World and a basic input output console application that worked fine on the PI. As I have recently had the need to communicate with a webservice from .NET, I decided to give the same code a go with mono on the PI hence the issue.

        I really wanted to get the confidence that using mono was the right choice for me for PI coding based on having some .NET experience.

        Your help is much appreciated!

        Regards,

        Chris

      • erictummers says:

        I’ve tried my Nas but it only supports mono 2.11. So no repro for me …
        On Stackoverflow I found this http://stackoverflow.com/questions/2210938/mono-could-not-load-file-or-assembly
        Tried adjusting the MONO_LOG_LEVEL / MONO_PATH?

    • M says:

      Hi, Christian. I have a vague memory of seeing a similar exception, but the filename shown in my experience was not System.ServiceModel or System.ServiceModel.BasicHttpBinding. I recall solving this issue by running “sudo apt-get install mono-complete” rather than “sudo apt-get install mono-runtime”. Mono-runtime appears to install only the basic stuff, but mono-complete will give you ALL the assemblies available in dotNet (or so I think). Could you try giving “sudo apt-get install mono-complete” a shot?

  6. Pingback: Mono on Synology NAS | .NET Development by Eric

  7. Ralph Bloch says:

    I am trying to migrate a pc application, I wrote in C# to a Mac environment using Mono. I followed your instructions step by step, except that the current version of Mono is now 4.01. Visual Studio 2012 told me that it was targeting to Mono 4.01, but the compiled version was identical to that targeted at .Net 4.0. In fact, the byte count of the .exe file was identical. What did I do wrong?

    • erictummers says:

      Did the application work on your Mac?
      Checking for incompatible code is my only reason to use the compiler from the mono framework. Both the .NET compiler and mono compiler should result in the same assembly.

  8. Dose it work with mono 4.6.1 (Dot net 4.6.1)?

    • erictummers says:

      Microsoft has done lots of work since 2012. I think this will still work but you’ll have to try it for yourself since I’ve moved on. Please report back with your experience.

  9. Pingback: Is it possible to remotely debug a Mono console app from Visual Studio? – program faq

  10. Irwene says:

    Just for information, still works with Mono 5.4.1 and .NetFramework 4.7, you just need to tweak a few values

Leave a reply to erictummers Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.