0

Hi im using razor pages asp.net c# and Im trying to use System.Web.Extensions and using System.Web.Script; and using System.Web.Script.Serialization; Because if i remove these references i get other errors:

'error CS0103: The name 'CommandType' does not exist in the current context'

'error CS0246: The type or namespace name 'JavaScriptSerializer' could not be found'

I tried looking on internet for solutions but ive had no success. Here is my code, hopefully you guys can help me

using Microsoft.AspNetCore.SignalR;
using System;
using System.Web;
using System.Web.Extensions;
using System.Web.Script;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using myWebApp.Database;
using myWebApp.Models;
using myWebApp.Pages;
using myWebApp.Hubs;
using Npgsql;

namespace myWebApp.Controllers
{
    public class SpeedListener :Controller
    {
        private IHubContext<speedalarmhub> _hubContext;
        private IMemoryCache _cache;
        public SpeedListener(IHubContext<speedalarmhub> hubContext,IMemoryCache cache)
        {
            _hubContext = hubContext;
            _cache = cache; 
        }
        public string cs = Database.Database.Connector();
        public void ListenForAlarmNotifications()
        {
            NpgsqlConnection conn = new NpgsqlConnection(cs);
            conn.StateChange += conn_StateChange;
            conn.Open();
            var listenCommand = conn.CreateCommand();
            listenCommand.CommandText = $"listen notifyalarmspeed;";
            listenCommand.ExecuteNonQuery();
            conn.Notification += PostgresNotificationReceived;
            _hubContext.Clients.All.SendAsync(this.GetAlarmList());
            while (true)
            {
                conn.Wait();
            }
        }
        private void PostgresNotificationReceived(object sender, NpgsqlNotificationEventArgs e)
        {

            string actionName = e.Payload.ToString();
            string actionType = "";
            if (actionName.Contains("INSERT"))
            {
                actionType = "Insert";
            }
            _hubContext.Clients.All.SendAsync("ReceiveMessage", this.GetAlarmList());
        }
        public string GetAlarmList()
        {
            var AlarmList = new List<string>();
            using (NpgsqlCommand sqlCmd = new NpgsqlCommand())
            {
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.CommandText = "sp_alarm_speed_process_get";
                NpgsqlConnection conn = new NpgsqlConnection(cs);
                conn.Open();
                sqlCmd.Connection = conn;
                using (NpgsqlDataReader reader = sqlCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        NotificationModel model = new NotificationModel();
                        
                    // you must fill  your model items
                        AlarmList.Add(model.Bericht);
                    }
                    reader.Close();
                    conn.Close();
                }



            }
            _cache.Set("SpeedAlarm", SerializeObjectToJson(AlarmList));
            return _cache.Get("SpeedAlarm").ToString();
        }
        public String SerializeObjectToJson(Object alarmspeed)
        {
            try
            {
                var jss = new JavaScriptSerializer();
                return  jss.Serialize(alarmspeed);
            }
            catch (Exception) { return null; }
        }
        private void conn_StateChange(object sender, System.Data.StateChangeEventArgs e)
        {

            _hubContext.Clients.All.SendAsync("Current State: " + e.CurrentState.ToString() + " Original State: " + e.OriginalState.ToString(), "connection state changed");
        }
    }
    

}

ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
anonD
  • 47
  • 8
  • Yes, `JavaScriptSerializer` is part of `System.Web.Extensions`. So what's the question? https://referencesource.microsoft.com/#System.Web.Extensions/Script/Serialization/JavaScriptSerializer.cs,d0a9bc29c74591f9 – Marshal Dec 15 '20 at 11:44
  • 1
    You can use `Newtonsoft.Json` for serialization, if you don't want to include the libraries you mentioned. https://stackoverflow.com/a/45687971/395500 – Marshal Dec 15 '20 at 11:46
  • I think you need add reference to some dlls: https://stackoverflow.com/questions/4520569/where-can-i-find-the-assembly-system-web-extensions-dll – apocalypse Dec 15 '20 at 12:01

1 Answers1

1

'error CS0103: The name 'CommandType' does not exist in the current context'

To fix this error, you can try to add:

using System.Data;

'error CS0246: The type or namespace name 'JavaScriptSerializer' could not be found'

For JSON serialization in ASP.NET Core applications, as @Marshal mentioned in comment, Newtonsoft.Json could be used.

And since .NET Core 3, the default JSON serializer for ASP.NET Core is now System.Text.Json that would be higher performance than Newtonsoft.Json.

return System.Text.Json.JsonSerializer.Serialize(alarmspeed);

Note: System.Text.Json may not provide complete parity with Newtonsoft.Json features that list in following doc, you can check it for detailed information.

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-core-3-1#table-of-differences-between-newtonsoftjson-and-systemtextjson

Fei Han
  • 26,415
  • 1
  • 30
  • 41