Skip to content

jakobt/Topshelf.StructureMap

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Getting Started You can download packages on NuGet Gallery:

#Example

class Program
{
    static void Main()
    {
        HostFactory.Run(c =>
        {
            var container = new Container(cfg =>
                                            {
                                                cfg.For<IDependency>().Use<Dependency>();
                                            });
            // Init StructureMap container 
            c.UseStructureMap(container);

            c.Service<SampleService>(s =>
            {
                //Construct topshelf service instance with StructureMap
                s.ConstructUsingStructureMap();

                s.WhenStarted((service, control) => service.Start());
                s.WhenStopped((service, control) => service.Stop());
                
                //Construct IJob instance with StructureMap
                s.UseQuartzStructureMap();

                s.ScheduleQuartzJob(q =>
                    q.WithJob(() =>
                        JobBuilder.Create<SampleJob>().Build())
                        .AddTrigger(() =>
                            TriggerBuilder.Create()
                                .WithSimpleSchedule(builder => builder
                                                                .WithIntervalInSeconds(5)
                                                                .RepeatForever())
                                                                .Build())
                    );
            });
        });
    }
}
public interface IDependency
{
    void Write();
}

public class Dependency : IDependency
{
    public void Write()
    {
        Console.WriteLine("Line from Service dependency");
    }
}
internal class SampleService
{
    private readonly IDependency _dependency;
    public SampleService(IDependency dependency)
    {
        _dependency = dependency;
    }

    public bool Start()
    {
        Console.WriteLine("--------------------------------");
        Console.WriteLine("Sample Service Started...");
        _dependency.Write();
        Console.WriteLine("--------------------------------");
        return true;
    }

    public bool Stop()
    {
        return true;
    }
}
internal class SampleJob:IJob
{
    private readonly IDependency _dependency;

    public SampleJob(IDependency dependency)
    {
        _dependency = dependency;
    }

    public void Execute(IJobExecutionContext context)
    {
        Console.WriteLine("{0} - Sample job executing...", DateTime.Now);
        _dependency.Write();
        Console.WriteLine("Sample job executed.");
    }
}

If for some reason initialization of your job from the container fails it will be logged using System.Diagnostics.Debug.WriteLine per default, if you want to override this behaviour to use a custom logging framework you can register your own implementation of IOnJobInitializationFailedHandler

About

StructureMap integration with Topshelf

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PowerShell 77.3%
  • C# 22.1%
  • Batchfile 0.6%