REST service WCF - cast error

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 年 前
Hello,

I created a service application with a Rest service as follows:

    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class RestProductService
    {

        #region Constructors

        public RestProductService()
        {
            NopConfig.Init();
            IoC.InitializeWith(new DependencyResolverFactory());
        }

        #endregion

//.................


        [WebGet(UriTemplate = "{id}")]
        public ProductDataContract GetByProductId(string id)
        {
            var keeper = OperationContext.Current;
            OperationContext.Current = null;
      
            int productId = 0;
            ProductDataContract dataContract = null;
            Product product = null;

            if (int.TryParse(id, out productId))
            {                
                product = this.ProductService.GetProductById(productId);
//.............................
            }

//.............................


            OperationContext.Current = keeper;
            return dataContract;
        }//end of GetByProductId method

//I put this in here to use it as a property
         public IProductService ProductService
                {
                    get { return IoC.Resolve<IProductService>(); }
                }

}//end of RestProductService class



In order to use the NopCommerce methods of ProductService, I had to do this before using them:

            var keeper = OperationContext.Current;
            OperationContext.Current = null;

and before returning, I had to restore the OperationContext.Current value:

            OperationContext.Current = keeper;

Without these statements, I would get the cast error from NopObjectContext to IProductService (or to others like ICategoryService, ISettingsManager, etc) that some other people were also mentioning in their posts
e.g.

https://www.nopcommerce.com/boards/t/8378/unable-to-cast-object-of-type-nopcommercebusinesslogicdatanopobjectcontext-to-type-nopcommercebusinesslogicconfigurationsettingsisettingmana.aspx

https://www.nopcommerce.com/boards/t/8005/how-to-use-iocresolve-from-wcf.aspx


I also created a normal service and using the ProductService methods worked without any problem!

My question is: why this cast problem for wcf rest services? My fix by making null the OperationContext.Current (which is not really a fix though) suggests maybe a problem in the BusinessLogic/Infrastructure/UnityPerExecutionContextLifetimeManager or something related?

Looking forward to hearing from you guys,

Ada
13 年 前
I have experienced similiar problems. Pinned it down to GetValue() method of UnityPerExecutionContextLifetimeManager.cs
which it seems is responsible for resolving dependencies (locating services from interface)

The code I encountered was this:

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

            if (OperationContext.Current != null)
            {
                //WCF without HttpContext environment
                ContainerExtension containerExtension = OperationContext.Current.Extensions.Find<ContainerExtension>();
                if (containerExtension != null)
                {
                    result = containerExtension.Value;
                }
            }
            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());
                }
            }

It seems like there is some support for setting up the context through OperationContext instead og HttpContext, but I couldnt find out how. So I ended up modifying the code to check for HttpContext first.

I also had to do some other modifications to the source to get Identity set in HttpContext.Current and to setup nopContext.
13 年 前
Thanks for replying :)

You mentioned "I also had to do some other modifications to the source to get Identity set in HttpContext.Current and to setup nopContext." Would you care to share? :)
13 年 前
I too am interested in this.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.