小编Dan*_*ndy的帖子

向 .NET Core 注册 RabbitMQ Consumer?

我正在尝试使用消息队列(RabbitMQ)在基于微服务的架构中处理请求授权。

我已经按照这些说明将接收器和发送器配置为 .NET Core 中的控制台应用程序。但是,当在现实世界的示例中使用它时,我的应用程序接收项目不会作为消费者收集消息。

我假设我必须在 中注册消费者Startup.cs,但我似乎无法让它工作。

我的消费者/响应者代码:

public class RabbitMqHandler 
{
    private readonly IJWTFactory _jwtFactory;

    public RabbitMqHandler(IJWTFactory jWTFactory)
    {
        _jwtFactory = jWTFactory;

    }

    public void Register()
    {
        var mqFactory = new ConnectionFactory() { HostName = "localhost" };

        using (var connection = mqFactory.CreateConnection())
        {
            Console.WriteLine("Listening on Rabbit MQ");
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "Authorize", durable: false, exclusive: false, autoDelete: false, arguments: null);

                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body …
Run Code Online (Sandbox Code Playgroud)

c# rabbitmq

6
推荐指数
1
解决办法
3331
查看次数

在RabbitMQ Consumer中使用DbContext(Singleton Service)

我有一个工作正常的RabbitMQ Singleton,但是当消息到达时它依赖于作用域服务:

consumer.Received += _resourcesHandler.ProcessResourceObject; //Scoped Service
Run Code Online (Sandbox Code Playgroud)

我的服务注册如下:

services.AddScoped<IHandler, Handler>();
services.AddSingleton<RabbitMqListener>();
Run Code Online (Sandbox Code Playgroud)

作用域服务构造函数使用DI作为Db上下文:

private readonly ApplicationDbContext _appDbContext;

public ResourcesHandler(ApplicationDbContext appDbContext)
{
    _appDbContext = appDbContext;
}
Run Code Online (Sandbox Code Playgroud)

此作用域服务调用Db上下文,以便在收到消息时将属性插入数据库.

但是,由于作用域服务具有不同的生命周期,因此启动失败.

有一个更好的方法吗?我可以使作用域服务成为单例,但后来我遇到了使用DbContext作为依赖的问题.

DI中用于在单例服务中调用dbContext的"协议"是什么?

我可以使用一个using语句来确保它被丢弃,但是我必须使用DI来传递DbContextOptions.这是实现这一目标的唯一途径吗?

.net c# rabbitmq asp.net-core

6
推荐指数
1
解决办法
846
查看次数

单个组件中的多个 Redux 状态 - Typescript、React、Redux

在身份验证期间,我返回一些需要在整个用户生命周期中携带到其他组件中的内部 ID。这些值保存在authentication状态中,所有其他相关的组件逻辑都保存在resources状态中。

当我在组件中以多个状态运行时,似乎身份验证状态会以某种方式覆盖资源状态,从而使我获得的值undefined尽管 React 状态肯定具有这些值(使用 React Dev Tool 确认)。

这是我的组件中的相关代码:

道具类型:

type ResourceProps =
    ResourceState.ResourceState
    & AuthState.AuthState
    & typeof ResourceState.actionCreators
    & RouteComponentProps<{ }>;
Run Code Online (Sandbox Code Playgroud)

Redux 连接:

export default connect(
    (state: ApplicationState) => state.resources && state.authentication,
    ResourceState.actionCreators 
)(ResourceDisplay) as typeof ResourceDisplay;
Run Code Online (Sandbox Code Playgroud)

我如何使用不同状态的示例。请看我在代码中的评论:

    //customerInfo comes from authentication state.
    if (this.props.customerInfo.subscriptions.length == 0) {
        console.log('empty subs list');
    }
    else {
        this.props.customerInfo.subscriptions.forEach(function (subscription) {
            // retrieveResources is an action being called from resources. This action populates the resources state. …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-router redux

1
推荐指数
1
解决办法
1071
查看次数

Promise fetch 返回 Promise&lt;void | T&gt; 而不是预期的对象

我正在使用类中的 Typescript 进行简单的获取,它应该返回一个承诺,然后我想从这个获取中获取 JSON 响应。但是,无论我做了什么更改,该函数总是返回 a Promise<void | T>,这会破坏我代码中的类型检查。

export class ApiHelper {
    constructor() {     }

    public FetchFromPortal<T>(xyz: string) {

    ------ Other less relevant code -----------

        return fetch(url, options)
            .then((response) => this.CheckStatus(response))
            .then((response) => this.ParseJSON<T>(response))
            .then((response) => { 
                console.log(response);
                return response;
            })
            .catch((error: Error) => { console.log(error) });
    }

    private CheckStatus(response : Response) : Promise<Response> {
        if (response.status >= 200 && response.status < 300) {
            return Promise.resolve(response);
        } else {
            let error = new Error(response.statusText);
            throw error;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

javascript promise typescript

1
推荐指数
1
解决办法
3746
查看次数