.NET development on MacOS
Originally published on conwy.co
Hats off to Microsoft for releasing the new Visual Studio for Mac.
Unfortunately this does not, at present and in all cases, enable us to properly develop .NET web applications on a Mac. Many projects can only be built on a Windows operating system. (E.g. projects requiring libraries that can’t easily be ported, such as the SyncFusion Excel libraries, which utilise Microsoft Office InterOp).
I managed to get around this on a recent project by hosting my .NET stack in VirtualBox, while using my MacOS for development using my favourite Mac text editor and Terminal. The following are some steps you can take to reproduce a similar setup on your Mac.
Firstly, let’s set up a VM to run your solution:
- Download the free Windows image.
- Create a VirtualBox VM. (Choose a Dynamic VHD and make it 80 GB. You’ll need the capacity. Also, to prevent freeze-ups, reduce max CPU consumption to 80%. Finally, for performance, increase memory to at least 50% of total memory, assuming 8 GB of total machine memory.)
- Turn off ‘Autocapture Keystokes’ (VirtualBox Preferences → Input). This makes it easier to CMD-tab between your VM and your text editor, e.g. when running MSBuild.
- Install Windows. Make sure you set a password on your user account. This will help when it comes time to create a network share.
- Install VirtualBox Extensions.
- Configure display to the lowest resolution (Display settings) for performance. (You might also want to apply a faster-rendering theme to Windows. I found Classic Shell from IvoSoft did this quite nicely.)
- Install IIS.
- Download and install Visual Studio. (At the time of writing, a free community edition of Visual Studio can be downloaded.) Keep the installation as minimal install possible, i.e. just the Web Development component.
- Install SQL Server Express. (At the time of writing, the Express edition can be downloaded for free.) Again, keep it minimal. Just the engine and Management Console.
- Using Visual Studio, check out your solution into a project folder on your VM.
- Open your solution in Visual Studio, build. Then go into your project properties and set it up in ‘Local IIS’, in the root (delete the text after ‘localhost/’).
- Open IIS and check that everything is working and your solution is being served locally.
Secondly, let’s use Resilio Sync to keep your source files synced between your Mac host and your Windows VM. This will enable you to rapidly edit → build, using the VM for build only, but being able to do everything else (e.g. use version control tools) in the host.
- Download and install Resilio Sync into your VM.
- Add the folder where your source code is kept in you VM to Resilio Sync.
- Likewise, download and install Resilio Sync on your Mac host.
- Use the 'Enter a key or link' feature to link to the folder on your VM.
Once everything has been copied over, you'll have a constantly (and, in my experience, rapidly) synced copy of your source folder on your Mac host. Since Resilio copies all files, byte-for-byte, everything including your version-control repository (e.g.
./git folder) will be copied. This means you can work with version control tools as if you were directly connected to the VM. This setup gave me the best experience, with no perceived difference between editing on the host vs. editing in the VM.
Thirdly, let’s connect to your running website from your Mac host’s browser, so you can rapidly edit → build → refresh:
- Disable Windows Firewall. (Control Panel → System and Security → Windows Firewall → Turn off)
- Find out the computer name in your VM. (Control Panel → System, look under Computer name)
- Now, in your Mac host’s browser, navigate to:
http://<computer-name>.local/. This should serve up your solution hosted on IIS.
This should give you a reasonably productive setup, enabling you to switch between your Mac text editor, Mac web browser and VM running a Build command-line.
Coming soon: I even managed to get a Mac SQL Server client working with this set-up. I will add instructions for this in the near future.