14

I have a controller.

 public sealed class AccountsController : BaseApiController { private readonly IDatabaseAdapter _databaseAdapter; public AccountsController(IDatabaseAdapter databaseAdapter) { _databaseAdapter = databaseAdapter; } [AllowAnonymous] [Route("create")] public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel) { if (!ModelState.IsValid) return BadRequest(ModelState); if (! await _databaseAdapter.DoesAgentExist(createUserModel.UserName)) return BadRequest(); if (await _databaseAdapter.DoesAgentHaveAccount(createUserModel.UserName)) return BadRequest(); // Create account. var password = PasswordHelper.GeneratePassword(32); createUserModel.Password = password; createUserModel.ConfirmPassword = password; var user = new ApplicationUser { UserName = createUserModel.UserName, }; var addUserResult = await AppUserManager.CreateAsync(user, createUserModel.Password); if (!addUserResult.Succeeded) return GetErrorResult(addUserResult); var locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id })); return Created(locationHeader, ModelFactory.Create(user)); } } 

When I send the following fiddler to the create method.

http://localhost:59430/api/accounts/create User-Agent: Fiddler

Content-Type: application/json Accept: application/json Host: localhost:59430 Content-Length: 106

{ "UserName":"a.xxxxx", "Password":"xxxxxx", "ConfirmPassword":"xxxxxx", }

It gets down to the following line:

var addUserResult = await AppUserManager.CreateAsync(user, createUserModel.Password);

Then the following exception occurs

{ "message": "An error has occurred.", "exceptionMessage": "Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'.", "exceptionType": "System.MissingMethodException", "stackTrace": " at WebAuth.Controllers.BaseApiController.get_AppUserManager()\r\n at WebAuth.Controllers.AccountsController.d__3.MoveNext() in C:\Users\stuarts\Documents\Visual Studio 2017\Projects\WebAuth\WebAuth\Controllers\AccountsController.cs:line 76\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" }

Anyone know what is going on? I have no idea why it can't find that method.

My bin folders contains

System.Web.Http.dll System.Web.Http.Owin.dll System.Net.Http.dll

ApplicationUserManager

public sealed class ApplicationUserManager : UserManager<ApplicationUser> { public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var appDbContext = context.Get<ApplicationDbContext>(); var appUserManager = new ApplicationUserManager(new UserStore<ApplicationUser>(appDbContext)); appUserManager.UserValidator = new UserValidator<ApplicationUser>(appUserManager) { AllowOnlyAlphanumericUserNames = true, RequireUniqueEmail = false, }; appUserManager.PasswordValidator = new PasswordValidator { RequiredLength = 12, RequireNonLetterOrDigit = true, RequireUppercase = true, RequireLowercase = true, RequireDigit = true }; appUserManager.MaxFailedAccessAttemptsBeforeLockout = 3; appUserManager.DefaultAccountLockoutTimeSpan = TimeSpan.FromHours(1); return appUserManager; } } 

BaseApiController

public class BaseApiController : ApiController { private ModelFactory _modelFactory; private readonly ApplicationUserManager _applicationUserManager = null; protected ApplicationUserManager AppUserManager => _applicationUserManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>(); protected ModelFactory ModelFactory => _modelFactory ?? (_modelFactory = new ModelFactory(Request, AppUserManager)); protected IHttpActionResult GetErrorResult(IdentityResult result) { if (result == null) return InternalServerError(); if (result.Succeeded) return null; if (result.Errors != null) foreach (var error in result.Errors) ModelState.AddModelError(string.Empty, error); if (ModelState.IsValid) return BadRequest(); return BadRequest(ModelState); } private readonly ApplicationRoleManager _appRoleManager = null; protected ApplicationRoleManager AppRoleManager => _appRoleManager ?? Request.GetOwinContext().GetUserManager<ApplicationRoleManager>(); } 
7
  • don't you need to add the attribute [HttpGet] or something? and in REST, something that creates should be a POST anyway if I'm not wrong Commented Sep 27, 2017 at 15:05
  • Why is your controller sealed and what is BaseApiController? Commented Sep 27, 2017 at 15:07
  • Can You share the code of AppUserManager property? Commented Sep 27, 2017 at 15:12
  • Is your create a GET or a POST? It looks like a GET when it probably should be a POST. Commented Sep 27, 2017 at 15:14
  • @Chetan Ranpariya, I've added AppUserManager class implementation. Commented Sep 27, 2017 at 15:17

1 Answer 1

53

I found a solution to this.

When I was building there was build warnings going to the output window but not showing in the main error / warning window.

They were to do with assembly conflicts and said recommend putting the assembly redirect in the web.Config.

Once I had went through them all (around 80) it now works.

e.g.

 <dependentAssembly> <assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" /> </dependentAssembly> 
Sign up to request clarification or add additional context in comments.

3 Comments

Yea, I got this too. Fwiw, in my case the issues started when I added web api capabilities to a regular asp.net mvc project. Confirm your solution worked.
it's weird, I already had an automatically inserted redirect, but, it was geared toward 4.1.0.0 and this answer prodded me to try upgrading the redirect.. which did the trick!
Thank you so much for this - fixed my issue!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.