ReusedWithin ReuseScope.Container is Ignored

We are experiencing differences between a service running on .net core and .net framework.

We’re currently using SS 5.1.0 and .net core version 2.1.300.

When we register a WebServices

G.IOC.Register(c => new Service0()).ReusedWithin(ReuseScope.Container);

When running on .net core on windows 10

  1. The service Service0 is recreated on every call to the web-service regardless of the value of ReusedWithin setting

  2. The public instance variables are set to null before the destructor is called. Private instance variables are unchanged. Note: Our code is not setting the public values to null.

Notes: In our example there are two other services that are being started after this. At one point this worked, and we don’t know what has changed to affect it.

Do you have any techniques to help us troubleshoot this?

Again, the .NET Framework running on windows 10 version works correctly.

As always thanks for your help!

Also, this is a standalone app that started from the windows command line.

Container is the same as singleton as there’s only 1 container in ServiceStack’s Funq. You can use None to register a transient dependency or Request for a per-request dependency for HTTP Requests.

I’m not sure what you mean by registering Web Services, but ServiceStack Services need to be registered as transient, i.e. ReuseScope.None.

Are you saying that Container scope should not be used for http request?

The Container scope is same as singleton (which is the default scope) so should only be used for singleton/thread-safe dependencies.

ServiceStack Services need to be transient, but it’s not clear if that’s what your registering.

Our Service object inherits from ServiceStack.Service and contains methods for get/put/post calls. It is thread safe.

We found this article in which you explain everything

The issue is we’re not using autowired since we have several named depednancys that we manually populate in the empty constructor.

BUT FYI, all of this works in .net framework and does not in .net core.

We are going to take your suggestion and use ReuseScope.Request scope and allow the Service() object to be created everytime. Does that make sense?

I think the difference between .net core and .net framekwork is as follows:

.net framework saw our public variables and knew they were set as Registered in the IOC but not RegisterAutoWired. And they were left alone.

But

.net core saw our public variables, and did not check if they were Registered in the IOC but assumed they were RegisterAutoWired and set them to null as no value was set for the RegisterAutoWired for that object.

This is my current working theory.

Just use ReuseScope.None so it’s consistent like every other ServiceStack Service. Request scope only works in the context of a HTTP Request. There is still only 1 instance of the Service created per request so Request scope isn’t giving you anything except incompatibility issues when trying to use your Service outside a HTTP Request.