Comments (3)
Hi!
Sure, the handler is just another class, meaning you should be able to inject dependencies as usual in ASP.NET Core.
Please let us know if this doesn't work as intended :)
Radu M
from websocket-manager.
I try to use the entityframework context as a dependency and it throws me the next exception:
System.InvalidOperationException: 'Cannot consume scoped service 'app.DataContext' from singleton 'app.WebSocketService'.'
My Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCors(options => options.AddPolicy("AllowAllOrigins", builder => builder.AllowAnyOrigin()));
services.AddOptions();
services.AddSingleton<IConfiguration>(Configuration);
services.AddDbContext<DataContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("Test"));
});
services.AddWebSocketManager();
}
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider)
{
app.UseWebSockets();
app.MapWebSocketManager("/service", serviceProvider.GetService<WebSocketService>());
app.UseMvc();
app.UseCors("AllowAllOrigins");
}
}
My Subclass
public partial class WebSocketService : WebSocketHandler
{
private readonly DataContext _context;
public WebSocketService(WebSocketConnectionManager webSocketConnectionManager, DataContext context)
: base(webSocketConnectionManager)
{
_context = context;
}
public override async Task OnConnected(WebSocket socket)
{
await base.OnConnected(socket);
}
public override async Task OnDisconnected(WebSocket socket)
{
await base.OnDisconnected(socket);
}
}
from websocket-manager.
Hi, did you solve this problem ?
from websocket-manager.
Related Issues (20)
- Unnecessary assignment (?) HOT 4
- Thread safety
- how to access HttpContext etc. in the sub class inherits from WebSocketHandler? HOT 3
- Unable to use send/receive complex types that are not part of the libarary HOT 4
- json format to sent message HOT 2
- Inject dependency rather than using new
- json error in mvc sample HOT 1
- How to call server in c# client
- Cannot inject dependencies in the sockethandler or use servicelocator to do so HOT 1
- WebSocketHandler and WebSocketConnectionManager lifetime HOT 1
- The remote party closed the WebSocket connection without completing the close handshake. HOT 2
- Build string to send in socket to server
- NuGet package upgrade
- When hanlder ReceiveAsync method is doing something take a long time,the websocket cannot receive any message
- I fail to run WebTerm HOT 1
- NuGet update
- How to hosting Chat application in IIS HOT 2
- Suggestions: ControllerMethodInvocationStrategy with Async Tasks and ASP.NET Core 3.x Endpoints
- System.OutOfMemoryException
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 websocket-manager.