当前上下文中不存在 Microsoft Bot Framework C# Web 客户端
Microsoft Bot Framework C# Web Client does not exist in the current context
我正在尝试 post 使用以下代码
使用 Web 客户端从用户那里收到的消息
包括:
using System;
using System.Net;
using System.Threading;
using Newtonsoft.Json;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
代码:
URIBase = new Uri("https://somedomain.com/");
var builder = new UriBuilder($"{UriBase}/analytics/message?");
using (WebClient webclient = new WebClient())
{
//Set the encoding to UTF8
webclient.Encoding = System.Text.Encoding.UTF8;
webclient.Headers.Add("Content-Type", "application/json");
var postBody = $"{{\"message\": \"{this.answer}\", \"key\": \"7F02D18E-88E7-486D-B51F-550118491CB1\"}}";
webclient.UploadString(builder.Uri, postBody);
}
在编译期间我收到以下错误:
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(98,17): error CS0103: The name 'URIBase' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(99,49): error CS0103: The name 'UriBase' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(113,17): error CS0103: The name 'webclient' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(114,38): error CS0103: The name 'webclient' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(114,74): error CS0103: The name 'postBody' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(115,17): error CS0103: The name 'chatresponse' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(115,32): error CS0103: The name 'JObject' does not exist in the current context
从错误看来它无法找到 WebClient
和 UriBase
但我认为它们在 System.Net
中所以我不确定发生了什么。我读过其他 post 的人使用 WebClient
所以似乎有办法。谁能看到我做错了什么?
URIBase
需要一个类型,而您拼写错误。将这两行更改为:
var uriBase = new Uri("https://somedomain.com/");
var builder = new UriBuilder($"{uriBase}/analytics/message?");
注意 uriBase
上的 var
,允许变量假定它的分配类型。您也可以明确声明变量类型,如下所示:
Uri uriBase = new Uri("https://somedomain.com/");
其余的错误可能是因此而发生的。
我正在尝试 post 使用以下代码
使用 Web 客户端从用户那里收到的消息包括:
using System;
using System.Net;
using System.Threading;
using Newtonsoft.Json;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
代码:
URIBase = new Uri("https://somedomain.com/");
var builder = new UriBuilder($"{UriBase}/analytics/message?");
using (WebClient webclient = new WebClient())
{
//Set the encoding to UTF8
webclient.Encoding = System.Text.Encoding.UTF8;
webclient.Headers.Add("Content-Type", "application/json");
var postBody = $"{{\"message\": \"{this.answer}\", \"key\": \"7F02D18E-88E7-486D-B51F-550118491CB1\"}}";
webclient.UploadString(builder.Uri, postBody);
}
在编译期间我收到以下错误:
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(98,17): error CS0103: The name 'URIBase' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(99,49): error CS0103: The name 'UriBase' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(113,17): error CS0103: The name 'webclient' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(114,38): error CS0103: The name 'webclient' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(114,74): error CS0103: The name 'postBody' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(115,17): error CS0103: The name 'chatresponse' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(115,32): error CS0103: The name 'JObject' does not exist in the current context
从错误看来它无法找到 WebClient
和 UriBase
但我认为它们在 System.Net
中所以我不确定发生了什么。我读过其他 post 的人使用 WebClient
所以似乎有办法。谁能看到我做错了什么?
URIBase
需要一个类型,而您拼写错误。将这两行更改为:
var uriBase = new Uri("https://somedomain.com/");
var builder = new UriBuilder($"{uriBase}/analytics/message?");
注意 uriBase
上的 var
,允许变量假定它的分配类型。您也可以明确声明变量类型,如下所示:
Uri uriBase = new Uri("https://somedomain.com/");
其余的错误可能是因此而发生的。