Im trying to use sonarqube from a Frosting cake project.
There are my tasks:
using Cake.Common;
using Cake.Common.IO;
using Cake.Common.Tools.DotNetCore;
using Cake.Common.Tools.DotNetCore.Build;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Docker;
using Cake.Frosting;
using Cake.Git;
using Cake.SemVer;
using Cake.Sonar;
using System.Linq;
public static class Program
{
public static int Main(string[] args)
{
return new CakeHost()
.UseContext<BuildContext>()
.Run(args);
}
}
public class BuildContext : FrostingContext
{
public string MsBuildConfiguration { get; set; }
public string ProjectPath { get; set; }
public string SolutionFile { get; set; }
public string SolutionName { get; set; }
// For SonarQube
public string SonarKey { get; set; }
public string SonarUrl { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string Verbose { get; set; }
public BuildContext(ICakeContext context)
: base(context)
{
ProjectPath = context.Argument("projectPath", #"C:\Users\User\source\repos\ConsoleApp1");
MsBuildConfiguration = context.Argument("configuration", "Release");
SolutionFile = context.GetFiles($"{ProjectPath}/*.sln").First().GetFilename().ToString();
SolutionName = context.GetFiles($"{ProjectPath}/*.sln").First().GetFilenameWithoutExtension().ToString();
SonarKey = context.Argument("SonarKey", "MyProject");
SonarUrl = context.Argument("SonarUrl", "http://localhost:9000/");
Login = context.Argument("Login", "admin");
Password = context.Argument("Password", "admin");
}
}
[TaskName("Clean")]
[TaskDescription("Clean build solution folder. It depends of value of configuration argument: ./bin/Debug for 'Debug' value or .bin/Release for 'Release' value")]
public sealed class CleanTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var directory = context.Directory($"{context.ProjectPath}/{context.SolutionName}/bin/{context.MsBuildConfiguration}");
context.Log.Information($"Cleaning folder: {directory}");
context.CleanDirectory(directory);
}
}
[TaskName("Restore")]
[TaskDescription("Restore selected project")]
public sealed class RestoreTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var directory = context.Directory($"{context.ProjectPath}/{context.SolutionFile}");
context.Log.Information($"Restoring project: {directory}");
context.DotNetCoreRestore(directory);
}
}
[TaskName("Build")]
[TaskDescription("Build selected project. It depends of value of configuration argument (Debug or Release)")]
[IsDependentOn(typeof(CleanTask))]
[IsDependentOn(typeof(RestoreTask))]
public sealed class BuildTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var directory = context.Directory($"{context.ProjectPath}/{context.SolutionFile}");
context.Log.Information($"Building project: {directory}");
context.DotNetCoreBuild(directory, new DotNetCoreBuildSettings
{
Configuration = context.MsBuildConfiguration,
});
}
}
[TaskName("SonarBegin")]
[TaskDescription("Start test with SonarQube.")]
public sealed class SonarBeginTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var directory = context.Directory($"{context.ProjectPath}/{context.SolutionFile}");
var settings = new SonarBeginSettings
{
Key = context.SonarKey,
Url = context.SonarUrl,
Login = context.Login,
Password = context.Password,
WorkingDirectory = directory,
Verbose = true,
};
context.SonarBegin(settings);
}
}
[TaskName("SonarEnd")]
[TaskDescription("End test with SonarQube.")]
public sealed class SonarEndTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var settings = new SonarEndSettings
{
Login = context.Login,
Password = context.Password,
};
context.SonarEnd(settings);
}
}
[TaskName("Default")]
[IsDependentOn(typeof(SonarBeginTask))]
[IsDependentOn(typeof(BuildTask))]
[IsDependentOn(typeof(SonarEndTask))]
public class DefaultTask : FrostingTask
{
}
The commad for exect this is:
./build.ps1 --target Default --projectpath C:\route\of\project --sonarkey Prueba --sonarurl http://localhost:9000/ --login admin --password admin --verbosity Diagnostic
But I get this error:
An error occurred when executing task 'SonarBegin'.
Error: System.Exception: No CoreCLR executable found (SonarScanner.MSBuild.dll)
at Cake.Sonar.SonarRunner.Run(SonarSettings settings)
at Cake.Sonar.SonarCakeAliases.SonarBegin(ICakeContext context, SonarBeginSettings settings)
at SonarBeginTask.Run(BuildContext context) in C:\Users\jmedingr\Desktop\MonEES.CICD.Cake\cake\Program.cs:line 166
at Cake.Frosting.FrostingTask`1.Cake.Frosting.IFrostingTask.RunAsync(ICakeContext context)
at Cake.Core.CakeTask.Execute(ICakeContext context)
at Cake.Core.DefaultExecutionStrategy.ExecuteAsync(CakeTask task, ICakeContext context)
at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report)
at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report)
at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report)
at Cake.Core.CakeEngine.RunTask(ICakeContext context, IExecutionStrategy strategy, CakeTask task, String target, Stopwatch stopWatch, CakeReport report)
at Cake.Core.CakeEngine.RunTargetAsync(ICakeContext context, IExecutionStrategy strategy, ExecutionSettings settings)
at Cake.Cli.BuildScriptHost`1.RunTargetAsync(String target)
at Cake.Core.Scripting.ScriptHost.RunTarget(String target)
at Cake.Frosting.Internal.FrostingEngine`1.Run(String target)
at Cake.Frosting.Internal.DefaultCommand.Execute(CommandContext context, DefaultCommandSettings settings)
I cant see the way to fix it...
The error you are seeing (No CoreCLR executable found) is raised by Cake.Sonar, when the required tool is not found.
If you check the ReadMe for Cake.Sonar, it states #tool nuget:?package=MSBuild.SonarQube.Runner.Tool is needed to run Cake.Sonar.
So I'm guessing your Main method should look like:
public static int Main(string[] args)
{
return new CakeHost()
.UseContext<BuildContext>()
.InstallTool(new Uri("nuget:?package=MSBuild.SonarQube.Runner.Tool"))
.Run(args);
}
I did not test that, though.
Related
When I want to Insert A New Object into the db bellow Error Occured:
No database provider has been configured for this DbContext
Services:
private IConfiguration config;
public Startup(IConfiguration config) => this.config = config;
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlServer().AddDbContext<DataContext>(options => options.UseSqlServer(config["ConnectionStrings:MainConnection"]));
services.AddMvc();
}
DataContext:
public class DataContext:DbContext
{
public DataContext() { }
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public DbSet<Request> Request { get; set; }
public DbSet<AdminAccept> AdminAccept { get; set; }
public DbSet<Payment> Payment { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
base.OnConfiguring(builder);
}
}
Insert command :
public async Task <int> SaveToStorageAsync()
{
using (DataContext context=new DataContext())
{
context.Request.Add(this);
return await context.SaveChangesAsync();
}
}
however migrations and database created succefully
I solved it finally.
everything is okay but use of using expression cause an error.(I wonder why)
to solving it first of all I removed a using and declare a DataContext as parameter:
public async Task<int> SaveToStorageAsync(DataContext context)
{
context.Request.Add(this);
return await context.SaveChangesAsync();
}
after it initiate constructor in the main controller :
DataContext context;
public HomeController(DataContext context)
{
this.context = context;
}
and finally call function by sending context as a parameter.
hopped you used in your scenarios and good luck
Since you register the DataContext with the constructor receiving a DbContextOptions<DataContext> option.You also need to pass that when you create a DataContext
var optionsBuilder = new DbContextOptionsBuilder<DataContext >();
optionsBuilder.UseSqlServer("Your connection string");
using (DataContext context = new DataContext (optionsBuilder.Options))
{
context.Request.Add(this);
return await context.SaveChangesAsync();
}
I suggest that you could use dbContext by DI in controller which is a more recommended way in asp.net core:
public class StudentsController : Controller
{
private readonly DataContext _context;
public StudentsController(DataContext context)
{
_context = context;
}
public async Task <int> SaveToStorageAsync()
{
_context.Request.Add(this);
return await context.SaveChangesAsync();
}
}
The two ways are included in below link:
https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext#configuring-dbcontextoptions
After updating to ABP v3.0.0, I started getting DbContext disposed exception when running parallel requests to repository that create new UnitOfWork like this:
using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
I looked in the source code and found some code in AsyncLocalCurrentUnitOfWorkProvider that I don't understand. When setting current uow, it sets property in wrapper:
private static readonly AsyncLocal<LocalUowWrapper> AsyncLocalUow = new AsyncLocal<LocalUowWrapper>();
private static void SetCurrentUow(IUnitOfWork value)
{
lock (AsyncLocalUow)
{
if (value == null)
{
if (AsyncLocalUow.Value == null)
{
return;
}
if (AsyncLocalUow.Value.UnitOfWork?.Outer == null)
{
AsyncLocalUow.Value.UnitOfWork = null;
AsyncLocalUow.Value = null;
return;
}
AsyncLocalUow.Value.UnitOfWork = AsyncLocalUow.Value.UnitOfWork.Outer;
}
else
{
if (AsyncLocalUow.Value?.UnitOfWork == null)
{
if (AsyncLocalUow.Value != null)
{
AsyncLocalUow.Value.UnitOfWork = value;
}
AsyncLocalUow.Value = new LocalUowWrapper(value);
return;
}
value.Outer = AsyncLocalUow.Value.UnitOfWork;
AsyncLocalUow.Value.UnitOfWork = value;
}
}
}
private class LocalUowWrapper
{
public IUnitOfWork UnitOfWork { get; set; }
public LocalUowWrapper(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
}
That does not look thread-safe, as any thread can set new UnitOfWork and then dispose it.
Is it a bug? I can use my own implementation of ICurrentUnitOfWorkProvider, without wrapping, but I'm not sure if that is correct.
Update
I can't give an example with DbContext disposed exception, but here is one with null reference exception in repository.GetAll() method. I think it has the same reason.
namespace TestParallelEFRequest
{
public class Ent1 : Entity<int> { public string Name { get; set; } }
public class Ent2 : Entity<int> { public string Name { get; set; } }
public class Ent3 : Entity<int> { public string Name { get; set; } }
public class Ent4 : Entity<int> { public string Name { get; set; } }
public class DomainStartEvent : EventData {}
public class DBContext : AbpDbContext {
public DBContext(): base("Default") {}
public IDbSet<Ent1> Ent1 { get; set; }
public IDbSet<Ent2> Ent2 { get; set; }
public IDbSet<Ent3> Ent3 { get; set; }
public IDbSet<Ent4> Ent4 { get; set; }
}
public class TestService : DomainService, IEventHandler<DomainStartEvent>
{
private readonly IRepository<Ent1> _rep1;
private readonly IRepository<Ent2> _rep2;
private readonly IRepository<Ent3> _rep3;
private readonly IRepository<Ent4> _rep4;
public TestService(IRepository<Ent1> rep1, IRepository<Ent2> rep2, IRepository<Ent3> rep3, IRepository<Ent4> rep4) {
_rep1 = rep1;_rep2 = rep2;_rep3 = rep3;_rep4 = rep4;
}
Task HandleEntityes<T>(IRepository<T> rep, int i) where T : class, IEntity<int> {
return Task.Run(() => {
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)) {
Thread.Sleep(i); // Simulating work
rep.GetAll(); // <- Exception here
}
});
}
public void HandleEvent(DomainStartEvent eventData) {
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)) {
var t1 = HandleEntityes(_rep1, 10);
var t2 = HandleEntityes(_rep2, 10);
var t3 = HandleEntityes(_rep3, 10);
var t4 = HandleEntityes(_rep4, 1000);
Task.WaitAll(t1, t2, t3, t4);
}
}
}
[DependsOn(typeof(AbpEntityFrameworkModule))]
public class ProgrammModule : AbpModule
{
public override void PreInitialize() { Configuration.DefaultNameOrConnectionString = "Default"; }
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
Database.SetInitializer<DBContext>(null);
}
}
class Program {
static void Main(string[] args) {
using (var bootstrapper = AbpBootstrapper.Create<ProgrammModule>()) {
bootstrapper.Initialize();
bootstrapper.IocManager.Resolve<IEventBus>().Trigger(new DomainStartEvent());
Console.ReadKey();
}
}
}
}
I'd like to be able to simulate the compass sensor when running a Windows Phone 7.1 in the emulator.
At this stage I don't particularly care what data the compass returns. Just that I can run against something when using the emulator to test the code in question.
I'm aware that I could deploy to my dev unlocked phone to test compass functionality but I've found the connection via the Zune software to drop out frequently.
Update
I've looked into creating my own wrapper class that could simulate the compass when running a debug build and the compass isn't otherwise supported.
The Microsoft.Devices.Sensors.CompassReading struct has me a bit stumpted. Because it is a struct where the properties can only be set internally I can't inherit from it to provide my own values back. I looked at using reflection to brute force some values in but Silverlight doesn't appear to allow it.
as you already noticed I had a similar problem. when I mocked the compass sensor, I also had difficulties because you cannot inherite from the existing classes and write your own logic. Therefore I wrote my own compass interface which is the only compass functionality used by my application. Then there are two implementations, one wrapper to the WP7 compass functionalities and my mock compass.
I can show you some code, but not before weekend as I'm not at my delevopment machine atm.
Edit:
You already got it but for other people who have the same problem I'll add my code. As I already said, I wrote an interface and two implementations, one for the phone and a mock implementation.
Compass Interface
public interface ICompass
{
#region Methods
void Start();
void Stop();
#endregion
#region Properties
CompassData CurrentValue { get; }
bool IsDataValid { get; }
TimeSpan TimeBetweenUpdates { get; set; }
#endregion
#region Events
event EventHandler<CalibrationEventArgs> Calibrate;
event EventHandler<CompassDataChangedEventArgs> CurrentValueChanged;
#endregion
}
Used data classes and event args
public class CompassData
{
public CompassData(double headingAccurancy, double magneticHeading, Vector3 magnetometerReading, DateTimeOffset timestamp, double trueHeading)
{
HeadingAccuracy = headingAccurancy;
MagneticHeading = magneticHeading;
MagnetometerReading = magnetometerReading;
Timestamp = timestamp;
TrueHeading = trueHeading;
}
public CompassData(CompassReading compassReading)
{
HeadingAccuracy = compassReading.HeadingAccuracy;
MagneticHeading = compassReading.MagneticHeading;
MagnetometerReading = compassReading.MagnetometerReading;
Timestamp = compassReading.Timestamp;
TrueHeading = compassReading.TrueHeading;
}
#region Properties
public double HeadingAccuracy { get; private set; }
public double MagneticHeading { get; private set; }
public Vector3 MagnetometerReading { get; private set; }
public DateTimeOffset Timestamp { get; private set; }
public double TrueHeading { get; private set; }
#endregion
}
public class CompassDataChangedEventArgs : EventArgs
{
public CompassDataChangedEventArgs(CompassData compassData)
{
CompassData = compassData;
}
public CompassData CompassData { get; private set; }
}
WP7 implementation
public class DeviceCompass : ICompass
{
private Compass _compass;
#region Implementation of ICompass
public void Start()
{
if(_compass == null)
{
_compass = new Compass {TimeBetweenUpdates = TimeBetweenUpdates};
// get TimeBetweenUpdates because the device could have change it to another value
TimeBetweenUpdates = _compass.TimeBetweenUpdates;
// attach to events
_compass.CurrentValueChanged += CompassCurrentValueChanged;
_compass.Calibrate += CompassCalibrate;
}
_compass.Start();
}
public void Stop()
{
if(_compass != null)
{
_compass.Stop();
}
}
public CompassData CurrentValue
{
get { return _compass != null ? new CompassData(_compass.CurrentValue) : default(CompassData); }
}
public bool IsDataValid
{
get { return _compass != null ? _compass.IsDataValid : false; }
}
public TimeSpan TimeBetweenUpdates { get; set; }
public event EventHandler<CalibrationEventArgs> Calibrate;
public event EventHandler<CompassDataChangedEventArgs> CurrentValueChanged;
#endregion
#region Private methods
private void CompassCalibrate(object sender, CalibrationEventArgs e)
{
EventHandler<CalibrationEventArgs> calibrate = Calibrate;
if (calibrate != null)
{
calibrate(sender, e);
}
}
private void CompassCurrentValueChanged(object sender, SensorReadingEventArgs<CompassReading> e)
{
EventHandler<CompassDataChangedEventArgs> currentValueChanged = CurrentValueChanged;
if (currentValueChanged != null)
{
currentValueChanged(sender, new CompassDataChangedEventArgs(new CompassData(e.SensorReading)));
}
}
#endregion
}
Mock implementation
public class MockCompass : ICompass
{
private readonly Timer _timer;
private CompassData _currentValue;
private bool _isDataValid;
private TimeSpan _timeBetweenUpdates;
private bool _isStarted;
private readonly Random _random;
public MockCompass()
{
_random = new Random();
_timer = new Timer(TimerEllapsed, null, Timeout.Infinite, Timeout.Infinite);
_timeBetweenUpdates = new TimeSpan();
_currentValue = new CompassData(0, 0, new Vector3(), new DateTimeOffset(), 0);
}
#region Implementation of ICompass
public void Start()
{
_timer.Change(0, (int)TimeBetweenUpdates.TotalMilliseconds);
_isStarted = true;
}
public void Stop()
{
_isStarted = false;
_timer.Change(Timeout.Infinite, Timeout.Infinite);
_isDataValid = false;
}
public CompassData CurrentValue
{
get { return _currentValue; }
}
public bool IsDataValid
{
get { return _isDataValid; }
}
public TimeSpan TimeBetweenUpdates
{
get { return _timeBetweenUpdates; }
set
{
_timeBetweenUpdates = value;
if (_isStarted)
{
_timer.Change(0, (int) TimeBetweenUpdates.TotalMilliseconds);
}
}
}
public event EventHandler<CalibrationEventArgs> Calibrate;
public event EventHandler<CompassDataChangedEventArgs> CurrentValueChanged;
#endregion
#region Private methods
private void TimerEllapsed(object state)
{
_currentValue = new CompassData(_random.NextDouble()*5,
(_currentValue.MagneticHeading + 0.1)%360,
_currentValue.MagnetometerReading,
new DateTimeOffset(DateTime.UtcNow),
(_currentValue.TrueHeading + 0.1)%360);
_isDataValid = true;
EventHandler<CompassDataChangedEventArgs> currentValueChanged = CurrentValueChanged;
if(currentValueChanged != null)
{
currentValueChanged(this, new CompassDataChangedEventArgs(_currentValue));
}
}
#endregion
}
I need to create a scheduled task using Orchard CMS.
I have a service method (let's say it loads some data from external sources), and I need to execute it every day at 8:00 AM.
I figured out I have to use IScheduledTaskHandler and IScheduledTaskManager... Does anyone know how to solve this problem? Some sample code will be appreciated.
In your IScheduledTaskHandler, you have to implement Process to provide your task implementation (I Advise you to put your implementation in another service class), and you have to register your task in the task manager. Once in the Handler constructor to register the first task, and then in the process implementation, to ensure that once a task was executed, the next one is scheduled.
Here is a sample:
public class MyTaskHandler : IScheduledTaskHandler
{
private const string TaskType = "MyTaskUniqueID";
private readonly IScheduledTaskManager _taskManager;
public ILogger Logger { get; set; }
public MyTaskHandler(IScheduledTaskManager taskManager)
{
_taskManager = taskManager;
Logger = NullLogger.Instance;
try
{
DateTime firstDate = //Set your first task date (utc).
ScheduleNextTask(firstDate);
}
catch(Exception e)
{
this.Logger.Error(e,e.Message);
}
}
public void Process(ScheduledTaskContext context)
{
if (context.Task.TaskType == TaskType)
{
try
{
//Do work (calling an IService for instance)
}
catch (Exception e)
{
this.Logger.Error(e, e.Message);
}
finally
{
DateTime nextTaskDate = //Your next date (utc).
this.ScheduleNextTask(nextTaskDate);
}
}
}
private void ScheduleNextTask(DateTime date)
{
if (date > DateTime.UtcNow )
{
var tasks = this._taskManager.GetTasks(TaskType);
if (tasks == null || tasks.Count() == 0)
this._taskManager.CreateTask(TaskType, date, null);
}
}
}
You should do the first scheduling with an implementation of IOrchardShellEvents rather than in the task constructor to avoid adding multiple tasks.
Here is an abstract DailyTaskHandler class you can implement :
Usage
public class MyTaskHandler : DailyTaskHandler {
private readonly IMyService _myService;
public MyTaskHandler(IMyService myService,
IDailyTasksScheduler dailyTasksScheduler) : base(dailyTasksScheduler) {
_myService = myService;
}
public override int Hour => base.Hour; // you can override default hour
public override void Process() => _myService.DoStuff();
}
Abstract daily task handler and scheduler
public abstract class DailyTaskHandler : IDailyTaskHandler, IScheduledTaskHandler {
private readonly IDailyTasksScheduler _dailyTasksScheduler;
protected DailyTaskHandler(IDailyTasksScheduler dailyTasksScheduler) {
_dailyTasksScheduler = dailyTasksScheduler;
Logger = NullLogger.Instance;
TaskType = GetType().FullName;
}
public ILogger Logger { get; set; }
public virtual int Hour { get; } = 1; // default scheduled hour of the day
public string TaskType { get; }
public void Process(ScheduledTaskContext context) {
if (context.Task.TaskType == TaskType) {
Logger.Information($"Process task: {TaskType}");
try {
Process();
}
catch (Exception e) {
Logger.Error(e, e.Message);
}
finally {
_dailyTasksScheduler.Schedule(this);
}
}
}
public abstract void Process();
}
public class DailyTasksStarter : IOrchardShellEvents {
private readonly IEnumerable<IDailyTaskHandler> _dailyTaskHandlers;
private readonly IDailyTasksScheduler _dailyTasksScheduler;
public DailyTasksStarter(
IEnumerable<IDailyTaskHandler> dailyTaskHandlers,
IDailyTasksScheduler dailyTasksScheduler) {
_dailyTaskHandlers = dailyTaskHandlers;
_dailyTasksScheduler = dailyTasksScheduler;
}
public void Activated() => _dailyTasksScheduler.Schedule(_dailyTaskHandlers);
public void Terminating() { }
}
public class DailyTasksScheduler : IDailyTasksScheduler {
private readonly IScheduledTaskManager _scheduledTaskManager;
public DailyTasksScheduler(IScheduledTaskManager scheduledTaskManager) {
_scheduledTaskManager = scheduledTaskManager;
}
public void Schedule(IDailyTaskHandler dailyTaskHandler) => Schedule(new IDailyTaskHandler[] { dailyTaskHandler });
public void Schedule(IEnumerable<IDailyTaskHandler> dailyTaskHandlers) {
DateTime nextDay = DateTime.UtcNow.AddDays(1);
foreach (var dailyTaskHandler in dailyTaskHandlers) {
DateTime nextTaskDate = new DateTime(nextDay.Year, nextDay.Month, nextDay.Day, dailyTaskHandler.Hour, 0, 0, DateTimeKind.Utc);
if (nextTaskDate > DateTime.UtcNow && _scheduledTaskManager.GetTasks(dailyTaskHandler.TaskType)?.Any() != true) {
_scheduledTaskManager.CreateTask(dailyTaskHandler.TaskType, nextTaskDate, null);
}
}
}
}
public interface IDailyTaskHandler : IDependency {
int Hour { get; }
string TaskType { get; }
}
public interface IDailyTasksScheduler : IDependency {
void Schedule(IDailyTaskHandler dailyTaskHandler);
void Schedule(IEnumerable<IDailyTaskHandler> dailyTaskHandlers);
}
You can avoid using a task scheduler class by having the schedule method and the IOrchardShellEvents implementation on your task handler class.
I've recently started to learn Fluent NH, and I'm having some trouble with this test method. It takes forever to run (it's been running for over ten minutes now, and no sign of progress...).
[TestMethod]
public void Entry_IsCorrectlyMapped()
{
Action<PersistenceSpecification<Entry>> testAction = pspec => pspec
.CheckProperty(e => e.Id, "1")
.VerifyTheMappings();
TestMapping<Entry>(testAction);
}
with this helper method (slightly simplified - i have a couple of try/catch blocks too, to provide nicer error messages):
public void TestMapping<T>(Action<PersistenceSpecification<T>> testAction) where T : IEntity
{
using (var session = DependencyFactory.CreateSessionFactory(true).OpenSession())
{
testAction(new PersistenceSpecification<T>(session));
}
}
The DependencyFactory.CreateSessionFactory() method looks like this:
public static ISessionFactory CreateSessionFactory(bool buildSchema)
{
var cfg = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory())
.Mappings(m => m.FluentMappings.AddFromAssembly(typeof(Entry).Assembly));
if (buildSchema)
{
cfg = cfg.ExposeConfiguration(config => new SchemaExport(config).Create(false, true));
}
return cfg.BuildSessionFactory();
}
I've tried debugging, but I can't figure out where the bottleneck is. Why is this taking so long?
I would think it has to do with the way your trying to use the session together with the persistence spec. Make a base test class like the one below that provides you a session; if whole test takes longer than about 3 - 4 seconds max something is wrong.
Cheers,
Berryl
[TestFixture]
public class UserAutoMappingTests : InMemoryDbTestFixture
{
private const string _nickName = "berryl";
private readonly Name _name = new Name("Berryl", "Hesh");
private const string _email = "bhesh#cox.net";
protected override PersistenceModel _GetPersistenceModel() { return new UserDomainAutoMapModel().Generate(); }
[Test]
public void Persistence_CanSaveAndLoad_User()
{
new PersistenceSpecification<User>(_Session)
.CheckProperty(x => x.NickName, _nickName)
.CheckProperty(x => x.Email, _email)
.CheckProperty(x => x.Name, _name)
.VerifyTheMappings();
}
}
public abstract class InMemoryDbTestFixture
{
protected ISession _Session { get; set; }
protected SessionSource _SessionSource { get; set; }
protected Configuration _Cfg { get; set; }
protected abstract PersistenceModel _GetPersistenceModel();
protected PersistenceModel _persistenceModel;
[TestFixtureSetUp]
public void SetUpPersistenceModel()
{
_persistenceModel = _GetPersistenceModel();
}
[SetUp]
public void SetUpSession()
{
NHibInMemoryDbSession.Init(_persistenceModel); // your own session factory
_Session = NHibInMemoryDbSession.Session;
_SessionSource = NHibInMemoryDbSession.SessionSource;
_Cfg = NHibInMemoryDbSession.Cfg;
}
[TearDown]
public void TearDownSession()
{
NHibInMemoryDbSession.TerminateInMemoryDbSession();
_Session = null;
_SessionSource = null;
_Cfg = null;
}
}
public static class NHibInMemoryDbSession
{
public static ISession Session { get; private set; }
public static Configuration Cfg { get; private set; }
public static SessionSource SessionSource { get; set; }
public static void Init(PersistenceModel persistenceModel)
{
Check.RequireNotNull<PersistenceModel>(persistenceModel);
var SQLiteCfg = SQLiteConfiguration.Standard.InMemory().ShowSql();
SQLiteCfg.ProxyFactoryFactory(typeof(ProxyFactoryFactory).AssemblyQualifiedName);
var fluentCfg = Fluently.Configure().Database(SQLiteCfg).ExposeConfiguration(cfg => { Cfg = cfg; });
SessionSource = new SessionSource(fluentCfg.BuildConfiguration().Properties, persistenceModel);
Session = SessionSource.CreateSession();
SessionSource.BuildSchema(Session, true);
}
public static void TerminateInMemoryDbSession()
{
Session.Close();
Session.Dispose();
Session = null;
SessionSource = null;
Cfg = null;
Check.Ensure(Session == null);
Check.Ensure(SessionSource == null);
Check.Ensure(Cfg == null);
}
}