This is a migrated thread and some comments may be shown as answers.

ISession' does not contain a definition for 'GetObjectFromJson'

3 Answers 2103 Views
Grid
This is a migrated thread and some comments may be shown as answers.
YN
Top achievements
Rank 1
YN asked on 14 May 2019, 12:23 AM

Hi,

I'm using Kendo professional for Asp.net Core.

I got the error in "Session.GetObjectFromJson<IList< " and "Session.SetObjectAsJson( "

SeverityCodeDescriptionProjectFileLineSuppression State
ErrorCS1061'ISession' does not contain a definition for 'GetObjectFromJson' and no accessible extension method 'GetObjectFromJson' accepting a first argument of type 'ISession' could be found (are you missing a using directive or an assembly reference?)

I tried to fix by install Session from Nuget. but I can't find any package called Session in Nuget.  Thank you if you can help.

 

ProductService.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;
using Kendo.Mvc.Examples.Extensions;

namespace Kendo.Mvc.Examples.Models
{
public class ProductService : BaseService, IProductService
    {
        private static bool UpdateDatabase = false;
        private ISession _session;

        public ISession Session { get { return _session; } }

        public ProductService(IHttpContextAccessor httpContextAccessor)
        {
            _session = httpContextAccessor.HttpContext.Session;
        }

        public IEnumerable<ProductViewModel> Read()
        {
            return GetAll();
        }

        public IList<ProductViewModel> GetAll()
        {
            using (var db = GetContext())
            {
                var result = Session.GetObjectFromJson<IList<ProductViewModel>>("Products");

                if (result == null || UpdateDatabase)
                {
                    var categories = db.Categories.ToList();

                    result = db.Products.ToList().Select(product =>
                    {
                        var category = categories.First(c => product.CategoryID == c.CategoryID);

                        return new ProductViewModel
                        {
                            ProductID = product.ProductID,
                            ProductName = product.ProductName,
                            UnitPrice = product.UnitPrice.HasValue ? product.UnitPrice.Value : default(decimal),
                            UnitsInStock = product.UnitsInStock.HasValue ? product.UnitsInStock.Value : default(int),
                            QuantityPerUnit = product.QuantityPerUnit,
                            Discontinued = product.Discontinued,
                            UnitsOnOrder = product.UnitsOnOrder.HasValue ? product.UnitsOnOrder.Value : default(int),
                            CategoryID = product.CategoryID,
                            Category = new CategoryViewModel()
                            {
                                CategoryID = category.CategoryID,
                                CategoryName = category.CategoryName
                            },
                            LastSupply = DateTime.Today
                        };
                    }).ToList();

                    Session.SetObjectAsJson("Products", result);
                }

                return result;
            } 
        }

        public void Create(ProductViewModel product)
        {
            if (!UpdateDatabase)
            {
                var products = GetAll();
                var first = products.OrderByDescending(e => e.ProductID).FirstOrDefault();
                var id = (first != null) ? first.ProductID : 0;

                product.ProductID = id + 1;

                products.Insert(0, product);

                Session.SetObjectAsJson("Products", products);
            }
            else
            {
                using (var db = GetContext())
                {
                    var entity = new Product();

                    entity.ProductName = product.ProductName;
                    entity.UnitPrice = product.UnitPrice;
                    entity.UnitsInStock = (short)product.UnitsInStock;
                    entity.Discontinued = product.Discontinued;
                    entity.CategoryID = product.CategoryID;

                    if (entity.CategoryID == null)
                    {
                        entity.CategoryID = 1;
                    }

                    if (product.Category != null)
                    {
                        entity.CategoryID = product.Category.CategoryID;
                    }

                    db.Products.Add(entity);
                    db.SaveChanges();

                    product.ProductID = (int)entity.ProductID;
                }
            }
        }

        public void Update(ProductViewModel product)
        {
            if (!UpdateDatabase)
            {
                var products = GetAll();
                var target = products.FirstOrDefault(e => e.ProductID == product.ProductID);

                if (target != null)
                {
                    target.ProductName = product.ProductName;
                    target.UnitPrice = product.UnitPrice;
                    target.UnitsInStock = product.UnitsInStock;
                    target.Discontinued = product.Discontinued;
                    target.CategoryID = product.CategoryID;
                    target.Category = product.Category;
                }

                Session.SetObjectAsJson("Products", products);
            }
            else
            {
                using (var db = GetContext())
                {
                    var entity = new Product();

                    entity.ProductID = product.ProductID;
                    entity.ProductName = product.ProductName;
                    entity.UnitPrice = product.UnitPrice;
                    entity.UnitsInStock = (short)product.UnitsInStock;
                    entity.Discontinued = product.Discontinued;
                    entity.CategoryID = product.CategoryID;

                    if (product.Category != null)
                    {
                        entity.CategoryID = product.Category.CategoryID;
                    }

                    db.Products.Attach(entity);
                    db.Entry(entity).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }

        public void Destroy(ProductViewModel product)
        {
            if (!UpdateDatabase)
            {
                var products = GetAll();
                var target = products.FirstOrDefault(e => e.ProductID == product.ProductID);

                if (target != null)
                {
                    products.Remove(target);
                }

                Session.SetObjectAsJson("Products", products);
            }
            else
            {
                using (var db = GetContext())
                {
                    var entity = new Product();

                    entity.ProductID = product.ProductID;

                    db.Products.Attach(entity);

                    db.Products.Remove(entity);

                    var orderDetails = db.OrderDetails.Where(pd => pd.ProductID == entity.ProductID);

                    foreach (var orderDetail in orderDetails)
                    {
                        db.OrderDetails.Remove(orderDetail);
                    }

                    db.SaveChanges();
                }
            }
        }
    }
}

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 16 May 2019, 01:17 PM
Hi Sarah,


The error you are seeing is shown because both GetObjectFromJson and SetObjectAsJson are not part of ASP.NET Core. If the project you are working on requires the functionality of these extension methods I can suggest using the approach described in the stackoverflow thread below. It describes how to include the two methods to a project.



Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Christian
Top achievements
Rank 1
answered on 13 Oct 2019, 09:01 PM
I ran into the same problem.  What the initial post did not specifically mention is that the code comes from the DataSource->Basic Usage example on the Telerik UI for AspNet Core.  Why not provide the code in question if the example requires it to run?
0
Viktor Tachev
Telerik team
answered on 16 Oct 2019, 01:03 PM

Hello Christian,

 

Thank you for the feedback. Including a lot of additional code in the examples can be confusing to the users when the code is not directly related to the functionality showcased in the demo.

Nevertheless, we will consider including the additional code in the examples. 

 

Regards,
Viktor Tachev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
YN
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Christian
Top achievements
Rank 1
Share this question
or