software rant

Mark Jourdan helps you learn more about .NET and the tools used in development

  • Quartz Scheduling Much More Flexible

    Tags: Quartz, Autofac, Atlas, Entity Framework, Closed XML

    I’ve created a new extremely flexible service using Quartz, through a configuration file that I wanted to share with you. In my example I’ve followed the standard Domain Driven Design principals and used the repository pattern. This service is a good tool to see log4net, Atlas, Quartz, Autofac, ClosedXML and Entity Framework using code first all in action. I will explain a few key areas of my service, but you will need to download the application to really get a feel for its full power and see it in action.

    First I will point out the quartz configuration. I’ve wired up in my app.config to tell quartz to look at the quartzjobs.config file. In the quartz file I’ve setup a job to run every 30 seconds on the 30 second and 60 second tick. This differs from previous posts where I wired up the job and triggers inside of the code. Doing the quartz plumbing inside of a configuration file will allow me to change scheduling, without having to touch a line of code.

    <?xml version="1.0" encoding="UTF-8"?>
    <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            version="2.0" >
      <processing-directives>
        <overwrite-existing-data>true</overwrite-existing-data>
      </processing-directives>
      
      <schedule>
        <job>
          <name>CreateReportsJob</name>
          <group>MY_JOBS_GROUP</group>
          <description>creates the my service reports</description>
          <job-type>application.CreateReportsJob, application</job-type>
          <job-data-map>
            <entry>
              <key>MessageToLog</key>
              <value>Hello from MyJob</value>
            </entry>
          </job-data-map>
        </job>
        <trigger>
          <cron>
            <name>CreateReportsTrigger</name>
            <group>MY_TRIGGER_GROUP</group>
            <description>the trigger to create the my service reports</description>
            <job-name>CreateReportsJob</job-name>
            <job-group>MY_JOBS_GROUP</job-group>
    
            
            <!-- (1)Seconds  (2)Minutes  (3)Hours   (4)Day-of-Month   (5)Month   (6)Day-of-Week   (7)Year -->
            <!--<cron-expression>0 0 10 3 * ?</cron-expression>-->   <!-- Fires Every Month on the 3rd day at 10AM -->
            <cron-expression>0/30 * * * * ?</cron-expression>    <!-- Fires Every 30 seconds on the 30/60 second tick -->
          </cron>
        </trigger>
      </schedule>
    </job-scheduling-data>
    

    Within the app.config there is a Quartz section where I tell quartz to check the configration file every 10 seconds to see if there was an update to the quartz jobs and triggers. This will help to alleviate having to restart the service when you make a change to the quartz configuration.

    <add key="quartz.plugin.xml.scanInterval" value="10" />
    

    Now I will explain the job, CreateReportsJob.cs. This class implements the Quartz IJob which implements the execute method which does a couple things when it executes. First it will write a message out to the log telling you that its running, it pulls the message from the Quartz job-data-map in the configuration file. It will then write out to the log file and to the console all the brokers and clients. The clients differs in one key area from the broker, it has a foreign key relationship to the contact address table. Which I put in to display how a foreign key relationship can working using Entity Framework code first. Finally it uses ClosedXML to write out the brokers to a Excel file.

    [DisallowConcurrentExecution]
    public class CreateReportsJob : IJob
    {
        public IBrokerRepository BrokerRepository { get; set; }
        public IClientRepository ClientRepository { get; set; }
        public IExcelRepository ExcelRepository { get; set; }
        public ICreateFileFactory CreateFileFactory { get; set; }
        public ILog Log { get; set; }
    
        public void Execute(IJobExecutionContext context)
        {
            var data = context.MergedJobDataMap;
            var msg = data.GetString("MessageToLog") ?? string.Empty;
    
            Console.WriteLine(DateTime.Now + " " + msg);
            Log.InfoFormat(DateTime.Now + " " + msg);
    
    
            WriteOutCollectionOfBrokers();
            WriteOutCollectionOfClients();
    
            CreateExcelOfBrokers();
        }
    
        private void CreateExcelOfBrokers()
        {
            var fileName = string.Format("Brokers {0} {1}.xlsx", DateTime.Now.ToShortDateString().Replace("/", ""), DateTime.Now.ToShortTimeString().Replace(":", ""));
            Log.Info("FileName:" + fileName);
    
            var brokers = BrokerRepository.Get();
            ExcelRepository.CreateBrokerExcelDocument(brokers, fileName);
        }
    
        private void WriteOutCollectionOfBrokers()
        {
            var brokers = BrokerRepository.Get();
            foreach (var broker in brokers)
            {
                Log.InfoFormat(broker.NameOf);
                Console.WriteLine(broker.NameOf);
            }
        }
    
        private void WriteOutCollectionOfClients()
        {
            var clients = ClientRepository.Get().Take(5000);
    
            foreach (var client in clients)
            {
                Log.InfoFormat("Client | Name: {0} Address 1: {1}", client.NameOf, client.ContactAddressAccount != null ? client.ContactAddressAccount.Address1 : string.Empty);
                Console.WriteLine("Client | Name: {0} Address 1: {1}", client.NameOf, client.ContactAddressAccount != null ? client.ContactAddressAccount.Address1 : string.Empty);
            }
        }
    }
    

     

    Those really are the key differences I wanted to point out in this new service that differ from my previous post a quick service that sends emails. I originally got the idea from a reader who was looking to have a more configurable option with quartz, rather than using application settings. I decided to explore a bit and pieced this solution together which I’m really happy with and wanted to share.

    Sample Excel File Output

    image

    Sample Console Output

    Notice that this console shows that it was triggered twice, once at 12:00 and once at 12:30.

    image

    Give me your thoughts and feel free to download the solution here.

  • a quick service that sends emails

    Tags: EmailNotification, Atlas, Autofac, AutoMapper, Email, Quartz

    As promised in my previous post I will show you how to put together a service in a matter of a few minutes using what I showed you in sending emails made easy and a quick way to create a windows service using Autofac, Quartz and Atlas. In this example I am going to wire up a service that will be powered by atlas so that it makes the service easy to install/uninstall and debug. I then use autofac to manage my IoC container. I also will use quartz for managing the schedule of my service, instead of just a standard timer. I then will use auto mapper to handle the mapping between entities. Finally I will use EmailNotification to send my emails. The final product will query a table every minute and send out emails, it will then update the table with the sent date for history tracking.

    To get started you will just create a new console application. You can quickly reference all of the references to atlas, autofac, auto mapper, email notification, and quartz using nuget.

    Once things are referenced lets make sure our database table and the entity that is used is wired up using auto mapper. Auto mapper takes care of the translation/mapping between my entity and the email notification MessageQueueEntity. So I will wire that up painlessly.

    public class AutomapperRegistrations
    {
        public void RegisterMappings()
        {
            CreateEmailMessageToMessageQueueMapping();
            CreateMessageQueueToEmailMessageMapping();
        }
    
        private void CreateEmailMessageToMessageQueueMapping()
        {
            Mapper.CreateMap<MessageQueueEntity, EmailMessage>()
                  .ForMember(from => from.EmailMessageId, dest => dest.MapFrom(from => from.Identifier));
        }
    
        private void CreateMessageQueueToEmailMessageMapping()
        {
            Mapper.CreateMap<EmailMessage, MessageQueueEntity>()
                  .ForMember(from => from.Identifier, dest => dest.MapFrom(from => from.EmailMessageId));
    
        }
    }
    

    Now I will create my autofac module which handles all the registrations into my IoC container.

    public class ServiceModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            LoadQuartz(builder);
            LoadServices(builder);
    
            builder.Register(c => new EmailRepository()).As<IEmailRepository>();
        }
    
        private static void LoadQuartz(ContainerBuilder builder)
        {
            builder.Register(c => new StdSchedulerFactory().GetScheduler()).As<IScheduler>().InstancePerLifetimeScope();
            builder.Register(c => new AutofacJobListener(ContainerProvider.Instance)).As<IJobListener>();
        }
    
        private static void LoadServices(ContainerBuilder builder)
        {
            builder.RegisterType<EmailService>().As<IAmAHostedProcess>().PropertiesAutowired();
        }
    }
    
    

    Now I will setup my job which processes the email(s). Since I am using Quartz it will inherit an IJob and I will implement the execute method. When Execute is called it will retrieve the unsent message from the database and send them using the power of EmailNotification.

    public class SendEmailJob : IJob
    {
        public IEmailRepository EmailRepository { get; set; }
    
        public void Execute(IJobExecutionContext context)
        {
            SendEmails();
        }
        
        public void SendEmails()
        {
            //Get Emails from the repository and translate them to the messagequeueentity. MessageQueueEntity is the defined email object.
            var emails = EmailRepository.GetUnsentEmails().ToMessageQueueEntities();
    
            //Use the web config configuration.
            var configuration = Master.UseAppConfig().WithEmails(emails);
    
            //The service will now send out the emails and return the results.
            var result = Master.Execute(configuration);
    
            //Set the sent date for each email that has been sent.
            foreach (var email in result.Emails.Where(e => e.IsSent))
            {
                var e = EmailRepository.GetEmail((long)email.Identifier);
                e.IsSent = email.IsSent;
                e.Sent = email.Sent;
    
                e.Save();
            }
    
            //Save any errors that were caught.
            foreach (var email in result.Emails.Where(e => !e.IsSent && e.SentException != null))
            {
                var e = EmailRepository.GetEmail((long) email.Identifier);
                e.ErrorMessage = email.SentException.Message;
    
                e.Save();
            }
        }
    }
    

    Now that we have our job I will go ahead and create the service using Quartz. Quartz will need a job that will call our SendEmailJob and a trigger. I will set the trigger to fire on an interval, every minute. You can set the trigger up many different ways, take a look at Quartz trigger options in there documentation.

    public class EmailService : IAmAHostedProcess
    {
        public IScheduler Scheduler { get; set; }
        public IJobListener AutofacJobListener { get; set; }
    
        public void Start()
        {
            //create the job with quartz
            var job = JobBuilder.Create<SendEmailJob>()
            .WithIdentity("SendEmail")
            .Build();
    
            //define the trigger in quartz to fire every minute
            var trigger = TriggerBuilder.Create()
                .WithIdentity("SendEmailTrigger")
                .StartNow()
                .WithCalendarIntervalSchedule(x => x.WithIntervalInMinutes(1))  //Run's every minute
                .Build();
    
            //schedule the job within quartz
            Scheduler.ScheduleJob(job, trigger);
            Scheduler.ListenerManager.AddJobListener(AutofacJobListener);
            Scheduler.Start();  //kick off the quartz job
        }
    
        public void Stop()
        {
            //shut quartz down
            Scheduler.Shutdown();
        }
    
        public void Resume()
        {
            //resume the quartz jobs
            Scheduler.ResumeAll();
        }
    
        public void Pause()
        {
            //pause the quartz jobs
            Scheduler.PauseAll();
        }
    }
    

    Finally we wire up the final piece which is to wire up the entry point of the application. I chose to use atlas since it makes it real easy for me to install/uninstall/debug a windows service. I will use the application config, tell it to use my autofac module and run my automapper registrations before atlas begins.

    static class Program
    {
        //Standard entry point for an application.
        static void Main(string[] args)
        {
            //Register Atlas using app.config, register the autofac module and setup any automapper registrations.
            var configuration = Host.UseAppConfig<EmailService>()
                .WithRegistrations(b => b.RegisterModule(new ServiceModule()))
                .BeforeStart(() => new AutomapperRegistrations().RegisterMappings())
                .WithArguments(args);
    
            //Kick off atlas
            Host.Start(configuration);
        }
    }
    

    Last thing we need to do is setup the app.config for both EmailNotification and atlas.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="atlas" type="Atlas.Configuration.XmlConfiguration, Atlas" />
        <section name="EmailNotificationSettings" type="EmailNotification.Config.EmailNotificationConfigurationSectionHandler, EmailNotification" allowLocation="false" allowDefinition="Everywhere" />
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <atlas>
        <host name="Email Service" displayName="My Sample Email Service" description="This sample email service will send emails using the email notification service." allowMultipleInstances="true">
          <runtime accounttype="networkservice" startup="automatic" />
        </host>
      </atlas>
      <EmailNotificationSettings isEnabled="true">
        <ServerSettings smtpServer="10.252.3.157" smtpServerPort="25" isSSLEnabled="false" smtpServerConnectionLimit="-1" smtpServerPassword="" smtpServerUser="" smtpServerRequiredLogin="false" useDefaultCredentials="true" />
        <TestEmailAccounts isTestEmailAccountsBlocked="false">
          <add account="mailinator.com"></add>
        </TestEmailAccounts>
      </EmailNotificationSettings>
    </configuration>
    

    That’s it were complete! Lets fire the thing up and see it fly. Remember when you go to install all you have to do is run at the command prompt Service.exe –i it will install and can be found in your windows service list using the name you set in the atlas section of your app.config. I hope you found setting up the service to be painless and easy, I’ve included the code for you to download.

  • sending emails made easy

    Tags: Email, EmailNotification

    I released a library a while back that makes some email functionality easier with a wrapper around the built in .NET email functionality called Email Notification. I prefer to use a library like this to segregate the email functionality from the code base of my application. The wrapper gives you the ability to easily configure emails using either fluent syntax or use the configuration file of your website or application. It also handles some of the common exceptions thrown by email servers and handles them for you. The project has been open sourced and can be found on CodePlex at http://emailnotification.codeplex.com

    I’ll give you a quick an easy basic use case for the service in a web site instance. In a future post I’ll share with you an example of sending emails through a windows service quickly and painlessly.

    So for this example I started with a basic MVC 4 project (you could use MVC 3). Once the project is setup, start with the configuration file. I’ll drop the following couple lines into the web.config. I set my SMTP server and port, tell it not to use SSL and tell it the server doesn’t require a login. I also want to make sure I tell it about my test email accounts used by my UI tester, this way I prevent mail from being sent out to these accounts when my auto UI test is activated.

    <configSections>
      <section name="EmailNotificationSettings" type="EmailNotification.Config.EmailNotificationConfigurationSectionHandler, EmailNotification" allowLocation="false" allowDefinition="Everywhere"/>
    </configSections>
    <EmailNotificationSettings isEnabled="true">
      <ServerSettings smtpServer="smtp.myserver.com" smtpServerPort="25" isSSLEnabled="false" smtpServerConnectionLimit="-1" smtpServerPassword=""
                      smtpServerUser="" smtpServerRequiredLogin="false" useDefaultCredentials="true"/>
      <TestEmailAccounts isTestEmailAccountsBlocked="true">
        <add account="myserver.com"></add>
      </TestEmailAccounts>
    </EmailNotificationSettings>

    Let’s create a view “Index.cshtml” that will send out a single email, this view uses a basic Email { To, From, Subject Body} model.

    @{ Ajax.BeginForm("SendEmail", "Email", new AjaxOptions { HttpMethod = "POST", OnSuccess = "SendEmailSuccess"}); }
    
    <div style="width:500px; background-color: #EEEEEE; border:solid 1px black; padding:5px;">
        <div><span class="label">To:</span>@Html.TextBoxFor(t => t.To, new { style = "width:350px;" })</div>
        <div><span class="label">From:</span>@Html.TextBoxFor(t => t.From, new { style = "width:350px;" })</div>
        <div><span class="label">Subject:</span> @Html.TextBoxFor(t => t.Subject, new { style="width:350px;"})</div>
        <div><span class="label">Body:</span>@Html.TextAreaFor(t => t.Body, new { style = "width:350px;" })</div>
        <div style="text-align: center;">
            <input type="submit" value="send" />
        </div>
    </div>
    
    @{ Html.EndForm(); }
    

    Now here comes the easy part, but also the meat, wiring up the controller to send the email. I grab the email from the view, convert the model to an email the Email Notification wrapper is able to read and send the email out using the configuration from the web.config. That result is then sent back to the view where its handled there through javascript.

    [HttpPost]
    public JsonResult SendEmail()
    {
        var email = new EmailMessage();
    
        if (TryUpdateModel(email))
        {
            var emails = new List<MessageQueueEntity>
                             {
                                 new MessageQueueEntity
                                     {
                                         To = email.To,
                                         From = email.From,
                                         Body = email.Body,
                                         Subject = email.Subject,
                                         BodyFormat = BodyFormat.PlainText,
                                         Created = DateTime.Now
                                     }
                             };
    
            var configuration = Master.UseAppConfig().WithEmails(emails);
            var result = Master.Execute(configuration);
            return Json(result);
        }
    
        return new JsonResult { Data = new { IsSuccess = false} };
    }
    

    The javascript on the view is pretty simple, it will check to make sure that each of the emails sent to the wrapper were successful or not. If not successful I display the error message in an alert window.

    <script type="text/javascript">
        function SendEmailSuccess(data) {
            console.log(data);
    
            $.each(data.Emails, function (index, value) {
                console.log(value);
    
                if (value.SentException != null) {
                    alert("Email " + index + " couldn't be sent because: " + value.SentException.Message);
                } else {
                    alert("Email " + index + " was successfully sent to : " + value.To);
                }
            });
        }
    </script>
    

    Hopefully you find the Email Notification wrapper as useful as I do. I’ve included the example for you to download, the libraries source code can also be found on CodePlex. As a bonus I threw in a multi-email example for some fun. If you find any issues with the library or have suggestions feel free to start up a discussion or post an issue on the source at CodePlex

    image

  • sql search

    Tags: SQL, Development Tools

    Often times I find myself needing a way to find some text within stored procedures or looking at anything that references a object within SQL server. Like most developers/DBAs I instantly start writing something that goes after the system tables. This works well, but can be annoying to press the keys a bunch of times punching out the same select statement where having something that will just do it for you would be nice.

    redgate has put out a free tool called SQL Search which will find text within stored procedures, functions, views, indexes, etc. It will find any references to an object and help you to quickly navigate the objects. It will even return the full stored procedure text properly formatted in a preview window when selecting an object.

    If you find yourself doing these kinds of tasks in SQL, I’d recommend taking it for a spin. I promise you won’t be disappointed.

    image

  • a quick way to create a windows service using Autofac, Quartz and Atlas

    Tags: Autofac, Quartz, Atlas

    How often have you cringed when you thought about writing a windows service? I bet the first thoughts that came to you were:

    • I hate having to write an installer or bat file to install the service!
    • I hate having to write redundant code to manage the schedule!
    • I really don’t like debugging services!
    • I would like to write very little code and have the service re-use the code my application or site is using.

    Laying out the basics

    For simplicity in my example I am going to set these requirements for my service since they are a pretty common reason for needing a service. I will want a service that runs every minute (could be hourly, daily, monthly, etc.) and will run a specific job in my case write to a file.

    I’ve found three tools that I’ve found to be extremely useful and take the pain out of it. The first tool is Autofac an IoC (Inversion of Control) container which is the only IoC container supported by Atlas. I use a IoC container so that it can manage my registrations which I pass off to Atlas. Next I use Quartz a job scheduling system which allows us to not worry about setting up timers and triggers, we’ll let Quartz do this for us. Finally I chose Atlas a framework which allows us to write, run, debug and deploy a windows service painlessly.

    The Solution

    Each time the job is executed I want to create a new container for the job and inject any properties that the job might need. I also want to dispose of the container when the job is complete. So to do this I create a new job Quartz job listener.

    public class AutofacJobListener : IJobListener
    {
        private readonly IContainerProvider _containerProvider;
        private IUnitOfWorkContainer _container;
    
        public AutofacJobListener(IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
        }
    
        public void JobToBeExecuted(IJobExecutionContext context)
        {
            _container = _containerProvider.CreateUnitOfWork();
            _container.InjectUnsetProperties(context.JobInstance);
        }
    
        public void JobExecutionVetoed(IJobExecutionContext context)
        {
            /*noop*/
        }
    
        public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
        {
            _container.Dispose();
        }
    
        public string Name
        {
            get { return "AutofacInjectionJobListener"; }
        }
    }
    
    

    Now let’s create our job. The job will implement the Quartz.IJob. This job is really simple in our case, I will write “Job Executing “ and the date to a file in the directory where the application is running.

    public class MyJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            //Get the current directory to behave like a normal application, else the service will log to \Windows\System32 folder
            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
    
            var dateTime = DateTime.Now;
            var sw = new StreamWriter("MyFile.txt",true);
    
            sw.WriteLine("Job Executing " + dateTime);
            Console.WriteLine("Job Executing " + dateTime);
    
            sw.Close();
            sw.Dispose();
        }
    }
    
    

    Now its time to setup the actual service and use Atlas. I will inherit off of the Atlas.IAmAHostedProcess and implement the methods. You will notice in the Start method I will use Quartz to create my job, my trigger and schedule it to run.

    public class MyService : IAmAHostedProcess
    {
        const int IntervalInMinutes = 1;
        
        public IScheduler Scheduler { get; set; }
        public IJobListener AutofacJobListener { get; set; }
    
        #region Implementation of IAmAHostedProcess
    
        public void Start()
        {
            var job = JobBuilder.Create<MyJob>()
                .WithIdentity("Job1")
                .Build();
    
            var trigger = TriggerBuilder.Create()
                .WithIdentity("Trigger1")
                .StartNow()
                .WithCalendarIntervalSchedule(x => x.WithIntervalInMinutes(IntervalInMinutes))
                .Build();
    
            Scheduler.ScheduleJob(job, trigger);
            Scheduler.ListenerManager.AddJobListener(AutofacJobListener);
            Scheduler.Start();
        }
    
        public void Stop()
        {
            Scheduler.Shutdown();
        }
    
        public void Resume()
        {
            Scheduler.ResumeAll();
        }
    
        public void Pause()
        {
            Scheduler.PauseAll();
        }
    
        #endregion
    }
    
    

    Now lets register our Quartz scheduler and the service with autofac.

    public class MyAutofacModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            LoadQuartz(builder);
            LoadServices(builder);
        }
    
        private static void LoadQuartz(ContainerBuilder builder)
        {
            builder.Register(c => new StdSchedulerFactory().GetScheduler()).As<IScheduler>().InstancePerLifetimeScope();
            builder.Register(c => new AutofacJobListener(ContainerProvider.Instance)).As<IJobListener>();
        }
    
        private static void LoadServices(ContainerBuilder builder)
        {
            builder.RegisterType<MyService>().As<IAmAHostedProcess>().PropertiesAutowired();
        }
    }
    
    

    Finally, we will wire up our Main method, the entry point when the console/service runs. We just configure Atlas and tell it to start.

    public class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            var configuration =
                Host.UseAppConfig<MyService>()
                    .AllowMultipleInstances()
                    .WithRegistrations(b => b.RegisterModule(new MyAutofacModule()))
                    .WithArguments(args);
    
            Host.Start(configuration);
        }
    }
    
    

    So now lets go ahead an lets get ready to run the application. Since Atlas can be run as a console application easily by executing it with the following argument –c. Let’s go into the Properties of the project and on the Debug tab set the Command line arguments to be –c

    image

    Ok go ahead and run the application, and notice that you can press P to pause the application or click ESC key to exit the application. It will fire every minute both writing to the console and a text file called MyFile.txt.

    So now that everything is running correctly and we’re ready to deploy the application, lets do one more thing to make installation a breeze. Lets add an Atlas section to the configuration file with the name, description and user to run as for the service to be installed.

    <?xml version="1.0"?>
    <configuration>
      <configSections>
        <section name="atlas" type="Atlas.Configuration.XmlConfiguration, Atlas" />
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
      </startup>
      <atlas>
        <host name="AtlasExample" displayName="Atlas Sample" description="a sample project for atlas" allowMultipleInstances="true">
          <runtime accounttype="networkservice" startup="automatic" />
        </host>
      </atlas>
    </configuration>
    

    Service Installation with Atlas

    Alright let’s install the service, watch how easy this is.

    1. Copy the installation files for the service to a folder where you want the service to run (copy the Bin folder contents to a folder)
    2. Open a command prompt window to the directory where the service exists.
    3. Run the following command line AtlasExample.exe –i (to uninstall all you have to do is run these same steps but instead of -i use –u)
    4. That’s it! you can now go to the Services manager in windows and find the service is ready to be turned on. Notice the name and description are what we setup inside of the configuration file.
      image

    Conclusion

    Look at how easy that was! In a matter of 15 minutes I was able to write a service, have it manage scheduling itself, manage memory and registrations efficiently, run as both a console and service and have it self install through a baked in installer. Quartz handles the scheduling and the job, Atlas handles the painless debug and install of the service and autofac manages our containers.

    Writing your services like this will allow you to write a single windows service that can be executing a lot of different jobs all at different times or at the same time. It lets you abstract any non-windows service code to be injected into a job using autofac from another class so that code can be shared with other parts of a solution and tested.

    I hope my way of writing services is a help to you and that the next time you are asked to write a service you don’t feel like its such a pain to write. I’ve included a copy of the source code for you to download and play with.
    download source code

  • Entity Framework 4.3 with Dino in a domain-driven design environment

    Tags: Entity Framework, Dino, Domain-Driven Design

    Recently a friend of mine was looking for me to throw together a quick sample of Entity Framework. I thought it would be a good time to introduce him into domain-driven design (DDD) as well as using Dino a great ORM wrapper framework a friend of mine recently built. I will walk you through my simple solution I built him.

    I decided to build a simple console application since it removes the dependency on focusing on the visual design. This application will simply allow you to add employees to a database using the –add command and display the employees to the screen using the –p command. The application will continue to run until the user wants to exit using the –q command.

    Solution Code

    Entity Framework 4.3 with Dino

    Core Technology:

    I start out by creating a new solution and within the solution I have 3 projects: application layer, domain layer and infrastructure layer.

    image

    Once I have my solution setup I begin with the Domain. The domain will contain any knowledge, influence or activity that relates to the application. For this example our domain will host 2 pieces of knowledge, it will contain our entity object and an interface with the knowledge of what our repository does.

    I will start by adding an Employee entity. I’m going to keep it simple, it will have an ID, Name, Department and Phone property because I’m only going to want to store those fields in the database.

    namespace Domain.Entities
    {
        public class Employee
        {
            public long EmployeeId { get; set; }
            public string Name { get; set; }
            public string Department { get; set; }
            public string Phone { get; set; }
        }
    }
    

    I'm going to take the time and also setup my repository interface. I will need my repository to have some basic functionality add an employee, add multiple employee's, get a list of all employees and return a single employee. Here is what my interface will look like.

    using System.Linq;
    using Domain.Entities;
    
    namespace Domain.Repositories
    {
        public interface IExampleRepository
        {
            void AddEmployee(Employee employee);
            void AddEmployees(IEnumerable employees); IQueryable<Employee> GetEmployees(); Employee GetEmployee(long employeeId); } } 

    Now that our domain is complete I will build out our infrastructure. In DDD the infrastructure layer is responsible for managing all infrastructure resources such as databases, communicating with other frameworks and other applications.

    The the primary responsibility for our infrastructure will be to talk to our database. For this application I will need to wire up Entity Framework code first with SQL and create the repository that will retrieve/store (repositories don’t create entities/data, leave that to factories)/delete data from the database. Let’s start by wiring up Entity Framework.

    I begin by creating the employee configuration using Entity Frameworks code first’s entity type configuration. I’ll make the EmployeeId filed the key for the table and call the table Employee

    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity.ModelConfiguration;
    using Domain.Entities;
    
    namespace Infrastructure.Data.Configurations
    {
        public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
        {
            public EmployeeConfiguration()
            {
                HasKey(p => p.EmployeeId);
                Property(p => p.EmployeeId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
                Property(p => p.Name);
                Property(p => p.Phone);
                Property(p => p.Department).IsRequired();
                ToTable("Employee");
            }
        }
    }
    
    

    Next we will create the ObjectContext, Dino makes this easy for us, the wrapper already has an Entity Framework implementation setup for us and we just need to inherit off of the IObjectContext. We now just have to implement the add, delete, query and commit methods. We will create a connection string constant which will be used later in our configuration file. We will also want to wire up the entity type configuration when the model is created, this is for allowing Entity Framework code first to create the database for us if it doesn’t exist.

    using System.Data.Entity;
    using System.Linq;
    using Dino;
    using Infrastructure.Data.Configurations;
    
    namespace Infrastructure.Data
    {
        public class EfExampleReviewContext : DbContext, IObjectContext
        {
            public const string ConnectionString = "name=EfExampleReviewContext";
    
            public EfExampleReviewContext() : base(ConnectionString)
            {
                Configuration.LazyLoadingEnabled = true;
            }
    
            public EfExampleReviewContext(string connectionString): base(connectionString)
            {
                Configuration.LazyLoadingEnabled = true;
            }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new EmployeeConfiguration());
                base.OnModelCreating(modelBuilder);
            }
            
            #region Implementation of IObjectContext
    
            public void Commit()
            {
                SaveChanges();
            }
    
            public IQueryable<T> Query<T>() where T : class
            {
                return Set<T>().AsQueryable();
            }
    
            public void Add<T>(T entity) where T : class
            {
                Set<T>().Add(entity);
            }
    
            public void Delete<T>(T entity) where T : class
            {
                Set<T>().Remove(entity);
            }
    
            #endregion
        }
    }
    
    

    Now its time to create our repository. In this example we make the repository really simple. Were just going to need to inherit off of our repository interface we created in our Domain and implement each of the methods.

    Since we are using Dino we just need to pass into the constructor our ObjectContext and let Dino manage our unit of work.

    using System.Collections.Generic;
    using System.Linq;
    using Dino;
    using Domain.Entities;
    using Domain.Repositories;
    
    namespace Infrastructure.Repositories
    {
        public class ExampleRepository : IExampleRepository
        {
            private readonly IObjectContext _contextProvider;
    
            public ExampleRepository(IObjectContext contextProvider)
            {
                _contextProvider = contextProvider;
            }
    
            public void AddEmployee(Employee employee)
            {
                _contextProvider.Add(employee);
            }
    
            public void AddEmployees(IEnumerable<Employee> employees)
            {
                foreach (var employee in employees)
                {
                    _contextProvider.Add(employee);
                }
            }
    
            public IQueryable<Employee> GetEmployees()
            {
                return _contextProvider.Query<Employee>();
            }
    
            public Employee GetEmployee(long employeeId)
            {
                return _contextProvider.Query<Employee>().SingleOrDefault(e => e.EmployeeId == employeeId);
            }
        }
    }
    
    

    Our Infrastructure is complete so now we can move onto our final layer, the console application. This layer will only contain logic that is specific to its technology (WPF/Console/Web). In my example I chose to just implement a console application.

    We will first implement our applications Main() method. First we will do the basic wire up, which is really wiring up Dino and setting up the repository and unit of work we will need later. We will create a new unit of work inside of Dino, add our ObjectContext to the unit of work and finally set the repository and session properties on the page.

            public static IExampleRepository ExampleRepository { get; set; }
            public static ISession Session { get; set; }
    
            static void Main()
            {
                Setup();
            }
    
            private static void Setup()
            {
                var uow = new UnitOfWork();
                uow.Add<EfExampleReviewContext>(() => new EfExampleReviewContext());
                ExampleRepository = new ExampleRepository(uow.GetContext<EfExampleReviewContext>());
                Session = uow;
            }
    

    We will display to the user some simple instructions on what we allow the user the ability to do. Next we will wait for the users input. Notice we do most of our communication with our repository.

    using System;
    using System.Linq;
    using Dino;
    using Domain.Entities;
    using Domain.Repositories;
    using Infrastructure.Data;
    using Infrastructure.Repositories;
    
    namespace EntityFrameworkExample
    {
        public class ExampleApplication
        {
            public static IExampleRepository ExampleRepository { get; set; }
            public static ISession Session { get; set; }
    
            static void Main()
            {
                Setup();
    
                Console.WriteLine("*****************");
                Console.WriteLine("Welcome to the Employee Database");
                Console.WriteLine("Add Employee: -add {employee first name} {employee last name} {department} {phone}");
                Console.WriteLine("Add Employees: -addmulti {employee first name} {employee last name} {department} {phone} | {employee first name} {employee last name} {department} {phone}");
                Console.WriteLine("Print list of employees: -p");
                Console.WriteLine("Quit: -q");
                Console.WriteLine("*****************");
    
                ReadLine();
            }
    
            ...
    
            private static void ReadLine()
            {
                var continueRunning = true;
    
                Console.WriteLine("waiting:");
                Console.Out.Flush();
                var inputStr = Console.ReadLine();
    
                if (inputStr != null)
                {
                    var inputStrArray = inputStr.Split(new[] { ' ' });
                    switch (inputStrArray.First())
                    {
                        case "-add":
                            if (inputStrArray.Count() != 5)
                            {
                                Console.WriteLine(
                                    "Not in the proper format, I expect '{{employee first name}} {{employee last name}} {{department}} {{phone}}' you provided '{0}'",
                                    inputStr.Replace(inputStrArray.First(), ""));
                                break;
                            }
                            var employeeName = inputStrArray.Skip(1).First() + " " + inputStrArray.Skip(2).First();
                            var department = inputStrArray.Skip(3).First();
                            var phone = inputStrArray.Skip(4).First();
    
                            ExampleRepository.AddEmployee(new Employee
                                                              {
                                                                  Department = department,
                                                                  Name = employeeName,
                                                                  Phone = phone
                                                              });
                            Session.Commit();
                            break;
                        case "-addmulti":
                            if (inputStrArray.Count() != 10)
                            {
                                Console.WriteLine(
                                    "Not in the proper format, I expect '{{employee first name}} {{employee last name}} {{department}} {{phone}} | {{employee first name}} {{employee last name}} {{department}} {{phone}}' you provided '{0}'",
                                    inputStr.Replace(inputStrArray.First(), ""));
                                break;
                            }
                            var employeeName1 = inputStrArray.Skip(1).First() + " " + inputStrArray.Skip(2).First();
                            var department1 = inputStrArray.Skip(3).First();
                            var phone1 = inputStrArray.Skip(4).First();
    
                            var employeeName2 = inputStrArray.Skip(6).First() + " " + inputStrArray.Skip(7).First();
                            var department2 = inputStrArray.Skip(8).First();
                            var phone2 = inputStrArray.Skip(9).First();
    
                            ExampleRepository.AddEmployee(new Employee
                            {
                                Department = department1,
                                Name = employeeName1,
                                Phone = phone1
                            });
    
                            ExampleRepository.AddEmployee(new Employee
                            {
                                Department = department2,
                                Name = employeeName2,
                                Phone = phone2
                            });
    
                            Session.Commit();
                            break;
                        case "-p":
                            var employees = ExampleRepository.GetEmployees();
    
                            Console.WriteLine("Name---------------- Department---------- Phone-----");
                            foreach (var employee in employees)
                            {
                                Console.WriteLine("{0} {1} {2}", employee.Name.PadLeft(20, ' ').Substring(0, 20), employee.Department.PadLeft(20, ' ').Substring(0, 20), employee.Phone.PadLeft(10, ' ').Substring(0, 10));
                            }
                            break;
                        case "-q":
                            Console.WriteLine("Now Exiting");
                            continueRunning = false;
                            break;
                        default:
                            Console.WriteLine("Unrecognized command '{0}'", inputStr);
                            break;
                    }
                }
    
                Console.WriteLine("");
                
                if(continueRunning) ReadLine();
            }
        }
    }
    
    

    Now you may be asking what does the Unit of Work do for you. The unity of work will maintain a list of objects that need to be committed through a single transaction. In this example the only benefit we get is during the “add multi” command. We are adding 2 users through 1 transactional commit to the database.

    ExampleRepository.AddEmployee(new Employee
    {
        Department = department1,
        Name = employeeName1,
        Phone = phone1
    });
    
    ExampleRepository.AddEmployee(new Employee
    {
        Department = department2,
        Name = employeeName2,
        Phone = phone2
    });
    
    Session.Commit();
    

    The final piece before running the application is to wire up the database in the configuration file. I chose to use SQL Server Compact 4.0 in this example since it is easy to use and I don’t need to run this on a full instance of SQL Server and just need to install a small footprint installer on the computer. I will just need to add the connection string using the name of the context

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="EfExampleReviewContext" connectionString="Data Source=|DataDirectory|\EntityFrameworkExample.sdf;" providerName="System.Data.SqlServerCe.4.0" />
      </connectionStrings>
    </configuration>
    

    Screenshot

    image

    Conclusion

    Now that I have walked you through the application I hope you get a better understanding of a simple application using Entity Framework 4.3 with Dino in a Domain-Driven application. Feel free to download and play with the solution.

  • Autofac with method interception for logging

    Tags: Autofac, Logging

    Are you currently using autofac and looking for a way to make logging easier?  Nicholas Blumhardt did a really great job documenting this process on the autofac site. However, I thought I would put together a functional example to download with a way of easily executing interception for you to download since a few colleagues of mine struggled when first learning autofac on how to accomplish interception fast and easy.

    Download the Example

    My example is a console application for simplicity but can easily be tweaked to the format of any web or desktop application solution. I used Nicholas’s intercept method example which will write out the method name being called and the methods parameters before the execution of the method. Then writes out the result when the method is done being executed.

    //The interceptor will log the beginning and end of the method call.
    public void Intercept(IInvocation invocation)
    {
        //This happens before the method call
        _output.Write("Calling method {0} with parameters {1}... ",
        invocation.Method.Name,
        string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));
    
        //Actual method call begins
        invocation.Proceed();
    
        //This happens after a method call
        _output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
    }
    

    My repository is simple, it will return a simple list of state names in string type format. On the interface for the repository I add the intercept attribute necessary to do the interception on all methods within the repository.

    public class ConsoleRepository : IConsoleRepository
    {
        /// <summary>
        /// Just a generic collection of states
        /// </summary>
        /// <returns></returns>
        public IEnumerable<string> GetListOfStates()
        {
            return new List<string> {"State 1", "State 2", "State 3", "State 4"};
        }
    }
    
    //the intercept can be on the interface of the class
    [Intercept("log-calls")]
    public interface IConsoleRepository
    {
        IEnumerable<string> GetListOfStates();
    }
    
    

    My console app is really simple, I create an autofac container, register my interceptor (tell it to publish the output to the console), register my repository and finally create the container for my application. My application will call the WriteOutStates method call which will write out each state name to the console.

    static void Main()
    {
        //Create the Autofac Container
        var builder = new ContainerBuilder();
    
        //Register the Log Interceptor, which will log out to the console of the application. Anything registered with 'log-calls' will be intercepted
        builder.Register(c => new LogInterceptor(Console.Out))
            .Named<IInterceptor>("log-calls");
    
        //Register the repository, make sure 'EnableInterfaceInterceptors' is called, this is important in letting Castle know it has Interceptors
        builder.Register(c => new ConsoleRepository())
            .As<IConsoleRepository>()
            .EnableInterfaceInterceptors();
    
        //Build the container
        using (var container = builder.Build())
        {
            WriteOutStates(container);
        }
        
    }
    
    private static void WriteOutStates(IComponentContext container)
    {
        //pull the registered repository from the Autofac container
        var consoleRepo = container.Resolve<IConsoleRepository>();
    
        //This method call to get the list of states will be intercepted.
        foreach (var state in consoleRepo.GetListOfStates())
        {
            Console.WriteLine(state);
        }
    }
    
    

    The WriteOutStates method will make the call to the repository where we are expecting the interception to take place. When it makes the call to get the list of states from the repository we are expecting to see the name of the method being called and the result when its finished. Then we it will write each state out to the console. Here is what it will look like after executing the application.

    image

    Download the application and play around with it. I hope it helps you out on when getting started with interception using autofac!

  • What Parallels can do for you…

    Tags: Parallels

    I thought I would share a quick sample of what .NET Parallels can do for you. In this case I wanted to be able to go out and pull back entire tables and save a copy of the tables locally. In this example I put together the timings of how long it takes to pull back a collection of the data from the table, and with using Parallels in .NET the amount of time to accomplish the same task was cut in over half the time.

    The Base Logic

    I first create a Task that can be used by both the Synchronous and Asynchronous call. This method will use LinqToSql to get a full list of everything in the table.

    public static void DoTask(IGetData getData)
    {    
        var startTime = DateTime.Now;
        getData.GetList();
        var endTime = DateTime.Now;
    
        var timeSpan = endTime - startTime;
        Console.WriteLine(getData.Name + " took " + timeSpan.TotalSeconds + " seconds long");
    }
    


    To get the data I created an interface and implemented GetList generically

    public abstract class GetData<TEntity> where TEntity : class
    {    
        public IList GetList()
        {
            var db = new MyDataContext();
            return db.GetTable<TEntity>().Select(ca => ca).ToList();
        }
    }
    
    public interface IGetData
    {
        string Name { get; }
        IList GetList();
    }
    

    Sample Implementation of the IGetData interface

    public class GetContactNumber : GetData<ContactNumber> IGetData
    {
        public string Name
        {
            get { return "ContactNumber"; }
        }
    }
    

    Parallel Test

    In this test I have it call the DoTask for each table, I track when I started and when I finish to get a pretty good idea of how long it took. It will call the Parallel.ForEach method on the generic list of IGetData implementations.

    private static void ParallelTest()
    {
        Console.WriteLine("Parallels ---------------");
        var getDataList = new List<IGetData>
                            {
                                  new GetContactAddresses(),
                                  new GetContactName(),
                                  new GetContactNumber(),
                                  new GetTransactions(),
                                  new GetDetail()
                            };
    
        var startTime = DateTime.Now;
        Parallel.ForEach(getDataList, DoTask);
        var endTime = DateTime.Now;
        var timeSpan = endTime - startTime;
    
        Console.WriteLine("Parallels took " + timeSpan.TotalSeconds + " seconds long");
    }
    

    Synchronous Test

    In this test I will call the DoTask for each table in synchronous order, I track when the tasks begin and when I finish to get a good idea of how long it took.

    private static void SynchronousTest()
    {
        Console.WriteLine("Synchronous");
        var startTime = DateTime.Now;
    
        DoTask(new GetContactAddresses());
        DoTask(new GetContactName());
        DoTask(new GetContactNumber());
        DoTask(new GetTransactions());
        DoTask(new GetDetail());
    
        var endTime = DateTime.Now;
        var timeSpan = endTime - startTime;
    
        Console.WriteLine("Synchronous took " + timeSpan.TotalSeconds + " seconds long");
    
        Console.WriteLine();
    }
    

    Final Results

    Finally I executed the program to see my results, and to my happiness I saved a huge amount of time by letting Parallel run each of my select statements at the same time across multiple threads.

    Synchronous took 70 seconds
    Asynchronous (Parallels) took 26 seconds

    image

  • Read Excel Spreadsheets Easily – Excel Data Reader

    Tags: Excel

    So often I am asked to move data from Excel into an application I am building. I wanted a Lightweight and fast solution. I stumbled across this library Excel Data Reader on CodePlex and thought I would share a simple example of how its used.

    In this example I needed to take a simple excel spreadsheet with a few thousand rows of employee names and addresses. The first row of the spreadsheet has a header so I want to skip it. I will return an enumerated collection of employees back to the calling method.

    public IEnumerable<Employee> ReadFile(string filePath)
    {
        var employees = new List<Employee>();
        var stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
        // Want to be able to handle .xls or .xlsx file formats
        var excelReader = filePath.Contains(".xlsx")
                              ? ExcelReaderFactory.CreateOpenXmlReader(stream)
                              : ExcelReaderFactory.CreateBinaryReader(stream);
    
        excelReader.IsFirstRowAsColumnNames = true;
    
        excelReader.Read(); //skip first row
        while (excelReader.Read())
        {
            employees .Add(new Employee
                              {
                                  Name = excelReader.GetString(0).CleanString(),
                                  Address = excelReader.GetString(1).CleanString(),
                                  City = excelReader.GetString(2).CleanString(),
                                  State = excelReader.GetString(3).CleanString(),
                                  PostalCode = excelReader.GetString(4).CleanString()
                              });
        }
    
        excelReader.Close();
    
        return employees;
    }
    

    The CleanString() method call is an extension to the string property. I do some simple cleanup on the string to remove extra spaces and any other cleanup I might need to do do the data coming in.