Unable to cast object of type 'NopCommerce.BusinessLogic.Data.NopObjectContext' to type 'NopCommerce.BusinessLogic.Configuration.Settings.ISettingMana

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 anni tempo fa
Hi,

I create a WCF service application separate project to be able to authenticate my user, and I gave all references I need about NopCommerce.

this is my service method;
public bool IsAuthenticate(string email, string password)
        {            
            return IoC.Resolve<ICustomerService>();
        }

If username exists then nopcommerce wants to check the password with this method;

private string CreatePasswordHash(string password, string salt)
        {
            //MD5, SHA1
           string passwordFormat = IoC.Resolve<ISettingManager>().GetSettingValue("Security.PasswordFormat");
            if (String.IsNullOrEmpty(passwordFormat))
                passwordFormat = "SHA1";

            return FormsAuthentication.HashPasswordForStoringInConfigFile(password + salt, passwordFormat);
        }

But while this line with bold one row running , it's giving me the exception below;

Exception Message:
Unable to cast object of type 'NopCommerce.BusinessLogic.Data.NopObjectContext' to type 'NopCommerce.BusinessLogic.Configuration.Settings.ISettingManager'.

Exception StackTrace:
at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, ResolverOverride[] overrides)
   at NopSolutions.NopCommerce.BusinessLogic.Infrastructure.UnityDependencyResolver.Resolve[T]() in G:\Projects\PrivateShop\Web\Nop19\Libraries\Nop.BusinessLogic\Infrastructure\UnityDependencyResolver.cs:line 238
   at NopSolutions.NopCommerce.BusinessLogic.Infrastructure.IoC.Resolve[T]() in G:\Projects\PrivateShop\Web\Nop19\Libraries\Nop.BusinessLogic\Infrastructure\IoC.cs:line 77
   at NopSolutions.NopCommerce.BusinessLogic.CustomerManagement.CustomerService.CreatePasswordHash(String password, String salt) in G:\Projects\PrivateShop\Web\Nop19\Libraries\Nop.BusinessLogic\Customer\CustomerService.cs:line 122
   at NopSolutions.NopCommerce.BusinessLogic.CustomerManagement.CustomerService.Login(String email, String password) in G:\Projects\PrivateShop\Web\Nop19\Libraries\Nop.BusinessLogic\Customer\CustomerService.cs:line 1453
   at NopServiceLayer.ZenService.IsAuthenticate(String email, String password) in G:\Projects\PrivateShop\Web\Nop19\NopServiceLayer\ZenService.svc.cs:line 21
   at SyncInvokeIsAuthenticate(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)



Please help me about it? What shuold i do? I hope someone knows this issue.
13 anni tempo fa
Hello,

It is unclear from your question what it is you're trying to do or which code you've modified that might be causing this issue.  Did you change the Unity configuration in UnityDependencyResolver?
13 anni tempo fa
I have similar problem. When I put a WCF service (.svc.cs file) in NopCommerceStore project, the second IoC.Resolve<T>() call in the service throws the exception 'Unable to cast object of type 'NopSolutions.NopCommerce.BusinessLogic.Data.NopObjectContext' to type 'T'. The first call works.

The same code in a .aspx* files works.

Can you help me?
13 anni tempo fa
I also had this problem when accessing IoC.Resolve from WCF hosted inside the NopCommerceStore application.

The workarround I used to solve this was to change the class UnityPerExecutionContextLifetimeManager in the NopSolutions.NopCommerce.BusinessLogic.Infrastructure namespace, concretely the GetValue.

This method uses OperationContext when accessing through WCF and HttpContext when accessing through asp.net, but (i guess because the wcf services are hosted inside the web app), the information for me were still comming inside the HttpContext, so I changed the if blocks order to this:


public override object GetValue()
{
        object result = null;

        //Get object depending on  execution environment ( WCF without HttpContext,HttpContext or CallContext)

        if (HttpContext.Current != null)
        {
                //HttpContext available ( ASP.NET ..)
                if (HttpContext.Current.Items[_key.ToString()] != null)
                result = HttpContext.Current.Items[_key.ToString()];
        }
        else if (OperationContext.Current != null)
        {
                //WCF without HttpContext environment
                ContainerExtension containerExtension = OperationContext.Current.Extensions.Find<ContainerExtension>();
                
                if (containerExtension != null)
                {
                        result = containerExtension.Value;
                }
        }
        else
        {
                //Not in WCF or ASP.NET Environment, UnitTesting, WinForms, WPF etc.
                if (AppDomain.CurrentDomain.IsFullyTrusted)
                {
                        //ensure that we're in full trust
                        result = CallContext.GetData(_key.ToString());
                }
        }

        return result;
}


This way works fine for me. You should also do the same change on the rest of the UnityPerExecutionContextLifetimeManager class methods (RemoveValue and SetValue) for coherence.

Hope that helps!
13 anni tempo fa
The container extension storage mechanism is used for the WCF context but it seems to be flawed. In its existing form it just stores one value and that keeps getting overwritten as more values are added. In effect only the last value is being cached. This is why the resolution of first service succeeds and second onwards fail. In my case, I updated the lifetime manager to  store multiple values as key value pairs in a dictionary inside the container extension.

Here is the container extension code:

    class MultiValueContainerExtension : IExtension<OperationContext>
    {
        public IDictionary<string, object> Items { get; set; }

        public void Attach(OperationContext owner)
        {
            Items = new Dictionary<string, object>();
        }

        public void Detach(OperationContext owner){ }
    }

UnityPerExecutionLifetimeManager can be modified to use the new container extension:
public override object GetValue()
        {
            object result = null;

            //Get object depending on  execution environment ( WCF without HttpContext,HttpContext or CallContext)

           if (OperationContext.Current != null)
            {
                //WCF without HttpContext environment
                MultiValueContainerExtension containerExtension = OperationContext.Current.Extensions.Find<MultiValueContainerExtension >();
                if (containerExtension != null)
                {
                    if(containerExtension.Items.ContainsKey(_key.ToString()))
                    {
                        result = containerExtension.Items[_key.ToString()];
                    }
                }
            }

            else if (HttpContext.Current != null)
            {
                //HttpContext available ( ASP.NET ..)
                if (HttpContext.Current.Items[_key.ToString()] != null)
                    result = HttpContext.Current.Items[_key.ToString()];
            }
            else
            {
                //Not in WCF or ASP.NET Environment, UnitTesting, WinForms, WPF etc.
                if (AppDomain.CurrentDomain.IsFullyTrusted)
                {
                    //ensure that we're in full trust
                    result = CallContext.GetData(_key.ToString());
                }
            }
            return result;
        }

public override void RemoveValue()
        {
           if (OperationContext.Current != null)
            {
                //WCF without HttpContext environment
                MultiValueContainerExtension containerExtension = OperationContext.Current.Extensions.Find<MultiValueContainerExtension >();
                if (containerExtension != null)
                {
                    if (containerExtension.Items.ContainsKey(_key.ToString()))
                    {
                        containerExtension.Items.Remove(_key.ToString());
                    }
                }

            }

            else if (HttpContext.Current != null)
            {
                //HttpContext avaiable ( ASP.NET ..)
                if (HttpContext.Current.Items[_key.ToString()] != null)
                    HttpContext.Current.Items[_key.ToString()] = null;
            }
            else
            {
                //Not in WCF or ASP.NET Environment, UnitTesting, WinForms, WPF etc.
                CallContext.FreeNamedDataSlot(_key.ToString());
            }
        }

public override void SetValue(object newValue)
        {

           if (OperationContext.Current != null)
            {
                //WCF without HttpContext environment
                MultiValueContainerExtension containerExtension = OperationContext.Current.Extensions.Find<MultiValueContainerExtension >();
                if (containerExtension == null)
                {
                    containerExtension = new MultiValueContainerExtension ();
                    OperationContext.Current.Extensions.Add(containerExtension);
                }
                containerExtension.Items[_key.ToString()] = newValue;
            }

            else if (HttpContext.Current != null)
            {
                //HttpContext avaiable ( ASP.NET ..)
                if (HttpContext.Current.Items[_key.ToString()] == null)
                    HttpContext.Current.Items[_key.ToString()] = newValue;
            }
            else
            {
                //Not in WCF or ASP.NET Environment, UnitTesting, WinForms, WPF etc.
                if (AppDomain.CurrentDomain.IsFullyTrusted)
                {
                    //ensure that we're in full trust
                    CallContext.SetData(_key.ToString(), newValue);
                }
            }
        }
12 anni tempo fa
HI,

using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

I am unable to find this namespace or folder in Nopcommerce source version. I try to find in 1.6,1.7,1.8,1.9 version. But unable to find the source code of this Part.


Could you please guide me where i will get the source code of above refrence and which version.

Thanks,
-Ajit
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.