Using Topshelf and Quartz.NET - c#

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.

Related

MSAL TokenCacheHelper.cs failes to build because of "Protect" and "Unprotect"

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.

How to resolve error On List collection it's can't accept or execut ToList() method?

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.

AddAsync() method is missing

I am using Entity Framework Core. I want to create an async method which will create new user in my database, i have included all the libraries I need, but some methods that are supposed to work with database are missing, I have almost every async method but i am missing AddAsync and RemoveAsync. When I type AddAsync manually I get the following error message: "Error CS1061 'DbSet' does not contain a definition for 'AddAsync' and no accessible extension method 'AddAsync' accepting a first argument of type 'DbSet' could be found (are you missing a using directive or an assembly reference?)"
The class where the method is created has following code and libraries included:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data;
using Microsoft.EntityFrameworkCore;
namespace ClassLibrary
{
public class Class1 : Interface1
{
public async Task AddKorisnik(Korisnici k)
{
using (ExtentEntities context = new ExtentEntities())
{
context.Korisnici.AddAsync();
await context.SaveChangesAsync();
}
}
}
}
The class where the DbContext is used is following:
namespace ClassLibrary
{
using System;
using System.Linq;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class ExtentEntities : DbContext
{
public ExtentEntities()
: base("name=ExtentEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Korisnici> Korisnici { get; set; }
}
}
There is nothing asynchronous with adding an object to an in-memory DbSet<T>. You should use the synchronous Add method to do this.
SaveChangesAsync() is the method that actually connects asynchronously to the underlying database.

Namespace not found error when trying to compile

Edit: Changing the class name or namespace name so that the namespace and class don't have the same name doesn't fix this issue.
I have this super simple code:
Program.cs:
using System;
using CodeCleaner;
class Program
{
private static void Main(string[] args)
{
Console.WriteLine("DocTypeChecker instantiated");
var codeCleaner = new CodeCleaner.CodeCleaner();
}
}
CodeCleaner.cs:
using System;
namespace CodeCleaner
{
public class CodeCleaner
{
public CodeCleaner()
{
Console.WriteLine("CodeCleaner instantiated");
}
}
}
This produces the following error when I try to compile when I run $ csc Program.cs:
Program.cs(4,7): error CS0246: The type or namespace name 'CodeCleaner' could not be found (are you missing a using directive or an assembly reference?).
I'm definitely not missing a using directive for CodeCleaner, but what it means with assembly reference I have no idea. Other solutions on the web didn't help me in this case. Anyone know the issue here?
Running csc Program.cs won't compile CodeCleaner.cs, so your assembly will be missing the CodeCleaner class and namespace. Using csc Program.cs CodeCleaner.cs should do the trick.

Xamarin Android - Type or namespace "Context" could not be found

I'm following this Xamarin tutorial. Visual Studio throws an error when I use the "Context" class in my Android project. Shouldn't this class be included in my Android app by default?
Also, I defined an interface named "IStreamLoader" in a Portable Class Library called "Portable", and added a reference to "Portable" in my Android project. But referencing "IStreamLoader" in my Android project throws another error.
Are these two errors related?
Errors
CS0246 The type or namespace name 'IStreamLoader' could not be found (are you missing a using directive or an assembly reference?)
CS0246 The type or namespace name 'Context' could not be found (are you missing a using directive or an assembly reference?)
CS0246 The type or namespace name 'Context' could not be found (are you missing a using directive or an assembly reference?)
MyTunes.Droid\MainActivity.cs
using System.Linq;
using Android.App;
using Android.OS;
namespace MyTunes
{
[Activity(Label = "My Tunes", MainLauncher = true)]
public class MainActivity : ListActivity
{
protected async override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var data = await SongLoader.Load();
ListAdapter = new ListAdapter<Song>() {
DataSource = data.ToList(),
TextProc = s => s.Name,
DetailTextProc = s => s.Artist + " - " + s.Album
};
}
}
public class StreamLoader : IStreamLoader
{
private readonly Context context;
public StreamLoader(Context context)
{
this.context = context;
}
public Stream GetStreamForFilename(string filename)
{
return context.Assets.Open(filename);
}
}
}
Portable\IStreamLoader.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Portable
{
public interface IStreamLoader
{
System.IO.Stream GetStreamForFilename(string filename);
}
}
Try this :
using Android.Content;

Categories