Comments (7)
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.
I do have the same problem. I send only once and receive 2.
from easy.messagehub.
Please provide a reproducible example for me to look at.
from easy.messagehub.
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.
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.
Please put all of that into a project where I can clone or even zip it up.
from easy.messagehub.
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)
- Registering IMessageHub with MessageHub HOT 1
- Subscribing a runtime type HOT 5
- Publish & Subscribe from different Project? HOT 2
- Non-singleton instances HOT 8
- Sometimes Unsubscribe method hangs HOT 6
- How to setup one Server with Many cleints between ASP Core web and .net Console service HOT 1
- System.ObjectDisposedException: ThreadLocal object has been deleted. HOT 1
- NuGet package?
- Async handlers HOT 2
- Get MessageHub to play nicely with Autofac in a web application HOT 1
- Conditional subscription HOT 6
- Unflexible detection of the given type HOT 4
- Support for async actions HOT 2
- Support for more profiles? HOT 3
- Available for dotnet core? HOT 2
- Add async version HOT 6
- MessageHub is holding objects in Memory HOT 18
- UnSubscribe method causes ArgumentNullException HOT 1
- Publish and subscribe from different threads HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from easy.messagehub.