Skip to content

dalpert-korewireless/Topshelf.StructureMap

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 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.");
    }
}

About

StructureMap integration with Topshelf

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 95.3%
  • PowerShell 4.7%