# Hangfire with parameterized constructor

**URL:** <https://discuss.hangfire.io/t/hangfire-with-parameterized-constructor/2643>\
**Category:** question\
**Created:** [November 10, 2016, 6:16am UTC](https://discuss.hangfire.io/t/hangfire-with-parameterized-constructor/2643 "2016-11-10T06:16:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tatyana](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@Tatyana](https://discuss.hangfire.io/u/Tatyana)\
**Post date:** [November 10, 2016, 6:16am UTC](https://discuss.hangfire.io/t/hangfire-with-parameterized-constructor/2643/1 "2016-11-10T06:16:09Z")

</div>

Hello!  
I need to start job, which will start some processing.  
I have class TaskSSN and the method Process in it. TaskSSN class have constructor with two parameters.

```
public class TaskSSN
{
    private string fileName;
    private string fileContent;
        
    public TaskSSN(string fileName, string fileContent)
    {
        this.fileName = fileName;
        this.fileContent = fileContent;
    }

    public void Process()
    {
        //some actions...
    }
}

```

Then I create object instanse and call Process method:

```
public void SomeMethod (LoadArg arg)
{
    TaskSSN task = new TaskSSN(arg.FileName, arg.FileContent);
    BackgroundJob.Enqueue(() => task.Process());
}

```

I understand that Hangfire uses default constructor and doesn’t have access to the object instance. In this issue [Hangfire needs parameterless constructor](http://hangfire.discourse.group/t/hangfire-needs-parameterless-constructor/645) it is said about dependency injection container and JobActivator. Sorry, but I’m new in dependency injection and don’t understand how to use DI and JobActivator in this case .  
Can you tell me please (using my code, if it possible), how can I fix problem with parameterized constructor?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Jonah\_Simpson](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/jonah_simpson/32/504_2.png) [@Jonah\_Simpson](https://discuss.hangfire.io/u/Jonah_Simpson)\
**Post date:** [November 18, 2016, 12:23am UTC](https://discuss.hangfire.io/t/hangfire-with-parameterized-constructor/2643/2 "2016-11-18T00:23:15Z")

</div>

If you can refactor your code and move the arguments into your Process method signature then you can avoid a parameterized constructor and pass arguments in like this:

```
BackgroundJob.Enqueue<TaskSSN>(t => t.Process(arg.FileName, arg.FileContent));

```

I don’t think dependency injection will help you in this situation unless your actual code is a lot different than the example you posted.

---

<div class="post-metadata">

**Author:** ![dan.s.ward](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dan.s.ward](https://discuss.hangfire.io/u/dan.s.ward)\
**Post date:** [February 18, 2017, 1:07pm UTC](https://discuss.hangfire.io/t/hangfire-with-parameterized-constructor/2643/3 "2017-02-18T13:07:10Z")

</div>

Move your configuration settings to a setting class.

```
public sealed class Configuration
{
    public string FileName { get; set; }
    public string FileContent { get; set; }
}

```

Modify your constructor to accept the Configuration object.

```
public TaskSSN(Configuration config)
{
    this.fileName = config.FileName;
    this.fileContent = config.FileContent;
}

```

Then, in your Startup class, configure your container. Here’s an example with Simple Injector pulling settings from an app.config.

```
var container = new Container();
container.RegisterSingleton(typeof(Email.Configuration), new Email.Configuration()
{
    FileName = ConfigurationManager.AppSettings["FileName"],
    FileContent = ConfigurationManager.AppSettings["FileContent"]
});
GlobalConfiguration
    .Configuration
    .UseSqlServerStorage("Hangfire")
    .UseMsmqQueues(ConfigurationManager.AppSettings["MSMQ-QueueName"])
    .UseActivator(new SimpleInjectorJobActivator(container));
```
