public WeixinWelcomePageMiddleware(
            RequestDelegate next,
            IOptions <WeixinWelcomePageOptions> options,
            ILoggerFactory loggerFactory,
            IWeixinMessageEncryptor encryptor)
        {
            _next      = next ?? throw new ArgumentNullException(nameof(next));
            _logger    = loggerFactory?.CreateLogger <WeixinWelcomePageMiddleware>() ?? throw new ArgumentNullException(nameof(loggerFactory));
            _options   = options?.Value ?? throw new ArgumentNullException(nameof(options));
            _encryptor = encryptor ?? throw new ArgumentNullException(nameof(encryptor));

            //入参检查
            if (string.IsNullOrEmpty(_options.WebsiteToken))
            {
                throw new ArgumentException($"参数 {nameof(_options.WebsiteToken)} 不能为空。");
            }

            if (string.IsNullOrEmpty(_options.Path))
            {
                throw new ArgumentException($"参数 {nameof(_options.Path)} 不能为空。");
            }

            _backchannel = new HttpClient(new HttpClientHandler());
            _backchannel.DefaultRequestHeaders.UserAgent.ParseAdd("AspNetCoreWeixin");
            _backchannel.Timeout = TimeSpan.FromSeconds(60);
            _backchannel.MaxResponseContentBufferSize = 1024 * 1024 * 10;             // 10 MB
        }
Esempio n. 2
0
        internal async Task <bool> InitializeAsync(
            WeixinWelcomePageOptions options,
            HttpContext context,
            ILogger logger,
            IWeixinMessageEncryptor encryptor)
        {
            _options    = options ?? throw new ArgumentNullException(nameof(options));
            HttpContext = context ?? throw new ArgumentNullException(nameof(context));
            _logger     = logger ?? throw new ArgumentNullException(nameof(logger));
            _encryptor  = encryptor ?? throw new ArgumentNullException(nameof(encryptor));

            context.Response.OnStarting(OnStartingCallback, this);
            {
                InitializeResult = await HandleOnceAsync();

                if (InitializeResult?.Handled == true)
                {
                    return(true);
                }
                if (InitializeResult?.Failure != null)
                {
                    _logger.LogWarning(0, InitializeResult.Failure, InitializeResult.Failure.Message);
                }
            }
            return(false);
        }