Posted on Leave a comment

Display Herocard in BotFramework

As the botframework evovles, many things are changing in the apis. I wanted to create a Herocard for a bot and the previously existed method wasn’t working, so i find the new way that one can reply herocards in the user. The main function needed is the

context.MakeMessage()

 

Full C# example

 List <CardAction> list = new List<CardAction>();
            list.Add(new CardAction {Title="help", Type=ActionTypes.ImBack, Value="help" });
            list.Add(new CardAction {Title = "commands", Type = ActionTypes.ImBack, Value = "commands" });
            HeroCard hero = new HeroCard();
            hero.Title = "Hero title";
            hero.Text = "Hero text";
            hero.Buttons = list;

            var msg = context.MakeMessage();
            msg.Attachments.Add(hero.ToAttachment());
            await context.PostAsync(msg);

 

The result will look like that

 

This github repo is very useful as it includes many botframework samples.

Posted on Leave a comment

Application Insights VS2015 failed to add applicationinsights.config

If you ever deal with the problem

nuget package install  failed to add applicationinsights.config

in Visual studio 2015 and Windows 10 you only have to create a file and name it  ApplicationInsights.config with the code below. That’s because vs and nuget cannot create this file in the root folder of your project.

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
  <InstrumentationKey>xxxx</InstrumentationKey>
</ApplicationInsights>

You must enter your specific InstrumentationKey for the application that you have registered through Azure portal for Insights.

The trick is that you must select Build Action as Content and Copy always to output folder

vs2015insights

Posted on 2 Comments

UnauthorizedAccessException C# File.IO Async

I had to upgrade one application and put some extra functionality. So i added an async function that handles a .txt file and edit some fields.

So i used this code

StorageFolder storageFolder = ApplicationData.Current.RoamingFolder;
 storageFile = await storageFolder.CreateFileAsync("file.txt",CreationCollisionOption.ReplaceExisting);
await FileIO.AppendTextAsync(storageFile, "saveSomeText");

 

But i also had a function in MainPage that asynchronously was reading the same file

StorageFolder storageFolder = ApplicationData.Current.RoamingFolder;
StorageFile storageFile;
storageFile = await storageFolder.CreateFileAsync("file.txt",CreationCollisionOption.OpenIfExists);
IList<string> list = await FileIO.ReadLinesAsync(storageFile);

 

Both functions were async so we could not determine when each function was running and which will be executed first.

So when i tried to run the app i came across with UnauthorizedAccessException because both function maybe were trying to access the file the same time. So i have to make one function wait. You can achieve that by many ways. I used the below function.

 

            async void waitsomething()
            {
                await Task.Delay(2000);
            }

 

But i want to execute this code only once. So i used localSettings

                try
                {
                    tempstr = App.localSettings.Values["YOURkey"].ToString();
                }
                catch (NullReferenceException exmas)
                {
                    waitsomething();
                }