Joel Parker
Joel Parker

Reputation: 297

Microsoft Bot Framework C# Web Client does not exist in the current context

I am trying to post a message received from the user with web client with the following code

Includes:

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;

Code:

 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);
  }

During compilation I receive the following errors:

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

From the error it seems like it cannot locate WebClient and UriBase but I thought they are in System.Net so I am not sure what is going on. I have read in other posts of people using WebClient so there seems to be a way. Can anyone see what I am doing wrong ?

Upvotes: 1

Views: 589

Answers (1)

Joe Mayo
Joe Mayo

Reputation: 7513

URIBase needs a type and you have a misspelling. Change those two lines to this:

var uriBase = new Uri("https://somedomain.com/");
var builder = new UriBuilder($"{uriBase}/analytics/message?");

Notice the var on uriBase, allowing the variable to assume it's assigned type. You could have also explicitly stated the variable type, like this:

Uri uriBase = new Uri("https://somedomain.com/");

The rest of the errors might have occurred because of this.

Upvotes: 1

Related Questions