GetAuthenticatedCustomer in a WPF application

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi (dmorley :-)),

I'm building a WPF app that handles order dispatched. When I try and use the nop service

When I try and use IOrderProcessingService Ship I get the exception below. It is something to do with notifying the customer because it uses - GetAuthenticatedCustomer

The code it doesn't like is

if (_httpContext == null ||
                _httpContext.Request == null ||
                !_httpContext.Request.IsAuthenticated ||
                !(_httpContext.User.Identity is FormsIdentity))

I figured it was something to do with it being a WPF app and not a web app with http context, but doesn't nop fake http context if its not there?

Darren


System.NotImplementedException was unhandled
  Message=The method or operation is not implemented.
  Source=System.Web
  StackTrace:
       at System.Web.HttpRequestBase.get_IsAuthenticated()
       at Nop.Services.Authentication.FormsAuthenticationService.GetAuthenticatedCustomer() in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Libraries\Nop.Services\Authentication\FormsAuthenticationService.cs:line 77
       at Nop.Web.Framework.WebWorkContext.GetCurrentCustomer() in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Presentation\Nop.Web.Framework\WebWorkContext.cs:line 75
       at Nop.Web.Framework.WebWorkContext.get_CurrentCustomer() in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Presentation\Nop.Web.Framework\WebWorkContext.cs:line 185
       at Nop.Web.Framework.WebWorkContext.get_WorkingLanguage() in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Presentation\Nop.Web.Framework\WebWorkContext.cs:line 241
       at Nop.Services.Localization.LocalizationExtentions.GetLocalized[T](T entity, Expression`1 keySelector) in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Libraries\Nop.Services\Localization\LocalizationExtentions.cs:line 25
       at Nop.Services.Messages.MessageTokenProvider.AddOrderTokens(IList`1 tokens, Order order, Int32 languageId) in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Libraries\Nop.Services\Messages\MessageTokenProvider.cs:line 421
       at Nop.Services.Messages.WorkflowMessageService.GenerateTokens(Order order, Int32 languageId) in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Libraries\Nop.Services\Messages\WorkflowMessageService.cs:line 112
       at Nop.Services.Messages.WorkflowMessageService.SendOrderCompletedCustomerNotification(Order order, Int32 languageId) in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Libraries\Nop.Services\Messages\WorkflowMessageService.cs:line 510
       at Nop.Services.Orders.OrderProcessingService.SetOrderStatus(Order order, OrderStatus os, Boolean notifyCustomer) in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Libraries\Nop.Services\Orders\OrderProcessingService.cs:line 214
       at Nop.Services.Orders.OrderProcessingService.CheckOrderStatus(Order order) in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Libraries\Nop.Services\Orders\OrderProcessingService.cs:line 365
       at Nop.Services.Orders.OrderProcessingService.Deliver(Order order, Boolean notifyCustomer) in C:\Users\DarrenP\Documents\Visual Studio 2010\Projects\nopCommerce2.4\Libraries\Nop.Services\Orders\OrderProcessingService.cs:line 1631
       at WpfApplication3.MainWindow.button1_Click(Object sender, RoutedEventArgs e) in C:\Users\DarrenP\documents\visual studio 2010\Projects\chapter1\WpfApplication3\WpfApplication3\MainWindow.xaml.cs:line 127
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at WpfApplication3.App.Main() in C:\Users\DarrenP\documents\visual studio 2010\Projects\chapter1\WpfApplication3\WpfApplication3\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
12 years ago
Narrowed it down to this line

!_httpContext.Request.IsAuthenticated

_httpContext.Request.IsAuthenticated = '_httpContext.Request.IsAuthenticated' threw an exception of type 'System.NotImplementedException'
12 years ago
wunpac wrote:
Narrowed it down to this line

!_httpContext.Request.IsAuthenticated

_httpContext.Request.IsAuthenticated = '_httpContext.Request.IsAuthenticated' threw an exception of type 'System.NotImplementedException'


A.M is this something you have come across in Unit Testing?
12 years ago
FakeHttpRequest does not implement IsAuthenticated property (unit tests do not use it). Open this file and add it:
public override bool IsAuthenticated
{
   get { return false; }
}
12 years ago
a.m. wrote:
FakeHttpRequest does not implement IsAuthenticated property (unit tests do not use it). Open this file and add it:
public override bool IsAuthenticated
{
   get { return false; }
}


Thank you!

Used  public bool IsAuthenticated { get { return false; } } without the override as HttpResponseBase does have the IsAuthenticated method and it works great.
12 years ago
Had an issue with

Here is the new FakeResponse Script with 2 new methods in case you want to use it in the source (will save me some work at upgrade time :-))

using System.Text;
using System.Web;

namespace Nop.Core.Fakes
{
    public class FakeHttpResponse : HttpResponseBase
    {
        private readonly HttpCookieCollection _cookies;
        private readonly StringBuilder _outputString = new StringBuilder();

       public FakeHttpResponse(HttpCookieCollection cookies)
        {
            _cookies = cookies;
            if (_cookies == null)
                _cookies = new HttpCookieCollection();
        }

        public string ResponseOutput
        {
            get { return _outputString.ToString(); }
        }

        public override int StatusCode { get; set; }

        public override string RedirectLocation { get; set; }

        public override void Write(string s)
        {
            _outputString.Append(s);
        }

        public override string ApplyAppPathModifier(string virtualPath)
        {
            return virtualPath;
        }

        public bool IsAuthenticated { get { return false; } }

        public override HttpCookieCollection Cookies
        {
            get { return _cookies; }
        }
    }
}


and obviously I changed the call to Response in FakeHTTPContext to pass _cookies

   public override HttpResponseBase Response
        {
            get
            {
                return _response ?? new FakeHttpResponse(_cookies);
            }
        }

Thanks again Andrei
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.