I am working on .Net razor pages. I have created a form on submit of form i am handling it using a post handler in .cs file but i am getting an error. I can add AddAsync in my handeler. Error is:-
Severity Code Description Project File Line Suppression State
Error CS1061 'object' does not contain a definition for 'AddAsync' and no accessible extension method 'AddAsync' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) youtubeproject C:\Users\lenovo\Desktop\MXN_INTERN\youtubeproject\youtubeproject\Pages\Categories\Create.cshtml.cs 22 Active
Here is my code of .cs file
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using youtubeproject.Model;
using youtubeproject.Data;
namespace youtubeproject.Pages.Categories
{
public class CreateModel : PageModel
{
public readonly ApolicationDbContext _db;
[BindProperty]
public Category Category { get; set; }
public CreateModel(ApolicationDbContext db)
{
_db = db;
}
public void OnGet()
{
}
public async Task<IActionResult>OnPost()
{
await _db.Category.AddAsync(Category);
await _db.SaveChangesAsync();
return RedirectToPage("Index");
}
}
}
Related
I'm am trying to use the provided C# MS Graph/MSAL application to create an application that utilizes the OneDrive API. I literally copy/pasted from the provided example code into my solution and I can't get the solution to build now.
I've ensured that the following NuGet packages are installed:
Microsoft.Graph
Microsoft.Identity.Client
Microsoft.Identity.Client.Desktop
Microsoft.Identity.Client.Extensions.Msal
Microsoft.Identity.Web.TokenCache
Microsoft.Windows.SDK.Contracts
I'm brand new to using MSAL and I'm sure this is just a dumb mistake on my part but any assistance would be greatly appreciated!
Error Received:
Severity Code Description Project File Line Suppression State
Error CS1061 'object' does not contain a definition for 'Unprotect' and no accessible extension method 'Unprotect' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) Test Case Designer D:\code\Test Case Designer\TokenCacheHelper.cs 54 Active
Severity Code Description Project File Line Suppression State
Error CS1061 'object' does not contain a definition for 'Protect' and no accessible extension method 'Protect' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) Test Case Designer D:\code\Test Case Designer\TokenCacheHelper.cs 70 Active
using System.IO;
using System.Security.Cryptography;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Desktop;
using System.Windows;
using System.Security;
using Microsoft.Identity.Web.TokenCacheProviders;
using Microsoft.Identity.Client.Extensions.Msal;
namespace Test_Case_Designer
{
static class TokenCacheHelper
{
// <summary>
// Path to the token cache
// </summary>
public static string CacheFilePath { get; private set; }
public static object ProtectedData { get; private set; }
private static readonly object FileLock = new object();
private static void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
lock (FileLock)
{
args.TokenCache.DeserializeMsalV3(File.Exists(CacheFilePath)
? ProtectedData.Unprotect(File.ReadAllBytes(CacheFilePath),
null,
DataProtectionScope.CurrentUser)
: null);
}
}
public static void AfterAccessNotification(TokenCacheNotificationArgs args)
{
// if the access operation resulted in a cache update
if (args.HasStateChanged)
{
lock (FileLock)
{
// reflect changes in the persistent store
File.WriteAllBytes(CacheFilePath,
ProtectedData.Protect(args.TokenCache.SerializeMsalV3(),
null,
DataProtectionScope.CurrentUser)
);
}
}
}
internal static void EnableSerialization(ITokenCache tokenCache)
{
tokenCache.SetBeforeAccess(BeforeAccessNotification);
tokenCache.SetAfterAccess(AfterAccessNotification);
}
}
}
In your code, ProtectedData is defined as an object.
This should have been System.Security.Cryptography.ProtectedData
change
public static object ProtectedData { get; private set; }
to
private static ProtectedData protectedData;
public static ProtectedData GetProtectedData(){
return protectedData;
}
should fix your issue.
I added a class library to an API project to get the data from database. In the class library "DataManager" I created a folder named "Entity" and added ADO entity data models for for sql and oracle db. When I was creating the ADO for oracle I had to degrade entity framework version from 6 to 5.
Models.Context.cs
namespace DataManger.Entity
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class myEntities : DbContext
{
public myEntities()
: base("name=myEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Application> Applications { get; set; }
Now when I try to get any of the models in "Entity".
using DataManager.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DataManager.Services
{
public class ApplicationDataService : IApplicationDataService
{
I get the following error message in the first line when using DataManager.Entity
Severity Code Description Project File Line Suppression State
Error CS0234 The type or namespace name 'Entity' does not exist in the namespace 'DataManager' (are you missing an assembly reference?) DataManger ......
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using MahadevBhaktDB;
using MahadevBhaktModal;
namespace MahadevBhakt_Dewasi_.Controllers
{
public class AdminController : Controller
{
public ActionResult Index()
{
return View();
}
public List<Tab_Country> GetCountryList()
{
DSPDatabaseEntities db = new DSPDatabaseEntities();
List<Tab Country> tab_Countries = db.tab_Countries.Tolist();
return tab_Countries;
}
}
}
'object' does not contain a definition for 'ToList' and no accessible extension method 'ToList' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) .
This kind of error is come and but i try to import some packages but there is not available so how to pick that kinds of package in my project.
I'm having problems setting up a basic Quartz.NET and Topshelf integration.
Errors appear when accessing ScheduleQuartzJob:
Error 1 Delegate 'System.Func<ServiceTest.MyService>' does not take 1 arguments
Error 2 Not all code paths return a value in lambda expression of type 'System.Func<Topshelf.Runtime.HostSettings,ServiceTest.MyService>'
Error 3 'Topshelf.Runtime.HostSettings' does not contain a definition for 'ConstructUsing' and the best extension method overload 'Topshelf.ServiceConfiguratorExtensions.ConstructUsing<T>(Topshelf.ServiceConfigurators.ServiceConfigurator<T>, System.Func<T>)' has some invalid arguments
Error 4 Instance argument: cannot convert from 'Topshelf.Runtime.HostSettings' to 'Topshelf.ServiceConfigurators.ServiceConfigurator<ServiceTest.MyService>'
Error 5 'Topshelf.Runtime.HostSettings' does not contain a definition for 'WhenStarted' and no extension method 'WhenStarted' accepting a first argument of type 'Topshelf.Runtime.HostSettings' could be found (are you missing a using directive or an assembly reference?)
Error 6 'Topshelf.Runtime.HostSettings' does not contain a definition for 'WhenStopped' and no extension method 'WhenStopped' accepting a first argument of type 'Topshelf.Runtime.HostSettings' could be found (are you missing a using directive or an assembly reference?)
Error 7 'Topshelf.ServiceConfigurators.ServiceConfigurator<ServiceTest.MyService>' does not contain a definition for 'ScheduleQuartzJob' and no extension method 'ScheduleQuartzJob' accepting a first argument of type 'Topshelf.ServiceConfigurators.ServiceConfigurator<ServiceTest.MyService>' could be found (are you missing a using directive or an assembly reference?)
I think I am missing something obvious here...
Side question: Is there a simpler way to set up a Quartz/Topshelf configuration?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using NLog;
using Quartz;
using Quartz.Impl;
using Common.Logging.Configuration;
using Common.Logging.NLog;
using Topshelf;
namespace ServiceTest
{
public class MyService
{
public bool Start(HostControl control)
{
return true;
}
public bool Stop(HostControl control)
{
return true;
}
}
public class Program
{
private static Logger log = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
var config = new NameValueCollection();
var adaptor = new NLogLoggerFactoryAdapter(config);
Common.Logging.LogManager.Adapter = adaptor;
HostFactory.Run(x =>
{
x.Service<MyService>(sc =>
{
sc.ConstructUsing(() => new MyService());
sc.WhenStarted((service, control) => service.Start(control));
sc.WhenStopped((service, control) => service.Stop(control));
sc.ScheduleQuartzJob(q =>
q.WithJob(() =>
JobBuilder.Create<MyJob>().Build())
.AddTrigger(() => TriggerBuilder.Create()
.WithSimpleSchedule(b => b
.WithIntervalInSeconds(10)
.RepeatForever())
.Build()));
});
x.RunAsLocalSystem()
.DependsOnEventLog()
.StartAutomatically()
.EnableServiceRecovery(rc => rc.RestartService(1));
x.SetDescription("Checker Service");
x.SetDisplayName("Checker Service");
x.SetServiceName("CheckerService");
x.UseNLog();
});
}
}
public class MyJob : IJob
{
private static Logger log = LogManager.GetCurrentClassLogger();
public void Execute(IJobExecutionContext context)
{
log.Info("It is {0} and all is well", DateTime.UtcNow);
}
}
}
Needed to add a reference to Topshelf.Quartz!
I knew it was something stupid and simple.
Got a Noob question. I am trying to write a C# MVC Application to connect to an MS SQL DB using the Entity Framework. But I am getting the following error.
Severity Code Description Project File Line Suppression State
Error CS1061 'DatabaseFacade' does not contain a definition for 'SqlQuery' and no extension method 'SqlQuery' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?) HAPI.DNX 4.5.1, HAPI.DNX Core 5.0
My Code is as follows
DB Context Class which flags no issues.
using Microsoft.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Something.Models
{
public class RulesDbContext: DbContext
{
public DbSet<Rules> Rules { get; set; }
}
}
Calling Class with errors on the Sql Statements highlighted in bold
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.Entity;
namespace Something.Models
{
public class ReadRules
{
private RulesDbContext db = new RulesDbContext();
private void Rules()
{
List<Rules> rulesMeta = db.Database.**SqlQuery**<Rules>("GetRuleMetaData #deviceID",
new **SqlParameter**("deviceID", deviceID)).ToList();
}
}
}