1

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 ?

Joel Parker
  • 295
  • 1
  • 4
  • 17
  • System.Net can be defined in different assemblies. You have to verify you've referenced the correct one. –  Sep 08 '17 at 15:36
  • I am new to C#, how would I do that ? – Joel Parker Sep 08 '17 at 15:36
  • Well, go look at the documentation on MSDN. At the top they tell you what assembly it is defined in. Go to your References in the project. Have you referenced the assembly yet? –  Sep 08 '17 at 16:40

1 Answers1

1

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.

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60