StringContent问题

时间:2017-10-02 09:57:44

标签: c# .net .net-core slack-api

这段代码真的没有问题,做了很多StringContent POST动作,但这是第一次发生...

这段代码只是用HttpClient直接将数据发送到Slack。代码将一直运行到PostAsync方法,然后崩溃,不会产生异常日志。我唯一的信息是当我调试到.PostAsync时,并利用我在那个时间点的当前变量来检索StringContent所具有的错误。我是否需要遵循任何结构才能使StringContent成为一个类型?

error CS0119: 'StringContent' is a type, which is not valid in the given context

enter image description here

整个班级的代码在这里:

namespace XXXXXXXXX
{
    public class SlackClient : ISlackClient
    {
        private readonly string _urlWithAccessToken;
        private readonly Uri _uri;
        private readonly Encoding _encoding = new UTF8Encoding();

        public SlackClient(string urlWithAccessToken)
        {
            _urlWithAccessToken = urlWithAccessToken;
            _uri = new Uri(urlWithAccessToken);
        }

        public async Task TestPostMessageAsync()
        {
            HttpClient client = new HttpClient();

            await client.PostAsync(_urlWithAccessToken, new StringContent(JsonConvert.SerializeObject(new BasicPayload() {
                Text = "Hello World, this is from .NET Core"
            }), _encoding, "application/json"));
        }

        //Post a message using simple strings  
        public async Task PostMessageAsync(string text, string username = null, string channel = null)
        {
            Payload payload = new Payload()
            {
                Channel = channel,
                Username = username,
                Text = text
            };

            await PostMessageAsync(payload);
        }

        //Post a message using a Payload object  
        public async Task PostMessageAsync(Payload payload)
        {
            try {
                var client = new HttpClient();
                //string postBodyStr = JsonConvert.SerializeObject(payload);
                // var stringContent = new StringContent(postBodyStr);

                var response = await client.PostAsync(_urlWithAccessToken, new StringContent(JsonConvert.SerializeObject(payload), _encoding));

                response.EnsureSuccessStatusCode();
                //The response text is usually "ok"  
                //string responseText = _encoding.GetString(response);
            }
            catch (Exception ex) { 
                Console.WriteLine("[SlackClient] POST to Webhook failed: " + ex.ToString());
            }
        }
    }
}

ISlackClient只包含继承类遵循的方法,实际上并没有什么。 Payload对象包含Slack为其Webhooks提供的格式,并将JsonProperty绑定到每个属性。

执行SlackClient的控制器:

private readonly UserManager<User> _userManager;
        private readonly SignInManager<User> _signInManager;
        private readonly ILogger<HomeController> _logger;
        private readonly RavenLogger _rLogger;
        private readonly SlackClient _slackLogger;

        public HomeController(ILogger<HomeController> logger,
          UserManager<User> userManager,
          SignInManager<User> signInManager,
          IRavenClient _ravenClient)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _logger = logger;
            _rLogger = new RavenLogger("HomeController", _ravenClient, LogLevel.Information);
            _slackLogger = new SlackClient(ApplicationSettings.GetString("Slack/CoreLoggingWebHook"));
        }

        public async Task<IActionResult> Index()
        {
            // Require the user to have a confirmed email before they can log on.
            var user = await _userManager.GetUserAsync(HttpContext.User);

            // Log to Sentry
            //_rLogger.LogInformation(
            //(int) LogEnum.VIEWINDEX, null, $"{HttpContext.Connection.RemoteIpAddress} Viewed Index");
            //await new SlackClient(ApplicationSettings.GetString("Slack/CoreLoggingWebHook")).PostMessageAsync("[GET Home/Index] Called by " + HttpContext.Connection.RemoteIpAddress
                    //, "HomeController", "#core-log");
            await _slackLogger.TestPostMessageAsync();

            if (user != null)
            {
                return View(new IndexViewModel
                {
                    DisplayName = user.FirstName,
                    HasPassword = await _userManager.HasPasswordAsync(user),
                    PhoneNumber = await _userManager.GetPhoneNumberAsync(user),
                    TwoFactor = await _userManager.GetTwoFactorEnabledAsync(user),
                    Logins = await _userManager.GetLoginsAsync(user),
                    BrowserRemembered = await _signInManager.IsTwoFactorClientRememberedAsync(user),
                    IsDriver = user.DriverId > 0
                });
            }

            return View();
        }

0 个答案:

没有答案