0

I have explored a very special thing: If I run my application I will get the current customer who is using my application.

First, the code in the controller is running, then the one from the service and last but not least the code in the backend.

If I set breakpoints in the backend code and run my application the code in the backend is never reaching the code where I set the breakpoints.

But, and that is crazy, I get an statuscode error 500 (Internal server error). And when I lookup this error in the browser Google Chrome I see this:

ExceptionMessage: "Sequence contains more than one element"
ExceptionType: "System.InvalidOperationException"
Message: "An error has occurred."
StackTrace: " at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
↵ at Microsoft.Owin.Security.AuthenticationManager.<AuthenticateAsync>d__8.MoveNext()
↵--- End of stack trace from previous location where exception was thrown ---
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵ at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
↵ at System.Web.Http.HostAuthenticationFilter.<AuthenticateAsync>d__0.MoveNext()
↵--- End of stack trace from previous location where exception was thrown ---
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵ at    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
↵ at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()
↵--- End of stack trace from previous location where exception was thrown ---
↵ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵ at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
↵ at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"

So I have checked what is going on with that error. All I read was about the "SingleOrDefault" or "FirstOrDefault". I have FirstOrDefault, but there is no looping through the breakpoints I have set, so how it knows that that the sequence contains more than one element? Did I understand something false?

Controller:

CustomerService.current().then(function(customer) {
    ...
}

Service:

.factory('CustomerService', function(Restangular) {
  return {
     current: function() {
        return Restangular.one('api/currentCustomer').get();
     },
  };

})

Backend

   [Route("api/currentCustomer")]      
   [Authorize(Roles = "User")]
   public IHttpActionResult GetCurrentCustomer()
   {
      IHttpActionResult result;

      try
      {             
         Customer customer = repository.GetCurrentCustomer(HttpContext.Current.User.Identity.Name);
         if (customer != null)
         {
            CustomerViewModel customerViewModel = new CustomerViewModel(customer);
            result = Ok(customerViewModel);
         }
         else
         {                
            //throw exception
         }
      }
      catch (Exception exception)
      {            
         //throw Exception
      }       
      return result;
   }


public Customer GetCurrentCustomer(string name)
  {
     using (IResModelContainer entities = new IResModelContainer())
     {
        Customer customer = entities.Customers.FirstOrDefault(c => c.UserLogin.Equals(name));
        return customer;
     }
  }
user3679607
  • 163
  • 2
  • 16

0 Answers0