Base solution for your next web application

Activities of "djfuzzy"

In the Unit Testing tutorial, it is mentioned that we use real implementations of dependencies when unit testing. What if we do not want to do this? For instance, I have an application service method that queues a background job (using the default background job manager). When I am unit testing this method, I do not want the job to actually be queued (in this case, I am queuing a job to send out an email). I only want to verify that JobManager.EnqueueAsync is called by my service method. I tried playing around with Moq and Castle to get it to work but my JobManager is always the real implementation of it. I could always stub out the call to JobManager.EnqueueAsync, and set a flag that I can check to verify it has been called but then I'm not sure how to do that when its recommended to use Resolve <IMyApplicationService>() to obtain a reference to my application service. I appreciate any advice!

I have a DTO that includes validation including some custom validation, but it is not being caught and thrown. I thought these should be thrown automatically. Here is my DTO:

public class CreateUserInput : ICustomValidate
    {
        [Required]
        [StringLength(User.MaxNicknameLength, MinimumLength = User.MinNicknameLength)]
        public string Nickname { get; set; }
        
        [Required]
        [EmailAddress]
        [StringLength(AbpUserBase.MaxEmailAddressLength)]
        public string EmailAddress { get; set; }

        [Required]
        [DisableAuditing]
        public string PasswordHashed { get; set; }

        public bool IsActive { get; set; }

        public void AddValidationErrors(List<ValidationResult> results)
        {
            if (Regex.IsMatch(Nickname, User.NicknameFormatValidationRegex))
            {
                throw new UserFriendlyException(string.Format("Nickname must be between {0} and {1} characters, may " +
                    "only contain letters, numbers, spaces, hyphens, underscores and periods, and cannot begin or end " +
                    "with spaces.", User.MinNicknameLength, User.MaxNicknameLength));
            }
        }
}

I have a view model that has some validation attributes:

public class SignUpViewModel
    {
        [Required]
        [StringLength(User.MaxNicknameLength, MinimumLength = User.MinNicknameLength)]
        public string Nickname { get; set; }

        [Required]
        [EmailAddress]
        [StringLength(User.MaxEmailAddressLength)]
        public string EmailAddress { get; set; }

        [StringLength(User.MaxPlainPasswordLength, MinimumLength = User.MinPlainPasswordLength)]
        [DisableAuditing]
        public string Password { get; set; }

        public bool IsExternalLogin { get; set; }
    }

However, if validation fails, before the controller action method is called, which would call the CheckModelState method, an exception is thrown:

Server Error in '/' Application.

Method arguments are not valid! See ValidationErrors for details.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Abp.Runtime.Validation.AbpValidationException: Method arguments are not valid! See ValidationErrors for details.

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:

[AbpValidationException: Method arguments are not valid! See ValidationErrors for details.] Abp.Runtime.Validation.Interception.MethodInvocationValidator.Validate() in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Validation\Interception\MethodInvocationValidator.cs:92 Abp.Web.Mvc.Validation.AbpMvcValidationFilter.OnActionExecuting(ActionExecutingContext filterContext) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.Web.Mvc\Web\Mvc\Validation\AbpMvcValidationFilter.cs:35 System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +176 System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +644 System.Web.Mvc.Async.AsyncInvocationWithFilters.InvokeActionMethodFilterAsynchronouslyRecursive(Int32 filterIndex) +644 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__31(AsyncCallback asyncCallback, Object asyncState) +58 System.Web.Mvc.Async.WrappedAsyncResult1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 parameters, AsyncCallback callback, Object state) +197 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743 System.Web.Mvc.Async.WrappedAsyncResult1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343 System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465 Castle.Proxies.AccountControllerProxy.BeginExecuteCore_callback(AsyncCallback callback, Object state) +8 Castle.Proxies.Invocations.Controller_BeginExecuteCore.InvokeMethodOnTarget() +80 Castle.DynamicProxy.AbstractInvocation.Proceed() +80 Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:29 Castle.DynamicProxy.AbstractInvocation.Proceed() +108 Castle.Proxies.AccountControllerProxy.BeginExecuteCore(AsyncCallback callback, Object state) +160 System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374 Castle.Proxies.AccountControllerProxy.BeginExecute_callback(RequestContext requestContext, AsyncCallback callback, Object state) +12 Castle.Proxies.Invocations.Controller_BeginExecute.InvokeMethodOnTarget() +131 Castle.DynamicProxy.AbstractInvocation.Proceed() +80 Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:29 Castle.DynamicProxy.AbstractInvocation.Proceed() +108 Castle.Proxies.AccountControllerProxy.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +174 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1586.0

I'm confused why validation is happening before the controller action method is called and how can I catch this exception to display a user friendly error instead?

Hi, I was wondering if you can steer me in the right direction towards being able to lookup localized texts in models, whether its an entity model, DTO or view model. The ABP documentation mentions that we should be doing validation at the domain/entity level, but I can't seem to figure out how to lookup a localized text instead of using a hard-coded string. Thanks in advance!

I want to store some user information in the session using claims after they log in but I am having a problem. This user information is in a Client entity that I am passing some info to a ClientDto that I have added to CurrentLoginInformationsOutput. Consider the following code:

public async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "")
        {
            ...
            await SignInAsync(loginResult.User, loginResult.Identity, loginModel.RememberMe, loginModel.IsClientLogin);            

            if (loginModel.IsClientLogin)
            {
                var loginInformations = await _sessionAppService.GetCurrentLoginInformations();
                if (loginInformations.Client == null)
                    throw new UserFriendlyException(string.Format("There is no client associated with the user with Id {0}.",
                        loginResult.User.Id));
                // Do something with client information 
            }
            ...
            return Json(new MvcAjaxResponse { TargetUrl = returnUrl });
        }

When _sessionAppService.GetCurrentLoginInformations() is called, I am getting the following exception:

WARN 2016-07-26 15:32:22,220 [44 ] Abp.Logging.LogHelper - Can not find 'CurrentUserDidNotLoginToTheApplication' in localization source 'Abp'! ERROR 2016-07-26 15:32:26,347 [51 ] rtgage.Web.Controllers.AccountController - [CurrentUserDidNotLoginToTheApplication] Abp.Authorization.AbpAuthorizationException: [CurrentUserDidNotLoginToTheApplication] at Abp.Authorization.AuthorizeAttributeHelper.<AuthorizeAsync>d__13.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\AuthorizeAttributeHelper.cs:line 29 --- 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 Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task) at Nito.AsyncEx.AsyncContext.<>c__DisplayClass3.<Run>b__1(Task t) at System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- 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 Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task) at Nito.AsyncEx.AsyncContext.Run(Func1 action) at Abp.Threading.AsyncHelper.RunSync(Func1 action) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Threading\AsyncHelper.cs:line 42 at Abp.Authorization.AuthorizeAttributeHelper.Authorize(IEnumerable1 authorizeAttributes) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\AuthorizeAttributeHelper.cs:line 45 at Abp.Authorization.Interceptors.AuthorizationInterceptor.Authorize(IEnumerable1 authorizeAttributes) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\Interceptors\AuthorizationInterceptor.cs:line 86 at Abp.Authorization.Interceptors.AuthorizationInterceptor.InterceptSync(IInvocation invocation, IEnumerable1 authorizeAttributes) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\Interceptors\AuthorizationInterceptor.cs:line 78 at Abp.Authorization.Interceptors.AuthorizationInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\Interceptors\AuthorizationInterceptor.cs:line 43 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 62 at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 41 at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Auditing.AuditingInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Auditing\AuditingInterceptor.cs:line 52 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Runtime.Validation.Interception.ValidationInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Validation\Interception\ValidationInterceptor.cs:line 33 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.SessionAppServiceProxy.GetCurrentLoginInformations() at NextMortgage.Web.Controllers.AccountController.<Login>d__12.MoveNext() in C:\Users\Dillon\Source\Repos\NextMortgage\NextMortgage.Web\Controllers\AccountController.cs:line 118 --- 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.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) at System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.

For one thing, I do have 'CurrentUserDidNotLoginToTheApplication' in my localization source. Secondly, how is my user not logged in yet at this point despite the SignInAsync() successfully completing? Any help would be appreciated.

Showing 1 to 5 of 5 entries