0

I'm trying to do a bit of a balancing act here. Currently Azure WebJobs don't support .NET Core.

With some help, I created a .NET Core Console App and made it work as a WebJob. On top of that I'm trying to implement Ninject for DI.

Code compiles fine but when I run it, I'm getting the "No parameterless constructor is defined for this object." error -- see below.

enter image description here

I may be in a bit of unchartered territory here with Azure WebJobs, .NET Core 2.0 and Ninject but any idea what may be causing this?

BTW, I had the same exact code running as a WebJob targeting .NET Framework. I needed to migrate to .NET Core 2.0 because I'm using class libraries that target .NET Core 2.0. I'm also using DI in those class libraries which is why I'm trying to migrate my WebJob to .NET Core 2.0 using Ninject.

P.S. I'm using Azure.WebJobs 3.0.0 beta2 to convert my .NET Core console app to WebJobs. I'm also using Ninject 3.2.2

UPDATE: Here's my code for the JobActivator

public class BrmJobActivator : IJobActivator
    {
        private readonly IKernel _container;

        public BrmJobActivator(IKernel container)
        {
            _container = container;
        }

        public T CreateInstance<T>()
        {
            return _container.Get<T>();
        }
    }

And here's the main Program:

class Program
    {
        static readonly IKernel Kernel = new StandardKernel();
        static JobHostConfiguration config;

        static void Main(string[] args)
        {
            Environment.SetEnvironmentVariable("AzureWebJobsDashboard", "MySettings");
            Environment.SetEnvironmentVariable("AzureWebJobsStorage", "MySettings");

            BootStrapIoc();

            config = new JobHostConfiguration();

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            var host = new JobHost(config);
            host.RunAndBlock();
        }

        private static void BootStrapIoc()
        {
            Kernel.Load(Assembly.GetExecutingAssembly());
            config = new JobHostConfiguration
            {
                JobActivator = new BrmJobActivator(Kernel)
            };
        }
    }

UPDATE 2: I'm now getting the following error.

enter image description here

This error is thrown at the following line -- also see second image. return _container.Get<T>();

enter image description here

UPDATE 3: Here's the code in Functions.cs file:

public class Functions
{

   private static ISomeService1 _someService1;
   private static ISomeService2 _someService2;

   private static IConfiguration _configuration;


   public Functions(ISomeService1 someService1, ISomeService2 someService2, IConfiguration configuration)
   {
       _someService1 = someService1;
       _someService2 = someService2;
       _configuration = configuration;
    }

    public async Task ProcessQueueMessage([QueueTrigger("my-brm-queue")] QueueMessage message, TextWriter log)
    {

        // Consume service
        _someService1.DoSomething(message);

    }

}

UPDATE 4: Here's the code in Ninject bindings class:

public class NinjectBindings : Ninject.Modules.NinjectModule
{
   IConfiguration Configuration;

   public override void Load()
   {
       // Bind to IConfiguration
       var builder = new ConfigurationBuilder();
       builder.SetBasePath(Directory.GetCurrentDirectory());
       builder.AddJsonFile("appsettings.json");
       Configuration = builder.Build();
       Bind<IConfiguration>().ToMethod(ctx => {
          return Configuration;
       });

       // Create instances of clients
       var docDbClient = new ClassLibrary1.DocumentDbClient(Configuration);
       var tsClient = new ClassLibrary2.TableStorageClient(Configuration);

       // Bind Services
       Bind<ISomeService1>().To<SomeService1>();
       Bind<ISomeService2>().To<SomeService2>();

       // Bind Repositories
       Bind<IRepository1>().To<Repository1>();
       Bind<IRepository2>().To<Repository2>();

   }
}
Thomas
  • 24,234
  • 6
  • 81
  • 125
Sam
  • 26,817
  • 58
  • 206
  • 383
  • How did you configure Ninject ? Can we have a look at the code of your JobActivator ? It seems that Ninject tries to create a instance of a dependency that has others dependencies but it couldn't find them. Can you post your code please ? – Thomas Sep 03 '17 at 21:38
  • Please see the UPDATE section in original post. Thanks. – Sam Sep 04 '17 at 01:54
  • how do you regisert your service `ISomeService1`, `ISomeService2` and `IConfiguration` ? Because you should register this services using ninject ??? – Thomas Sep 05 '17 at 09:03
  • Not sure why I forgot to include that part of the code in my last update. Sorry about that. Please see UPDATE 4 in original post. Thanks! – Sam Sep 05 '17 at 16:14
  • Sorry just re-reading the exception. `mscorlib` is part of the .NET Framework and not part of .NET Standard / .NET Core. you may have a dependency on the .Net Frameowrk ??? – Thomas Sep 06 '17 at 21:29
  • Thank you for your response. How can I figure out where that dependency may be? – Sam Sep 07 '17 at 19:12
  • don't really know, need to check the dll your are using to see what are their dependencies and also you can try to check what is dll is for. sorry – Thomas Sep 07 '17 at 21:16
  • Thanks! Looks like this is a dead-end route for me. My initial idea was to use my .NET Standard class libraries in a WebJobs project targeting .NET Framework. I got talked out of it but looks like I'm going back to that route. Thanks for your help. – Sam Sep 08 '17 at 02:48
  • [Ninject **3.3.0**](https://www.nuget.org/packages/Ninject/3.3.0) was released September 26th 2017 and now targets **.NET Standard 2.0** and thus also runs on .NET Core 2.0. – BatteryBackupUnit Oct 02 '17 at 07:47

1 Answers1

0

After instantiating the JobHostConfiguration from the BootStrapIoc method, you re-instantiate it from the main method.

Just remove this line in your main method:

config = new JobHostConfiguration();
Thomas
  • 24,234
  • 6
  • 81
  • 125
  • Great catch! Thank you. However, I'm now having this error: "Could not load type 'System.Runtime.Remoting.RemotingServices' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'." – Sam Sep 04 '17 at 16:53
  • I updated the original post for more details. See UPDATE 2 section. Thanks. – Sam Sep 04 '17 at 17:00
  • Can you please post de code of your triggered function please ? – Thomas Sep 04 '17 at 21:18
  • Thank you for your help. Please see UPDATE 3 in original post for the `Functions.cs` code. – Sam Sep 05 '17 at 01:50