StructureMap.MVC5 Errors in Default Visual Studio 2013 MVC WebApp

StructureMap is the leading Dependency Injection/Inversion of Control (IoC) library for .Net development. So of course as I started a brand new project it was one of the very first NuGet packages I installed.

Just as an FYI… if you’re doing ASP.NET MVC development, you’ll want to install the newer StructureMap.MVC5 package, and if you’re adding Web API functionality to your project, you’ll want to use StructureMap.WebApi2 – which will install StructureMap.MVC5 as a dependency.

I had created a Web Application with authentication using the ‘Individual User Accounts’ option enabled. I ran the app prior to installing StructureMap just to make sure it compiled and all was good:

structuremap_01

I then clicked around the pages, checking everything was ok. It all worked fine. I then right clicked my solution node in the solution explorer and selected ‘Manage NuGet Packages for Solution…’

structuremap_02

On the left side of the Package Manager dialog click and select to view ‘Online’ packages. In the upper right corner type into the search box ‘StructureMap.MVC5′ and hit enter to start searching. You should see StructureMap.MVC5 come up in the search results in the middle column.

structuremap_03

Now click the ‘Install’ button and you will see the dialog prompting which projects in the solution to install the package. Select the ones you want and then click the ‘OK’ button.

structuremap_04

 

 

 

 

 

 

 

 

You will see a progress window appear as NuGet installs the package and a new green tick will appear next to the StructureMap.MVC5 item in the search results.

structuremap_05

structuremap_06

Now recompile and run the webapp again. This time click on the ‘Register’ link and see what happens.

structuremap_07

structuremap_08

You will see an unusual error message displayed in Visual Studio:

An exception of type ‘Microsoft.Practices.ServiceLocation.ActivationException’ occurred in Microsoft.Practices.ServiceLocation.dll but was not handled in user code

Click the link that says ‘Copy exception detail to the clipboard’ and then paste it into your favourte text editor. This will give us a hint as to what is going wrong.

structuremap_10

You can see that the error occurred while trying to instantiate the AccountController class. This is the controller that manages all the account creation, registration and login activities. Line 11 gives us some more information that points to the root cause.

No default Instance is registered and cannot be automatically determined for type ‘IUserStore<ApplicationUser>’

This is because StructureMap doesn’t know which concrete class to instantiate when it sees an Interface of type IUserStore<ApplicationUser>. If you’re like me (but you’re probably not – you’re a whole lot smarter) you’ll think, that’s fine, I’ll just add a mapping entry to the DefaultRegistry.cs file:

structuremap_11

But when you actually try to think what concrete implementation class to provide, you’ll be left scratching your head!

The key to remember here, is that StructureMap is ‘greedy’ about which constructor it will try to call, as it creates concrete instances of each interface. So it’s trying to create an instance of the AccountController class that requires 2 parameters.

structuremap_12

Unless we tell it to use a difference constructor, or we figure out all the concrete classes to register in the StructureMap registry. For more information on this checkout this Stackoverflow post on the topic.

The easiest way to solve this error, is to tell StructureMap which constructor to use. In other words, we want to satisfy the error message which asks for a default constructor. Thankfully there is a nice and simple way to do this. Simply add the [DefaultConstructor] attribute to the default, no arg constructor in AccountController.

structuremap_13

StructureMap will see this ‘hint’ and use this constructor and all will be fine. You can click the Register or Login links without any exceptions being thrown.

Now, if you don’t like the feel of skipping out on the principle of IoC with the ASP.NET Identity classes, you can register the various concrete classes that are required to let StructureMap invoke it’s preferred constructor – the one with 2 parameters!

structuremap_14

We need to register 3 concrete implementations in our registry. I must confess, I got 2/3rds of the way there, but I couldn’t manage to figure out how to register the Owin Authentication Context. Credit for this solution goes to Pouya Yousefi who answered this StackOverflow question. He links to a nice code example on Github too.

Hopefully this helps anyone else who is struggling with their StructureMap config in an ASP.NET MVC web app.

 

Want to Self-Publish a Super Successful Ebook?

“I worked really, really long hours. Think 12-hour workdays, with around 8 hours of working on the book and around 4 hours of consulting work to keep the money coming in,” says Samuel. It took him around three months to finish The Elements of User Onboarding.

This is a great review on Nathan Barry’s blog about how Samuel Hulick wrote and shipped his ebook to huge success! http://nathanbarry.com/samuel-hulick-37000-self-published-book

Windows 8.1 Install Fails on VMware Fusion 6.0.2

If you are trying to install Windows 8.1 into a virtual machine using VMware Fusion v6.0.2 and you get the following error, it might be that your CD/DVD ROM settings are incorrect.

Missing Driver File

Missing Driver File

Some websites will tell you that you have a corrupted .iso file. This might well be the case, but if you have checked the checksum of the downloaded file, that’s obviously not your problem. What the message is telling you is that it needs a driver to be able to control the CD/DVD drive correctly. This could simply be a config error in your VM.

Instead, check to see what your CD/DVD ROM settings are for your virtual machine. Cancel out of the Windows Setup screens and shutdown your VM. You can still keep the Windows install image connected to your CD/DVD drive.

Click the “Edit Hardware Settings” button for the VM:

Edit VM Settings

Edit VM Settings Button

This will open a new Settings dialog window. next click on the “CD/DVD” icon under the “Removable Devices” section:

VM Settings

VM Settings

Notice how mine says (SCSI)? This is a hint that your settings might be wrong. Expand the “Advanced Options” on the next screen to see what Bus Type your VM is set to use:

SCSI Bus Type

SCSI Bus Type

If it’s SCSI change it to SATA:

SATA Bus Type

SATA Bus Type

Next click the “Show All” button – this will take you back to the main Settings window. Click on the “Startup Disk” item:

Settings - Startup Disk

Settings – Startup Disk

Select the CD/DVD icon and then click the “Restart” button:

Startup Disk - Restart

Startup Disk – Restart

This will reboot your VM and your Windows 8.1 installation should continue successfully.

 

Record Solar Output

Today I hit 39.0kWh of power generation. I’m really looking forward to seeing what the long days of summer will bring!! Here in Perth, Western Australia, we get so many gorgeous sunny days without a cloud in the sky. Perfect for solar generation.

Last 2.5 months of daily output:

pvoutput.org_20131202

Todays generation graph:

pvoutput.org_output_20131202

 

Raspberry Pi Setup to monitor Aurora PVI-5000 Solar Inverter – Part 3

Click here to read Part 1 of this setup guide.

Click here to read Part 2 of this setup guide.

This post is meant to provide additional information to the excellent work done by Whirlpool Forums user ‘goldiexxxx’ and the guides he has published. For full info and details see http://forums.whirlpool.net.au/forum-replies.cfm?t=1965598 and http://forums.whirlpool.net.au/forum-replies.cfm?t=2116394

NB: these instructions might be helpful to anyone else who is running on Mac OS X

Visit the main Raspberry Pi website for additional information: http://www.raspberrypi.org/

If you have followed the steps in Part 2 of this guide, you should be ready to install the Aurora Monitoring software on your configured Raspberry Pi. You will have remote access setup and configured via SSH (command line) and VNC (graphical). Lets dive straight in.

Install Aurora Monitor Software

Connect to your Raspberry Pi using VNC Viewer and open the web browser (Midori), then type in the following URL:

http://auroramonitor.sourceforge.net/

Download Aurora Monitor

Across the top of the screen you will see the download link for the latest version (v1.07 at the time of writing).

Download Aurora Monitor Software

Download Aurora Monitor Software

Click the download link. You will be taken to the Sourceforge project page:

Aurora Monitor Zip File Download

Aurora Monitor Zip File Download

Click on the auroramon-1.07.zip to start the download process. You will then be presented with a Save As… dialog box. Click the “Save As” button. This will allow you to choose where to save the file.

Save As Dialog

Save As Dialog

Next you will see the following File Manager style window. Make sure that the ‘pi’ username is selected in the left hand panel. This is whats called the Home directory of the current user – you should be logged in as the username ‘pi’ (this is the default). Click the “Save” button to save the zip file to this folder.

Save Folder

Save Folder

Unzip Aurora Monitor .Zip File

Next, open an LXTerminal window… we’re going back to the command line for a while. Type the command (note: it’s a lower case ‘L’ character followed by an ‘s’):

ls

This will list all the files in the current folder – which should be your home folder, for the user ‘pi’:

ls command output

ls command output

You should see the downloaded zip file in the list:  auroramon-1.07.zip.

Install Zip Tools

If there isn’t a compatible zip/unzip tool installed on the Raspberry Pi, we can install one first. Skip ahead to the next paragraph below to try the unzip step and if it says “Zip: command not found“, come back here and type the following command to download and install the zip and unzip tools. Then repeat the unzip step again. Note: running this command if you already have the zip tools installed will not damage anything. It will check the version installed and upgrade it if necessary.

sudo apt-get install zip unzip

Already Have Zip Tools

If you have the unzip tool installed, or you want to test it out first, type the following command to unzip the downloaded file:

unzip ./auroramon-1.07.zip

You should see some text scroll by as the tool runs. If there are now errors, run the ‘ls’ command again, this time with these extra arguments (lower case ‘L’ again):

ls -al

You should see more detailed file listing:

Detailed File Listing

Detailed File Listing

At the top of the listing you will see the existing zip file, but there will also be a new directory with the same name, minus the .zip extension: auroramon-1.07. This directory is where the Aurora Monitoring software has been extracted to. Our next task will be to download a package that is required for Aurora Monitor to run, and then we will finish the installation of the Aurora software.

Download WX Widgets Packages

Type the following commands in the open LXTerminal window:

sudo apt-get install libwxgtk2.8-0

Check to see if any of the files in the WX package require updating or missing. This will actually check for any missing packages on the whole system, so might take a little while:

sudo apt-get update --fix-missing

Get the next required package:

sudo apt-get install libwxgtk2.8-dev

You should be able to continue straight to the next section but if you encounter any issues, try doing a reboot make sure all the changes are applied correctly and then try to continue again.

sudo reboot

Compile Aurora Monitor Software

Open LXTerminal once again. We are going to change directory to the /src folder in the auroramon-1.07 directory. Type the following command:

cd ./auroramon-1.07/src

Now we compile the source code ready for running. This can take a while so be patient for it to finish:

sudo make

You should see something like this:

Compile Aurora Monitor Source Code

Compile Aurora Monitor Source Code

Start Aurora Monitor

Now you are ready to run Aurora Monitor for the first time. If this fails, try rebooting again (sudo reboot). Make sure you are in the auroramon/src directory again if you reboot the system.

./auroramon

If all goes well you should see something this:

Aurora Monitor - 1st Run

Aurora Monitor – 1st Run

Close Aurora Monitor by clicking on the ‘File | Quit’ menu option.

Configure Aurora Monitor to Start at System Bootup

You should still be in the auroramon-1.07/src directory. Copy the “auroramon” executable file (this is what was created by the compile step above) to system programs directory:

sudo cp auroramon /usr/local/bin

Change directory to the ‘autostart’ directory under the ‘pi’ home directory:

cd ~/.config/autostart

Create a new file called ‘auroramon.desktop’:

sudo nano ./auroramon.desktop

and add the following contents:

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=Auroramon
Comment=
Exec=auroramon
StartupNotify=false
Terminal=false
Hidden=false

Save the file by pressing Ctrl+X, and then ‘Y’ to confirm the save, and finally press ‘enter’ to confirm the filename ‘auroramon.desktop’.

Lastly reboot the system so make sure that Aurora Monitor starts up automatically upon reboot. Give it a minute or two for the system to startup and then reconnect with VNC Viewer.

sudo reboot

 

Configure Aurora Monitor Settings

If Aurora Monitor is not running when you reconnect with VNC Viewer, you will need to go back and review the previous steps and try to find out where the error is.

Serial Port Setup

The first thing you will want to do is determine the Serial Port that the RS-485 to USB Converter is registered under. This is required when configuring the Aurora Monitor. Type the following command in an LXTerminal window:

dmesg | grep tty

Note that the character between the dmesg and grep is the pipe symbol. It is often found on the same key as the backslash (\) character, but requires the Shift to be pressed as well. You should see something like the following output:

List Serial Ports

List Serial Ports

You want to take note of the port name for the USB connected FTDI Converter. On my computer the port is:

ttyUSB0  (Note: the last character is a zero)

In Aurora Monitor click on the “Settings | Setup” menu item.

Aurora Monitor Setup Menu

Aurora Monitor Setup Menu

This will open the Setup dialog box:

Aurora Monitor Setup Dialog

Aurora Monitor Setup Dialog

Change the Serial Port to the correct one that was displayed in the ‘dmesg’ output. eg: /dev/ttyUSB0. The Inverter address is 2 by default on Aurora Solar inverters. Click OK to save the settings.

Location Setup

Click the “Settings | Location” menu item. The Location dialog will open.

Location Config Dialog

Location Config Dialog

You need to enter the Latitude and Longitude for your current location to allow for correct plotting of the solar insolation graph.

How do you find out your latitude and longitude? That’s a good question! I used this blog post as a guide.

  • Go to Google Maps (http://maps.google.com.au)
  • Type in your address and then zoom in to a decent level so you can see your house or building nice and easily in the centre of the screen.
  • Right click on your house and choose the option “What’s Here?”
  • A green arrow should appear where you right clicked. Click it!!
  • The latitude and longitude will be displayed above the arrow
Latitude & Longitude

Latitude & Longitude

Note: Aurora Monitor lists the longitude first and then the latitude, but in Google Maps they are listed with latitude first, then longitude.

You want to use the values in the red square. These are measurements in Degrees, Minutes and Seconds. So using the values above, I would set my location as follows:

  • Latitude:  -31:57:28
  • Longitude:  115:51:49

Enter your values and click OK to save them.

Latitude & Longitude Values

Latitude & Longitude Values

Inverter Setup

Click the “Settings | Inverter” menu item. The Inverter dialog will open.

Inverter Settings

Inverter Settings

Clicking the checkbox will auto retrieve energy data from the inverter in 5 minute intervals.

PVOutput.org Setup

Click the “Settings | PVOutput.org” menu item. The PVOutput dialog will open.

PVOutput.org Configuration

PVOutput.org Configuration

Once you have configured your own PVOutput account from the website, then you can enter your account ID and API key. Aurora Monitor will automatically upload your data to your website account every 5 mins while the inverter is working during daylight hours. Please donate to PVOutput.org to show your support for this [currently] free service.

As for the remaining settings – Charts, Historgrams & Extra Readings – feel free to investigate them and play around with these settings.

Final Configuration Setting

In the original setup guide by ‘goldiexxxx’ he makes mention about a USB/Comms lockup issue with conflicting ports. This was in part to the session that the VNC viewer was connecting to the Raspberry Pi with. The following fix resolves this issue.

Open a new LXTerminal window and enter the following command:

sudo nano /boot/cmdline.txt

Add this text:

dwc_otg.speed=1

immediately after the opening config setting:

dwc_otg.lpm_enable=0

Here is my edited file contents:

Edit /boot/cmdline.txt

Edit /boot/cmdline.txt

Now lets reboot the system and you should be ready to start monitoring!!

sudo reboot

Final Steps

All that’s left to do now is hook up your Raspberry Pi to your inverter via the Serial to USB Converter so that it can start monitoring your power output.

The images and notes that ‘goldiexxxx’ has in his setup guide covers this brilliantly, so I won’t duplicate his work here. Whether you are on a Mac, Windows or other system, the physical hardware setup is essentially the same. You can connect with a modified RJ-45 cable or directly into the RS-485 terminal block. This really is just personal preference.

Good luck with your setup and Happy Generation!!

 

Photos of Final Setup

My inverter is mounted in the carport on the wall. There is a power point on the wall just nearby so I was keen to make use of this in the most efficient way. When you have the Raspberry Pi setup with a keyboard, mouse and screen, you need to have a powered USB hub connected to be able to utilise all these different peripherals. Not to mention that you also have the Wireless Adapter to plug in and the RS-485 to USB adapter.

Once you have remote access to the Pi setup (via the wireless adapter), the only other cables or connections you need are:

  • Power Cable (from a USB enabled power source)
  • RS-485 to USB Adapter

With the wireless adapter already taking up 1 USB port, your RS-485 to USB will fill the remaining one. If we set things up this way though, we somehow need to power the device from another USB based power source. I could have used the powered USB hub that was provided with the kit, but that means that there is a large power brick to be plugged into the power point on the carport wall, plus there is extra cabling to connect the hub to the Pi and then the hub itself.

So I thought I would try something different… what about my spare iPhone 5 charger brick? This is a very small USB charger but it works perfectly well.

iPhone 5 Power Brick

iPhone 5 Power Brick

 

You could also use an iPhone 4 charger brick too.

iPhone 4 Charger

iPhone 4 Charger

I could plug it straight into the wall, run the cable directly to the inverter and plug it into the Raspberry Pi inside. This would remove the need for the hub to be included in the deployment. It would save on space too. So here is what the final setup looks like:

Final Setup

Final Setup

NOTE: In this photo, you can see that the Inverter is powered on. This was only during final testing and at no time was I reaching inside or touching anything with it turned on and the cover off. This is definitely dangerous and you are risking electric shock if you do so. At all times make sure the AC Isolator switch is turned off, as well as the DC Isolator switch before opening the front panel and reaching inside the Inverter!!

With the front panel closed on the inverter, there is no wireless signal, so I positioned the wireless antenna to poke down through the cable gland. This works fine and I can connect from anywhere in the house to check on its health.

Wireless Antenna exposed through cable gland

Wireless Antenna exposed through cable gland

This is what it looks like inside the inverter. There isn’t a whole lot of room to move, but it fits nicely:

Raspberry Pi inside Inverter

Raspberry Pi inside Inverter

RS-485 Cable Setup

This is often a source of frustration and confusion. For connecting from the Inverter motherboard to the RS-485 to USB converter I used a generic computer network cable (CAT 5). This has by default the RJ-45 connectors on each end. This is the correct size to fit in the two adapter ports on the motherboard, however if you do this, some network cables don’t have the correct pins wired up that are needed for data communication from the inverter.

So instead I snipped off each plug at the end of the cable and used 3 of the 4 available wires:

Cable Connection Options

Cable Connection Options

NOTE: Don’t forget to push the termination switch on the left of the RJ-45 sockets into the DOWN position, as shown in the image above. 

  • Terminal Block Position 3 – RTN – White and Blue Striped Wire
  • Terminal Block Position 4 – +T/R – Solid Orange Wire
  • Terminal Block Position 5 – -T/R – Solid Blue Wire
  • The Orange and White Striped Wire is unused and I actually ended up clipping it off from each end so it wasn’t in the way.
Wiring Hookup RS-485 Adapter Plug

Wiring Hookup RS-485 Adapter Plug

RS-485 to USB Wiring Orientation

RS-485 to USB Wiring Orientation

Once I figured out which wire was meant to go into which plug on the RS-485 to USB adapter everything worked perfectly. Data will start flowing immediately to your Raspberry Pi and Aurora Monitor will start graphing it, and if configured, start sending it up to the pvoutput.org website too.

Aurora Monitor Live Data

Aurora Monitor Live Data

Hopefully if you’re bothered reading this far, you also have a screen in Aurora Monitor something similar the one above. Thanks for reading and if you have any questions, please don’t hesitate to leave a comment, drop me a line on twitter or via email (jenart at gmail dot com).