1

ServiceStack.Funq.Quartz Sample Code is

public class MyServices : Service
{
    public object Any(Hello request)
    {
        return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) };
    }
}

public class HelloJob : IJob
{
    private MyServices MyServices { get; set; }
    public HelloJob(MyServices myServices)
    {
        MyServices = myServices;
    }
    public void Execute(IJobExecutionContext context)
    {
        var response = MyServices.Any(new ServiceModel.Hello
        {
            Name = "CodeRevver"
        });
        response.PrintDump();
    }
}

The above is works fine. if I in the MyServices Class, removed the Any function, and comment the Execute inner code.

public class MyServices : Service
{

}

the quartz.net will get the error:

[Quartz.Core.ErrorLogger】 An error occurred instantiating job to be executed. job= 'JobGroup1.GetUserJob111' Problem instantiating type 'ServiceStackWithQuartz.HelloJob'

why the class must have public object Any(Hello request) function ?

mythz
  • 141,670
  • 29
  • 246
  • 390
ali
  • 11
  • 2

1 Answers1

2

Thanks for using the package – I had no idea that other people would find it useful.

So If I understand correctly, in your situation you have:

public class MyServices : Service
{
}

And you’re trying to resolve this Service via constructor injection, which is effectively doing a:

container.Resolve<MyServices>();

This will fail because of the way the ServiceStack Funq IoC works. You can’t resolve a ServiceStack Service that has nothing in it (you'd probably never want to either) – It has to at least have one service implementation, It doesn’t matter what the implementation is.

Also, if you want to improve ServiceStack.Funq.Quartz, feel free to contribute to the code base.

Edit: It's probably worth mentioning that you can inject a "Non-Service" class with your logic in it if you want. You can resolve other classes that aren't based off of ServiceStack.Service even if there's nothing in them.

Edit 2: Responding to your "Service wont dispose" problem. This is the same across ServiceStack and has nothing to do with your Quartz Job. If you call a:

container.Resolve<MyServices>().Any(new new ServiceModel.Hello { });

from AppHost for example, your service wont dispose by itself. If you want it to dispose you can wrap it in a using statement. e.g.

using (var service = MyServices)
{
    var response = MyServices.Any(new ServiceModel.Hello { });
}

The using will ensure that your service will be disposed afterwards.

Alternatively you can add the interface "IDispose" on to your Quartz Job and implement a Dispose() method that will do a:

MyServices.Dispose();  

This will be called after a job has executed.

Michael Clark
  • 462
  • 5
  • 17
  • Thanks for reply. Yes, you are right. because of the way the ServiceStack Funq IoC , It has to at least have one service implementation, the one service if contains (any,put,get,post..), resolve works fine , but I only need some database operations, @mythz has any advice? – ali Apr 23 '16 at 13:20
  • Funq uses `container.Resolve` for constructor injection which represents a mandatory dependency, but if you use **public properties** Funq instead resolves dependencies with `container.TryResolve` which can be optional. – mythz Apr 23 '16 at 16:04
  • At last, I have to in the initialize class add `container.RegisterAutoWiredType(typeof(MyServices));` . but I found the Quartz.net Job Executed, It will not Invoke the `Service.Dispose()` , then If in MyService I Invoke Db Operation, It will not close the connection and dispose(). @Michael Clark – ali Apr 24 '16 at 11:54