MVC WEB-API 2 and .NET 4.5.2- No parameterless constructor defined for this object when using structuremap

When creating a Visual Studio MVC Web API and you are using Structure Map 3 (see below for older version) you will need a couple of packages for things to wire up correctly or you will get an error that says, “No parameterless constructor defined for this object. ”

The packages you need are: StructureMap.MVC5, StructureMap.WebApi2

<package id=”StructureMap.MVC5” version=”3.1.1.134″ targetFramework=”net452″ />

 <package id=”StructureMap.WebApi2” version=”3.0.4.125″ targetFramework=”net452″ />

Then in the basic template project you can use the ValueController to test and it should be that simple.

DR

You will notice in the MVC WEB-API project a few more folders added, check out the DependencyResolution folder!  If you are using DTOs, some type of BLL or Repository you will need to add mappings to the DefaultRegistry.cs file to wire up the mapping of Interfaces to concrete implementations.

 


What if you are using an older structuremap like version 2.0.50727?

Do the following:

Manually add a folder called DependencyResolver or whatever you like…

I added a class file called ServiceActivator.cs

Add the following lines of code:

 public class ServiceActivator : IHttpControllerActivator
 {
 public ServiceActivator(HttpConfiguration configuration) { }

 public IHttpController Create(HttpRequestMessage request
 , HttpControllerDescriptor controllerDescriptor, Type controllerType)
 {
 var controller = ObjectFactory.GetInstance(controllerType) as IHttpController;
 return controller;
 }
 }

Then add the following to the Global.ascx file

 HttpConfiguration config = GlobalConfiguration.Configuration;
 config.Services
 .Replace(typeof(IHttpControllerActivator), new ServiceActivator(config));

 

I then add another file in the DepedencyResolver folder called BootstrapStructuremap.cs

This is where I linkup my interfaces to my concretes

 public class BootstrapStuctureMap
 {
 private static bool _hasStarted;
 public static void Bootstrap()
 {
 new BootstrapStuctureMap().BootstrapStructureMap();
 }

 public static void Restart()
 {
 if (_hasStarted)
 {
 ObjectFactory.ResetDefaults();
 }
 else
 {
 Bootstrap();
 _hasStarted = true;
 }
 }



 public void BootstrapStructureMap()
 {
 _hasStarted = true;

 ObjectFactory.Initialize(x =>
 {
 x.PullConfigurationFromAppConfig = false;
 x.Scan(y =>

 {
 y.Assembly(Assembly.GetAssembly(typeof(IWebAccess)));
etc...  etc...
 }
 );

 });

 }
 }

 

Then back in my global.ascx I add

protected void Application_BeginRequest()
 {
 
 try
 {
 BootstrapStuctureMap.Bootstrap();
 }
 catch (Exception ex)
 {
 throw;
 }
 
 }

code-ON

 

Warren LaFrance

makecert and creating ssl or signing certificates

Credit to this blogger for this post.  I am reposting the contents here just incase the blog goes away as if very common.

Another blog about CERTIFICATES that is useful…

How to Set Up Site Bindings in Internet Information Services (IIS)

 

 

I’ve been asked to post my makecert scripts for creating self-signed certificates (one for SSL and the other for signing). I use both of these scripts as .bat files. These scripts accept one parameter — the CN (common name) you want the certificate to match. For the SSL cert this must match the host name. For signing it’s just a unique name. Both of these need to be run from an administrative command prompt because the scripts install the certificate into the local machine’s personal certificate store. If you need the public key portion (.cer) then you’d have to open mmc and export it. Also, notice the expiration in the scripts — this is something you might want to change based upon your situation.

The first script is for creating SSL certificates. This is good for setting up SSL on your local IIS for a new web site (you’d need to ensure the host is indicated and SNI is configured). Although the SSL certificate won’t be trusted until you configure the cert as trusted on the client machine. Here are the .bat file contents:

makecert -r -pe -n "CN=%1" -b 01/01/2015 -e 01/01/2020 -eku 1.3.6.1.5.5.7.3.1 -sky exchange -a sha256 -len 2048 -ss my -sr localMachine

The second script is for creating signing certificates (for things like token signing within a token service such as IdentityServer). Here are the .bat file contents:

makecert -r -pe -n "CN=%1" -b 01/01/2015 -e 01/01/2020 -eku 1.3.6.1.5.5.7.3.3 -sky signature -a sha256 -len 2048 -ss my -sr LocalMachine

HTH

log4Net logging

I struggled a bit with understanding this tool

These articles helped a ton:

The basics

http://www.codeproject.com/Articles/140911/log4net-Tutorial

More advance

http://www.codeproject.com/Articles/406634/Creating-a-custom-log-net-appender

Then custom table example:

 

<appender name=”ADONetAppender_SqlServer” type=”log4net.Appender.AdoNetAppender”>
<bufferSize value=”1″ />
<connectionType value=”System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ />
<connectionString value=”Data Source=Jcdc-d-sql-001\dev;Database=log4Net;User Id=int_serv;Password=Int@2015!;” />
<filter type=”log4net.Filter.LevelRangeFilter”>
<levelMin value=”FATAL” />
<levelMax value=”FATAL” />
</filter>
<!–<connectionString value=”data source=JCDC-D-SQL-001\DEV;initial catalog=log4net;integrated security=true;persist security info=True;User ID=jcdev\lafrance.warren” />–>
<commandText value=”INSERT INTO AppLog ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)” />
<parameter>
<parameterName value=”@log_date” />
<dbType value=”DateTime” />
<!–
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%date{yyyy’-‘MM’-‘dd HH’:’mm’:’ss’.’fff}” />
</layout>
–>
<layout type=”log4net.Layout.RawTimeStampLayout” />
</parameter>
<parameter>
<parameterName value=”@thread” />
<dbType value=”String” />
<size value=”255″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%thread” />
</layout>
</parameter>
<parameter>
<parameterName value=”@log_level” />
<dbType value=”String” />
<size value=”50″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%level” />
</layout>
</parameter>
<parameter>
<parameterName value=”@logger” />
<dbType value=”String” />
<size value=”255″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%logger” />
</layout>
</parameter>
<parameter>
<parameterName value=”@message” />
<dbType value=”String” />
<size value=”4000″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%message” />
</layout>
</parameter>
<parameter>
<parameterName value=”@exception” />
<dbType value=”String” />
<size value=”2000″ />
<layout type=”log4net.Layout.ExceptionLayout” />
</parameter>
</appender>
<!–Configure the AdoNetAppender to connect to MS SQL Server –>
<appender name=”InfoAppender_SqlServer” type=”log4net.Appender.AdoNetAppender”>
<level value=”INFO” />

<filter type=”log4net.Filter.StringMatchFilter”>
<stringToMatch value=”Sending Message” />
</filter>
<filter type=”log4net.Filter.LevelRangeFilter”>
<levelMin value=”INFO” />
<levelMax value=”INFO” />
</filter>
<bufferSize value=”1″ />
<connectionType value=”System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ />
<connectionString value=”Data Source=Jcdc-d-sql-001\dev;Database=log4Net;User Id=int_serv;Password=Int@2015!;” />
<commandText value=”INSERT INTO [dbo].[EmailLog] ([Date],[Thread],[Level],[Logger],[Message],[Exception],[StartDate],[EndDate],[DeliveryFrequency],[Address] ,[Parms] ,[Body],[sirsLogNumber], [sirsIncidentNumber]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @StartDate,@EndDate,@DeliveryFrequency,@Address,@Parms,@Body, @sirsLogNumber, @sirsIncidentNumber)” />

<parameter>
<parameterName value=”@log_date” />
<dbType value=”DateTime” />
<!–
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%date{yyyy’-‘MM’-‘dd HH’:’mm’:’ss’.’fff}” />
</layout>
–>
<layout type=”log4net.Layout.RawTimeStampLayout” />
</parameter>
<parameter>
<parameterName value=”@thread” />
<dbType value=”String” />
<size value=”255″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%thread” />
</layout>
</parameter>
<parameter>
<parameterName value=”@log_level” />
<dbType value=”String” />
<size value=”50″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%level” />
</layout>
</parameter>
<parameter>
<parameterName value=”@logger” />
<dbType value=”String” />
<size value=”255″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%logger” />
</layout>
</parameter>
<parameter>
<parameterName value=”@message” />
<dbType value=”String” />
<size value=”4000″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%message” />
</layout>
</parameter>
<parameter>
<parameterName value=”@exception” />
<dbType value=”String” />
<size value=”2000″ />
<layout type=”log4net.Layout.ExceptionLayout” />
</parameter>

<parameter>
<parameterName value=”@DeliveryFrequency” />
<dbType value=”String” />
<size value=”255″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%property{DeliveryFrequency}” />
</layout>
</parameter>

<parameter>
<parameterName value=”@StartDate” />
<dbType value=”DateTime” />
<layout type=”log4net.Layout.RawTimeStampLayout”>
<conversionPattern value=”%property{StartDate}” />
</layout>
</parameter>
<parameter>
<parameterName value=”@EndDate” />
<dbType value=”DateTime” />
<layout type=”log4net.Layout.RawTimeStampLayout”>
<conversionPattern value=”%property{EndDate}” />
</layout>
</parameter>
<!–@sirsLogNumber–>
<parameter>
<parameterName value=”@sirsLogNumber” />
<dbType value=”String” />
<size value=”255″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%property{sirsLogNumber}” />
</layout>
</parameter>
<!–@sirsIncidentNumber–>
<parameter>
<parameterName value=”@sirsIncidentNumber” />
<dbType value=”String” />
<size value=”255″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%property{sirsIncidentNumber}” />
</layout>
</parameter>
<!–@address–>
<parameter>
<parameterName value=”@Address” />
<dbType value=”String” />
<size value=”-1″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%property{Address}” />
</layout>
</parameter>
<!–@parms–>
<parameter>
<parameterName value=”@Parms” />
<dbType value=”String” />
<size value=”-1″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%property{Parms}” />
</layout>
</parameter>
<!–@body–>
<parameter>
<parameterName value=”@Body” />
<dbType value=”String” />
<size value=”-1″ />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%property{Body}” />
</layout>
</parameter>
</appender>

Xamarin, VS 2015 and SDK’s for Hello Android

Well, worked this weekend on getting  Xamarin for Visual Studio 2015 working.

A few notes:

  1. Download installer from Xamarin, but do not run it directly.
  2. Kill any Java Exe processes before running installer
  3. Locate Installer and RUN AS ADMIN
  4. If you get an error at the end of the install process about certain SDK’s not installing.
    1. Go to:  “C:\Program Files (x86)\Android\android-sdk\SDK Manager.exe” and run as admin.
    2. Deselect everything and then select ONLY the SKD’s you need and nothing extra.
    3. Reboot after install
  5. Run visual studio in admin mode.
  6. Do the follow to create a super simple test so you can debug any other configuration issue.  https://developer.xamarin.com/guides/android/getting_started/hello,android/hello,android_quickstart/

The end result:

Phoneword

Flour Tortillas Recipe Easy

Tools Needed:

Instructions:

Preheat Griddle to 350 degrees and place cooling rack to the side. If using cast iron use PAM (light coat) or such to keep dough from sticking.

IN ELECTRIC MIXER COMBINE

2 3/4 cups wheat flour – all purpose
5 tablespoons of lard – NOT butter, yes lard…
Mix ( start on low speed) above ingredients until beady or about 1.5 minutes.. Not a big deal if this ism’t perfect really.


1/2 cup to 3/4 cup hot water in microwave proof container
3/4 tsp sea salt
Dissolve salt into water after removing from microwave

While blender is on LOW, slowly add hot water mixture until incorperated into flour and kind of forms a ball or feels like play dough.


On clean surface dust with flour.

Pinch small ball (smaller than a golf ball) and gently form into ball.

Place on floured surface and dust a bit more.

Using rolling pin, flatten to desired thickness.. I like mine very thin.

Prepare about 4 and then place 2 on griddle or more if it will hold.  Flip with spatula.

While they are cooking,  form up more balls and roll out flat.  Keep a close eye on the cooking items and flip once they start to blister (puff up a bit) .  Cook the other side for a minute or so or until you think it is ready to remove.  Place on cooling rack and repeat.

Test one to make sure they are cooked enough, if not put it back on the griddle longer.

Yes stacking them on top of each other is fine.

Once they are completely cooled store in refrigerator in gallon zip-lock bag.

Enjoy.

Updating from MVC 3.0.0.0 to 3.0.0.1 Causes an error

So you need to update from MVC 3.0.0.0 to MVC 3.0.0.1 or should I say your windows update tool caused you to update.

Refer to this article

Microsoft Asp.Net MVC Security Update MS14-059 broke my build!

 

Then open package manager for nuget and make sure it is pointed to the correct project..

Usually your root mvc application

Then type in the console :  get-package

It will list out your installed packages

See if your framework number is: 3.0.50813.1 for Microsoft.AspNet.Mvc

if not, then you will need to update your solution projects

The COmmand is:  Install-Package Microsoft.AspNet.Mvc -Version {hit the tab key.. find the version you need – in my case 3.0.50813.1} and select it.. Run it and you should be done…

Note you might get a sql lite issue.. See https://warrenlafrance.com/2016/03/08/sql-lite-error/

 

SQL LITE ERROR

If you get a sql lite error like this:

Could not load file or assembly ‘System.Data.SQLite’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
Open IIS Manager
Make sure the application Pool is set to allow 32 bit applications under advanced settings.

 

AutoMapper InjectFrom value mapper

FYI Value injector doesn’t support automatic collection mapping like AutoMapper but you could use a simple LINQ expression and operate on each element:
IEnumerable<Category> categoryList = categoryService.GetAll();
List<CategoryViewModel> viewModelList = categoryList
 .Select(x => new CategoryViewModel().InjectFrom(x)).Cast<CategoryViewModel>()
 .ToList();

Immediately-Invoked Function Expression (IIFE)

A great article by By “Cowboy” Ben Alman 

| 0 Comments

In case you hadn’t noticed, I’m a bit of a stickler for terminology. So, after hearing the popular, yet misleading, JavaScript term “self-executing anonymous function” (or self-invoked anonymous function) one too many times, I’ve finally decided to organize my thoughts into an article.

In addition to providing some very thorough information about how this pattern actually works, I’ve actually made a recommendation on what we should call it, moving forward. Also, If you want to skip ahead, you can just check out some actual Immediately-Invoked Function Expressions, but I recommend reading the entire article.

Please understand that this article isn’t intended to be an “I’m right, you’re wrong” kind of thing. I’m genuinely interested in helping people understand potentially complex concepts, and feel that using consistent and accurate terminology is one of the easiest things that people can do to facilitate understanding.

So, what’s this all about, anyways?

The full article is here… http://benalman.com/news/2010/11/immediately-invoked-function-expression/