Why can't I map `ApplicationUser` to a view model with AutoMapper? - asp.net-mvc

My ApplicationUser is fairly standard as generated by the MVC 5 project template, with the addition of FullName and the IPayCaddyEntity interface:
public class ApplicationUser : IdentityUser, IPayCaddyEntity
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public string FullName { get; set; }
}
The new interface is only:
public interface IPayCaddyEntity
{
string Id { get; set; }
}
and is already implemented by IdentityUser having a string Id property. Everything builds and mostly runs OK. Then I want to map a list of users to a viewmodel:
public class PlayerViewModel: PayCaddyViewModel
{
public string FullName { get; set; }
}
They should map nicely, both have the same type FullName property. I try this:
Mapper.CreateMap<ApplicationUser, PlayerViewModel>().ReverseMap();
Mapper.AssertConfigurationIsValid();
then this:
var players = _db.Users.ToList()
var model = Mapper.Map<IList<PlayerViewModel>>(players);
The Map call throws an exception.
I am trying to include the Identity entities in my own database, and the Users property is a DbSet<ApplicationUser that my DbContext inherits from IdentityDbContext<ApplicationUser>.
The full stack trace is:
[AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
ApplicationUser -> PlayerViewModel
PayCaddy.Data.Models.ApplicationUser -> PayCaddy.Client.Models.PlayerViewModel
Destination path:
IList`1[0]
Source value:
System.Data.Entity.DynamicProxies.ApplicationUser_4342C7C6E2802320D156341C80F8DED74454F28D43628C766C6951DB971B9BDA]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +610
AutoMapper.Mappers.EnumerableMapperBase`1.Map(ResolutionContext context, IMappingEngineRunner mapper) +482
AutoMapper.Mappers.CollectionMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +126
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +610
AutoMapper.MappingEngine.MapCore(Object source, Type sourceType, Type destinationType, MappingOperationOptions options) +179
AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action`1 opts) +59
AutoMapper.MappingEngine.Map(Object source, Action`1 opts) +92
AutoMapper.MappingEngine.Map(Object source) +93
AutoMapper.Mapper.Map(Object source) +62
PayCaddy.Client.Controllers.<PlayersIndex>d__11.MoveNext() in C:\Development\Cordova\PayCaddy\PayCaddy.Client\Controllers\AccountController.cs:65
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9723757
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Your problem must be the CreateMap not being called, since your code works fine as you can see in this .NET fiddle.
Double check if the CreateMap is being called properly. Try calling it in Application_Start() method, Global.asax.cs file.

Related

Cannot close stream until all bytes are written (VirusTotal API, Mvc)

This Question is different from
Sending file to VirusTotal with MVC
I am a student trying out another mini project where the user can upload a CSV file to the web server. But before I can create another program to execute the file, I would like to send the file to virustotal to have it check for the virus.
I tried but I got an error: "Cannot close stream until all bytes are written"
Here are my codes:
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VirusTotalNET;
namespace Testing_1.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult Upload(HttpPostedFileBase file)
{
string filename = Server.MapPath("~/CSV/" + file.FileName);
file.SaveAs(filename);
FileInfo fi = new FileInfo(filename);
VirusTotal vtObj = new VirusTotal("%API KEY");
var resID = vtObj.ScanFile(fi).ToString();
ViewBag.resID = resID;
ViewBag.Path = filename;
return View();
}
}
}
I got the error at this line: string resID = vtObj.ScanFile(file.FileName);
Index
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="File" id="file"/>
<input type="submit" value="Upload" />
}
Upload
#{
ViewBag.Title = "Upload";
}
<h2>Uploaded: #ViewBag.Path : #ViewBag.resID</h2>
Stack Trace
[IOException: Cannot close stream until all bytes are written.]
System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting) +609
[WebException: The request was aborted: The request was canceled.]
VirusTotalNET.VirusTotal.GetResults(RestRequest request, Boolean applyHack) in d:\Source control\Github\VirusTotal.NET\VirusTotal.NET\VirusTotal.cs:687
VirusTotalNET.VirusTotal.ScanFile(Stream fileStream, String filename) in d:\Source control\Github\VirusTotal.NET\VirusTotal.NET\VirusTotal.cs:191
VirusTotalNET.VirusTotal.ScanFile(FileInfo file) in d:\Source control\Github\VirusTotal.NET\VirusTotal.NET\VirusTotal.cs:150
C200_1.Controllers.HomeController.Upload(HttpPostedFileBase file) in C:\Users\FrezzeY\Desktop\C200\C200_1\Controllers\HomeController.cs:28
lambda_method(Closure , ControllerBase , Object[] ) +103
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +30
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +197
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +46
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +37
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +24
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +43
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +69
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +230
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +27
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +42
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +124
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +27
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +48
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +32
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +26
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +40
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +24
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +27
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +48
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +29
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +23
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9744261
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Please help me Thank you
Saving the file to disk before scanning could be a security issue.
You should scan it before trying to save it.
Update your method.
public ActionResult Upload(HttpPostedFileBase file) {
string fileName = file.FileName
var fileStream = new MemoryStream();
file.InputStream.CopyTo(fileStream);
var vtObj = new VirusTotal("%API KEY");
var fileResults = vtObj.ScanFile(fileStream, fileName);
var resId = fileResults.ScanId;
//should do something based on message and then decide if you want to save the file
var savePath = Server.MapPath("~/CSV/" + fileName);
file.SaveAs(savePath);
ViewBag.resID = resId;
ViewBag.Path = savePath;
return View();
}

No connection could be made because the target computer the actively refused

i am kinda new working with Asp.net Rest and i dont know how to solve this, i already read many articles at stackoverflow and no one solved my problem, i tryed firewall i tryed the configurations on internet option and other things, i think the problem can be related to something that is confusing me, do i need to run the webservice before i run my client application ?? how to do it? because what i did was just programming my rest application then i created my asp.net mvc application where i connect to the rest service with this helper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Http;
namespace ClienteMvcProdutos.Helpers
{
public static class WebApiHttpClient
{
public const string WebApiBaseAddress = "http://localhost:25426/";
public static HttpClient GetClient()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(WebApiBaseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
}
}
already checked ports and they matched, already changed ports and it still dont work im kinda desesperating :S here is my stacktrace
[SocketException (0x274d): Nenhuma ligação pôde ser feita porque o computador de destino
as recusou activamente 127.0.0.1:25426]
System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) +6604828
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +249
[WebException: Unable to connect to the remote server]
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
+606 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
+64
[HttpRequestException: An error occurred while sending the request.]
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) +58 System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
+28 ClientMvcProdutos.Controllers.<Index>d__1.MoveNext() in C:\Users\Filipe\Documents\Visual Studio
2015\Projects\ClientMvcProdutos\ClientMvcProdutos\Controllers\ProdutosController.cs:25
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()
+26 System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task)
+42 System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult
asyncResult) +73
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult
asyncResult) +37
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult
asyncResult) +27
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +68
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult
asyncResult) +34
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
+69 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
+230 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +27
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult
asyncResult) +27
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +68
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult
asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
+42 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult
asyncResult) +124
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult
asyncResult) +27
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +30
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult
asyncResult) +29
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult
asyncResult, ExecuteCoreState innerState) +27
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +48
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +30
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +21
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +32
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult
asyncResult, Controller controller) +26
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +40
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +30
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +21 System.Web.Mvc.Controller.EndExecute(IAsyncResult
asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult
asyncResult) +24
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult
asyncResult, ProcessRequestState innerState) +27
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +48
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +30
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +21
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
+29 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
result) +23
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult
ar) +129

Object reference not set to an instance Office 365 api

I am trying to pull a list of files from OneDrive to a list and display it to a view with a MVC application. This is my first go at it so I apologize if I am missing something obvious. I am getting an object reference not set to an instance of an object error. here is my code
public async Task<ActionResult> GetOneDriveFiles()
{
List<OneDriveFiles> myFilesList = new List<OneDriveFiles>();
try
{
var fileResults = await _spFilesClient.Files.Take(10).ExecuteAsync();
var files = fileResults.CurrentPage.OfType<Microsoft.Office365.SharePoint.File>().OrderBy(e => e.Name);
foreach (File file in files)
{
OneDriveFiles myFile = new OneDriveFiles();
myFile.Id = file.Id;
myFile.Name = file.Name;
myFile.Url = file.Url;
myFile.Size = file.Size;
myFile.TimeLastModified = file.TimeLastModified;
myFilesList.Add(myFile);
}
}
catch (RedirectRequiredException redir2)
{
return Redirect(redir2.RedirectUri.ToString());
}
return View(myFilesList);
}
here is the error code:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
O365APIURSAITO.Controllers.<GetOneDriveFiles>d__14.MoveNext() +110
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +84
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651188
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
My issue was I forgot to ensure the client was created. To do this right after the opening bracket for the try statement put:
public async Task<ActionResult> GetOneDriveFiles()
{
List<OneDriveModel> oneDriveFiles = new List<OneDriveModel>();
try
{
await MyFilesApiSample.EnsureClientCreated();
var allMyFiles = await MyFilesApiSample.GetMyFiles();
foreach (var item in allMyFiles)
{
OneDriveModel newFile = new OneDriveModel();
newFile.Name = item.Name;
newFile.Url = item.Url;
newFile.Size = item.Size;
newFile.LastModified = item.TimeLastModified;
oneDriveFiles.Add(newFile);
}
}
catch (RedirectRequiredException ex)
{
return Redirect(ex.RedirectUri.ToString());
}
return View(oneDriveFiles);
}

No Handler Registered CQRS-ES

I am creating a cqrs-es test application. I scan command handler against command by following code
public class StructureMapCommandHandlerFactory : ICommandHandlerFactory
{
public ICommandHandler<T> GetHandler<T>() where T : Command
{
var handlers = GetHandlerTypes<T>().ToList();
var cmdHandler = handlers.Select(handler =>
(ICommandHandler<T>)ObjectFactory.GetInstance(handler)).FirstOrDefault();
return cmdHandler;
}
private IEnumerable<Type> GetHandlerTypes<T>() where T : Command
{
var handlers = typeof(ICommandHandler<>).Assembly.GetExportedTypes()
.Where(x => x.GetInterfaces()
.Any(a => a.IsGenericType && a.GetGenericTypeDefinition() == typeof(ICommandHandler<>)))
.Where(h => h.GetInterfaces()
.Any(ii => ii.GetGenericArguments()
.Any(aa => aa == typeof(T)))).ToList();
return handlers;
}
}
I dispatches command as follows:
public class CommandBus:ICommandBus
{
private readonly ICommandHandlerFactory _commandHandlerFactory;
public CommandBus(ICommandHandlerFactory commandHandlerFactory)
{
_commandHandlerFactory = commandHandlerFactory;
}
public void Send<T>(T command) where T : Command
{
var handler = _commandHandlerFactory.GetHandler<T>();
if (handler != null)
{
handler.Execute(command);
}
else
{
throw new UnregisteredDomainCommandException("no handler registered");
}
}
}
When I run the project it throw an exception
"No Handler Registered"
My project location is https://drive.google.com/file/d/0B1rU7HOTfLwea0tVOXdIb2Nib2M/edit?usp=sharing
The Stack-Trace of this exception as follow:
[UnregisteredDomainCommandException: no handler registered]
CQRS.Infrastructure.Messaging.CommandBus.Send(T command) in f:\Video\Latest Readable\CQRS Tutorial\CQRS By Authors\HRMSystem\CQRS.Infrastructure\Messaging\CommandBus.cs:31
HRMSWeb.Facade.DiaryItemFacade.Delete(Guid id) in f:\Video\Latest Readable\CQRS Tutorial\CQRS By Authors\HRMSystem\HRMSWeb\Facade\DiaryItemFacade.cs:59
HRMSWeb.Controllers.HomeController.Delete(Guid id) in f:\Video\Latest Readable\CQRS Tutorial\CQRS By Authors\HRMSystem\HRMSWeb\Controllers\HomeController.cs:29
lambda_method(Closure , ControllerBase , Object[] ) +184
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +211
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +27
System.Web.Mvc.Async.<>c_DisplayClass42.b_41() +28
System.Web.Mvc.Async.<>c_DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) +10
System.Web.Mvc.Async.WrappedAsyncResult1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48
System.Web.Mvc.Async.<>c_DisplayClass39.b_33() +57
System.Web.Mvc.Async.<>c_DisplayClass4f.b_49() +223
System.Web.Mvc.Async.<>c_DisplayClass37.b_36(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
System.Web.Mvc.Async.WrappedAsyncResult1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
System.Web.Mvc.<>c_DisplayClass1d.b_18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.<>c_DisplayClass4.b_3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult1.End() +62
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult1.End() +62
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c_DisplayClass8.b_3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.<>c_DisplayClass4.b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
The problem is on your ObjectFactory.
I don't know if you use some DI container (I can't see your code), but it can't retrieve your handlers.
You should use some kind of Typed Factory, so you could retrieve more than one commandHandler (as you do in your code).
I send you the link of my training journey into CQRS/ES: it's very similar to what you're doing and there are some articles about the journey.
I Hope it helps.

ASP.NET MVC 4 + EF5 beta2 + DbGeography types + MiniProfiler throws error

I have an MVC 4 project using EF 5 Code First. I am trying to install MiniProfile at no avail.
I pulled MiniProfiler 2.0.1 and MiniProfiler.EF 2.0.2 from NuGet, added the following on the Global.asax.cs:
protected void Application_Start()
{
MiniProfilerEF.Initialize();
}
Immediately upon run, I get this error:
The provider did not return a DbSpatialServices instance.
Line 58: if (user != null)
Line 59: {
Line 60:>>> if (user.Places != null)
Line 61: {
Line 62: var place= user.Places .OrderByDescending(x => x.CreationTime).FirstOrDefault();
[ProviderIncompatibleException: The provider did not return a DbSpatialServices instance.]
System.Data.Common.DbProviderServices.GetDbSpatialDataReader(DbDataReader fromReader, String manifestToken) +62
System.Data.Common.DbProviderServices.GetSpatialDataReader(DbDataReader fromReader, String manifestToken) +101
System.Data.Spatial.SpatialHelpers.CreateSpatialDataReader(MetadataWorkspace workspace, DbDataReader reader) +70
System.Data.Common.Internal.Materialization.Shaper.CreateSpatialDataReader() +12
System.Data.Common.Utils.Singleton`1.get_Value() +25
System.Data.Common.Internal.Materialization.Shaper.<GetSpatialPropertyValueWithErrorHandling>b__d(DbDataReader reader, Int32 column) +12
System.Data.Common.Internal.Materialization.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) +149
System.Data.Common.Internal.Materialization.Shaper.GetSpatialPropertyValueWithErrorHandling(Int32 ordinal, String propertyName, String typeName, PrimitiveTypeKind spatialTypeKind) +269
lambda_method(Closure , Shaper ) +942
System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly(Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) +239
lambda_method(Closure , Shaper ) +221
System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) +163
System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +88
System.Data.Objects.DataClasses.RelatedEnd.Merge(IEnumerable`1 collection, MergeOption mergeOption, Boolean setIsLoaded) +222
System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption) +218
System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption) +25
System.Data.Objects.DataClasses.RelatedEnd.Load() +37
System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad() +300
System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty(TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject) +85
System.Data.Objects.Internal.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item) +105
System.Data.Entity.DynamicProxies.User_C006B0EF9498157C70250EEE038C6BEADB719A2D5BDC4AA1FB567FB579AECEB5.get_Places() +55
Project.Web.Controllers.PlaceController.Ribbon() in c:\Project\Project.Web\Controllers\PlaceController.cs:60
lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +204
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +30
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +45
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +42
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +54
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +9
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__4(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +47
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +23
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +59
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.Mvc.<>c__DisplayClassa.<EndProcessRequest>b__9() +22
System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +10
System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +27
System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Action action) +64
System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult result) +71
System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1121
I have a couple of DbGeography types in my Places POCO.
public class Places
{
public Guid PlacesId { get; set; }
...
public DbGeography Location { get; set; }
public DbGeography Area { get; set; }
...
}
I have tried searching for that error but it does not show up in google anywhere.
This looks like a bug in MiniProfiler, it should be reported at: http://community.miniprofiler.com
In particular profiling EF is very tricky, a lot of the work is done by wrapping every "provider" it uses. It seems that EFProfiledDbProviderFactory needs some special logic to locate, intercept and wrap the spatial services.

Resources