Coder Social home page Coder Social logo

Comments (7)

NimaAra avatar NimaAra commented on July 23, 2024

I cannot help you without looking at your code. Please consider including a reproducible example which you can then zip up and attach to this issue for me to look at.

from easy.messagehub.

jmdavid avatar jmdavid commented on July 23, 2024

I do have the same problem. I send only once and receive 2.

from easy.messagehub.

NimaAra avatar NimaAra commented on July 23, 2024

Please provide a reproducible example for me to look at.

from easy.messagehub.

jmdavid avatar jmdavid commented on July 23, 2024

Thanks for answering.

Dotnet Core 2.1.101

in Startup.cs:

services.AddSingleton<ISandboxService, SandboxService>();
services.AddSingleton<IActWorkflowService, ActWorkflowService>();
services.AddSingleton<IActTaskService, ActTaskService>();

services.AddSingleton<IMessageHub, MessageHub>();

I have a service locator to inject singletons from Startup based on that article: https://dotnetcoretutorials.com/2018/05/06/servicelocator-shim-for-net-core/

In each service, I have:

private readonly IMessageHub hub;
private readonly Guid subscriptionToken;

And in the constructors of each service, I have this (just the name of the service is different in Console.WriteLine:

hub = MyServiceLocator.Current.GetInstance<IMessageHub>();
subscriptionToken = hub.Subscribe<ActHubMessage>(p => Console.WriteLine($"ActTaskService({subscriptionToken}): {p.messageType}"));

In SandboxService, I publish an object with a messageType property:

var msg1 = new ActHubMessage()
{
    messageType = ActHubMessage.HubMessageType.WorkflowTransition,
    workflowId = 1
};
hub.Publish(msg1);

Here is the output:

ActWorkflowService(bc467ca9-64c0-47e4-868d-fd1692bcbe03): TaskCancel
SandboxService(1e4ce11d-281a-4208-bd1c-886f5cd95f5b): TaskCancel
ActWorkflowService(e8c259d3-be2c-4cdc-8eb9-11fae38c7d7b): TaskCancel
ActTaskService(67e5bbdf-f83f-4752-aba5-7faaadaed9d1): TaskCancel
ActTaskService(73c1999f-e7c2-4f6c-998d-c01d504a946d): TaskCancel

Notice that SandboxService have only 1 token (it is the service that publish, the only one that both publish AND subscribe); in the other services, I have 2 tokens.

from easy.messagehub.

jmdavid avatar jmdavid commented on July 23, 2024

just in case:

namespace MyBackend
{
    //FROM THIS ARTICLE: https://dotnetcoretutorials.com/2018/05/06/servicelocator-shim-for-net-core/
    public class MyServiceLocator
    {
        private ServiceProvider _currentServiceProvider;
        private static ServiceProvider _serviceProvider;

        public MyServiceLocator(ServiceProvider currentServiceProvider)
        {
            _currentServiceProvider = currentServiceProvider;
        }

        public static MyServiceLocator Current
        {
            get
            {
                return new MyServiceLocator(_serviceProvider);
            }
        }

        public static void SetLocatorProvider(ServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public object GetInstance(Type serviceType)
        {
            return _currentServiceProvider.GetService(serviceType);
        }

        public TService GetInstance<TService>()
        {
            return _currentServiceProvider.GetService<TService>();
        }
    }
}

from easy.messagehub.

NimaAra avatar NimaAra commented on July 23, 2024

Please put all of that into a project where I can clone or even zip it up.

from easy.messagehub.

jmdavid avatar jmdavid commented on July 23, 2024

The problem seems to be the mix of DI.

In 2.1, it was not possible to inject in service like we do in controllers.

This works:

public SandboxController(ISandboxService _sandboxService, IActTaskService _actTaskService...

This NOT works:

public SandboxService(IActTaskService _taskService...

That's why I used the ServiceLocator to inject services in other services like this:

actTaskService = MyServiceLocator.Current.GetInstance<IActTaskService>();

However, I still use the first pattern in my controllers. And I realized that I inject ActTaskService and ActWorkflowService in 2 controllers, in correponding controller (ActTaskController, ActWorfklowController) AND in SandboxController (that I use as a scratchpad). As long as I removed the 2 services from SandboxController, the class is instantiated only once.

But I wonder why injection in controllers creates a new instance.

from easy.messagehub.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.