.NET Core WebSockets如何获取DbContext

4

我正在测试 .net core,并使用 .net core + websockets 创建一个小的示例应用程序,以将一些数据推送到我的应用程序中。我希望使用 dbcontext 将这些数据保存在数据库中。

然而,我在 websocket 处理程序中遇到了获取 dbcontext 的问题。那么我该如何创建一个可用的 dbcontext 呢?

我的启动 Configure 方法包含以下内容:

...
app.Map("/ws", WSHandler.Map);
...

这是我的WSHandler类,实现从传入连接中读取数据。我需要一个DbContext,以便可以用它来读写数据库。
/// <summary>
/// Handler for an incoming websocket client
/// </summary>
public class WSHandler {
    /// <summary>
    /// Max size in bytes of an incoming/outgoing message
    /// </summary>
    public const int BufferSize = 4096;

    /// <summary>
    /// The socket of the current connection
    /// </summary>
    WebSocket socket;

    /// <summary>
    /// Constructor, assign socket to current instance and adds socket to ConnectedClients. 
    /// </summary>
    /// <param name="socket"></param>
    WSHandler(WebSocket socket) {
        this.socket = socket;
    }

    /// <summary>
    /// Configure app to use websockets and register handler.
    /// </summary>
    /// <param name="app"></param>
    public static void Map(IApplicationBuilder app) {
        app.UseWebSockets();
        app.Use((WSHandler.Acceptor);
    }

    /// <summary>
    /// Accept HttpContext and handles constructs instance of WSHandler.
    /// </summary>
    /// <param name="hc">The HttpContext</param>
    /// <param name="n">Task n</param>
    /// <returns></returns>
    static async Task Acceptor(HttpContext hc, Func<Task> n) {
         if (hc.WebSockets.IsWebSocketRequest == false) {
            return; 
        }

        var socket = await hc.WebSockets.AcceptWebSocketAsync();
        var h = new WSHandler(socket);
        await h.Loop();
    }

    /// <summary>
    /// Wait's for incoming messages 
    /// </summary>
    /// <returns></returns>
    async Task Loop() {
        var buffer = new Byte[BufferSize];
        ArraySegment<Byte> segment = new ArraySegment<byte>(buffer);
        while (this.socket.State == WebSocketState.Open) {
            WebSocketReceiveResult result = null;
            do {
                result = await socket.ReceiveAsync(segment, CancellationToken.None);
            } while (result.EndOfMessage == false);

            // do something with message here. I want to save parse and save to database
        }

    }
}

嗨,@John Smith。你解决了这个问题吗?我现在正在寻找解决方案。 - Ilya Loskutov
@Mergasov 是的,让我去看代码,然后我会发布答案。 - user2882307
1个回答

1

由于这篇文章有一些关注度,我已经添加了我使用的解决方案。

您可以通过 HttpContext 访问所有服务。所以我从该服务中获取上下文选项,然后在需要时创建上下文。请注意,不建议使用长时间存在的上下文,如果发生错误,则 DbContext 将无法使用。最终,我实现了一个不同的数据库缓存层,并将其写入 WebSocket 处理程序中,而不是直接使用 DbContext 本身。

以下是扩展创建 DbContexts 的代码。由于我现在没有可用的 Visual Studio,因此尚未对其进行测试...

 <summary>
/// Handler for an incoming websocket client
/// </summary>
public class WSHandler {
    /// <summary>
    /// Max size in bytes of an incoming/outgoing message
    /// </summary>
    public const int BufferSize = 4096;

    /// <summary>
    /// The socket of the current connection
    /// </summary>
    WebSocket socket;

    /// <summary>
    /// The options to create DbContexts with.
    /// </summary>
    DbContextOptions<AssemblyServerContext> ctxOpts;

    /// <summary>
    /// Constructor, assign socket to current instance and adds socket to ConnectedClients. 
    /// </summary>
    /// <param name="socket"></param>
    /// <param name="ctxOpts"></param>
    WSHandler(WebSocket socket, DbContextOptions<AssemblyServerContext> ctxOpts) {
        this.ctxOpts = ctxOpts;
        this.socket = socket;
    }

    /// <summary>
    /// Configure app to use websockets and register handler.
    /// </summary>
    /// <param name="app"></param>
    public static void Map(IApplicationBuilder app) {
        app.UseWebSockets();
        app.Use((WSHandler.Acceptor);
    }

    /// <summary>
    /// Accept HttpContext and handles constructs instance of WSHandler.
    /// </summary>
    /// <param name="hc">The HttpContext</param>
    /// <param name="n">Task n</param>
    /// <returns></returns>
    static async Task Acceptor(HttpContext hc, Func<Task> n) {
         if (hc.WebSockets.IsWebSocketRequest == false) {
            return; 
        }

        var socket = await hc.WebSockets.AcceptWebSocketAsync();
        var ctxOptions = hc.RequestServices.GetService<DbContextOptions<AssemblyServerContext>>();
        var h = new WSHandler(socket, ctxOptions);
        await h.Loop();
    }

    /// <summary>
    /// Wait's for incoming messages 
    /// </summary>
    /// <returns></returns>
    async Task Loop() {
        var buffer = new Byte[BufferSize];
        ArraySegment<Byte> segment = new ArraySegment<byte>(buffer);
        while (this.socket.State == WebSocketState.Open) {
            WebSocketReceiveResult result = null;
            do {
                result = await socket.ReceiveAsync(segment, CancellationToken.None);
            } while (result.EndOfMessage == false);

            // do something with message here. I want to save parse and save to database
            using (var ctx = new AssemblyServerContext(ctxOpts)) {

            }
        }

    }
}

你能提供一个使用缓存层处理这个问题的例子吗? - jordanpowell88
@jpdesigning,我无法提供代码,因为我不再拥有它的访问权限。我的解决方法是创建了一个长时间运行的后台线程,定期从线程安全队列中读取数据,并将来自该队列的更改保存到数据库中。WebSocket逻辑只需将信息传递给该队列即可。 - user2882307

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接