在asp.net核心webapi中启动时创建连接的最佳方法是什么?

问题描述 投票:0回答:1

我想使用JamaaTech Smpp库创建一个SmppClient。与我将创建的客户端一起,我想收听传入的短信并将短信正文发送到Rabbitmq队列。我的问题是:如何在应用程序启动时仅创建一个smppclient实例,并将其注入其他服务以进行操作。

这是我用jamaatech创建我的smppclient的方式。

private SmppClient CreateSmppClient(ISmppConfiguration config)
    {
        var client = new SmppClient
        {
            Name = config.Name
        };
        //client.SmppEncodingService = new SmppEncodingService(System.Text.Encoding.UTF8);

        client.ConnectionStateChanged += Client_ConnectionStateChanged;
        client.StateChanged += Client_StateChanged;
        client.MessageSent += Client_MessageSent;
        client.MessageDelivered += Client_MessageDelivered;
        client.MessageReceived += Client_MessageReceived;

        var properties = client.Properties;
        properties.SystemID = config.SystemId;// "mysystemid";
        properties.Password = config.Password;// "mypassword";
        properties.Port = config.Port;// 2034; //IP port to use
        properties.Host = config.Host;// "196.23.3.12"; //SMSC host name or IP Address
        properties.SystemType = config.SystemType;// "mysystemtype";
        properties.DefaultServiceType = config.DefaultServiceType;// "mydefaultservicetype";
        properties.DefaultEncoding = config.Encoding;

        //Resume a lost connection after 30 seconds
        client.AutoReconnectDelay = config.AutoReconnectDelay;

        //Send Enquirer Link PDU every 15 seconds
        client.KeepAliveInterval = config.KeepAliveInterval;

        return client;
    }
c# asp.net-core dependency-injection asp.net-core-webapi smpp
1个回答
0
投票

1-创建您自己的接口ISmppClient和SmppClient类

2-在startup.cs中配置依赖项

services.AddSingleton<IMySmppClient , MySmppClient >();

3-将Contructor的IMySmppClient注入您需要的类中

© www.soinside.com 2019 - 2024. All rights reserved.