3

I have a service that needs to return a code each time method GetNext() is called. For that, I have several code providers implementing an interface. The same code provider should be used between ServiceHost opening and closing, and so for each service call. I would like to avoid creating one instance of the code provider at each call.

I already looked at several posts, in particular dependency injection for WCF services (mostly Mark Seemann's answers), where solutions using IInstanceProvider and ServiceHostFactory allow to pass parameters during service construction. Perhaps I did not fully understood what was happening there, as I have no extended knowledge of WCF. I ended up implementing a static factory and using it in the service implementation as follows:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single,
                 InstanceContextMode = InstanceContextMode.PerCall)]
public class CodeService : ICodeService
{
    public string GetNext()
    {
        return CodeProviderFactory.GetCodeProvider().GetNextCode();
    }
}

If needed, I could extend this by passing the type of code required to the factory, getting it from configuration DB or file, but that is not the point. Is this the best way to have one single instance of the code provider, and if not, what would be the correct (i.e. more elegant, performant and maintainable) solution? Also is the concern of having a single instance of the code provider legitimate?

evilmandarine
  • 4,241
  • 4
  • 17
  • 40
  • Are you using an IoC container? If so, which one? Certain containers have WCF extensibility so you can use their factory and get up and running in a few minutes without having to roll your own. – moarboilerplate Sep 24 '15 at 16:05
  • Another suggestion: You can register an instance of your CodeProviderFactory as a singleton and inject it into the constructor of your service, effectively making it function the same as a static invocation. – moarboilerplate Sep 24 '15 at 16:08
  • I -tried- to use SimpleInjector but client side, not server side. After reading a considerable amount of articles and tutorials about DI, I stopped because it was too complex for me, but also because project size is not very big so I felt it was not worth it. Still I would like to write code as much extensible as possible and on a solid base. – evilmandarine Sep 24 '15 at 16:11
  • Well, passing parameters during construction *is* DI, so if you are trying to go that route I would strongly recommend using a container and existing tools over writing your own factory (which is actually more complex). Otherwise, I'd keep your code the way it is. Just make sure CodeProviderFactory doesn't morph into a service locator that resolves all dependencies. – moarboilerplate Sep 24 '15 at 16:31
  • I am trying to understand your question. Do you want to use only a single Code Provider? When do you decide to use a different one? Do you want to be able to specify a CodeProvider for each ServiceHost? – Yacoub Massad Sep 24 '15 at 17:16
  • 2
    Did you see [this question](http://stackoverflow.com/questions/2454850/how-do-i-pass-values-to-the-constructor-on-my-wcf-service)? – Yacoub Massad Sep 24 '15 at 17:24
  • Yes, I saw it, and I tried to include that example in my code, without success. Did not understand how to obtain the instance. And as moarboilerplate said, perhaps I can stick with the factory in my case. There may be several Code Provider types, but I want one instance of each during the time server runs, not one instance per call. So I guess yes, that's one (of each type) by service host. – evilmandarine Sep 25 '15 at 08:15

0 Answers0