7

I pretty New at IOC and web-api 2, but have got StructureMap to work on my own Controllers in web-api 2. What I don't have managed is to use StructureMap on the AccountController using Individual Accounts. I use AccountController out of the Box, and what I have managed so far is following:

  1. In Ioc.cs I have added following (Because of errors)

      x.For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>();
      x.For<DbContext>().Use(() => new ApplicationDbContext());
      x.For<ISecureDataFormat<AuthenticationTicket>()
           .Use<SecureDataFormat<AuthenticationTicket>>();
    

But now I'm stuck With this error:

"No default Instance is registered and cannot be automatically determined for type IDataSerializer<AuthenticationTicket>"

I realy dont know what to do here. I have tried to find a detault instance of IDataSerializer, but no Luck.

By the way... I have installed the Nuget package "Structuremap.webapi2"

Roger
  • 71
  • 2
  • hey did you figure this out? im having same exact issue. – parliament Jun 23 '14 at 22:25
  • 3
    Yes I did. The AccountController uses 2 constructors, and StructureMap uses by default the most greedy one. (The one with most parameters). What I did was to set the StructureMap attribute "DefaultConstructor" on the one that is least greedy. Like this: [DefaultConstructor] public AccountController() { } – Roger Jun 25 '14 at 05:18
  • 1
    I had the same issue, and added the [DefaultConstuctor] attribute. However, this does mean that the other constructor is never called. I don't think that this is a big issue though, as all this sets is is the UserManager which has a default getter and the AccessTokenFormat which you can just set explicitly. Do you know if there are any problems if the other constructor is removed? – ColinM Jul 31 '14 at 06:40
  • 3
    Please add this as a detailed answer and mark your own answer as correct. – Patrick Magee Dec 12 '14 at 09:15

1 Answers1

8

Solved it!

Add this configuration to the IoC.cs or DefaultRegistry:

        For<ISecureDataFormat<AuthenticationTicket>>().Use<SecureDataFormat<AuthenticationTicket>>();
        For<IDataSerializer<AuthenticationTicket>>().Use<TicketSerializer>();
        For<IDataProtector>().Use(() => new DpapiDataProtectionProvider().Create("ASP.NET Identity"));
        For<ITextEncoder>().Use<Base64UrlTextEncoder>();

        For<Microsoft.AspNet.Identity.IUserStore<ApplicationUser>>().Use<Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>>();
        For<System.Data.Entity.DbContext>().Use(() => new ApplicationDbContext());
Henry IT
  • 81
  • 1
  • 3