Saturday, April 30, 2016

Session : Azure IoT Hub at Azure Roadshow 2016, Sri Lanka

For the second consecutive year in Sri Lanka, Infront Consulting Private Limited along with Sri Lankan Microsoft MVPs carried out the Azure Roadshow 2016 on 29th April. I was privileged enough to deliver a session there along with Fiqri Ismail, who is one of Sri Lanka’s most senior MVPs.

There were mainly two tracks, IT Pro Track and Developer Track which were filled with great sessions delivered by following list of speakers.

image
Azure Roadshow 2016 - Speakers
Our session was about Azure IoT Hub and it was delivered under Developer track. Fiqri started off with the session giving an introduction to IoT and what Azure is offering for IoT. Then after explaining what Azure IoT Hub is, Fiqri handed over the session to me to carry out the demos. There I have demoed how we can use Azure IoT Hub to send messages from Device to Cloud and from Cloud to Device. Then moved into Azure IoT Suite and demoed how we can easily create a remote monitoring application using provided templates. The session concluded demoing adding a custom device where a demo application written using C sends telemetry data to created remote monitoring application.

Happy Coding.

Regards,
Jaliya

Wednesday, April 27, 2016

MultipartFormDataContent Add Extension Method to Accept Custom Header Values

By default MultipartFormDataContent.Add method has three overloads.
  • Add(HttpContent content)
  • Add(HttpContent content, string name)
  • Add(HttpContent content, string name, string fileName)
In this post let’s see how we can add an extension method to MultipartFormDataContent to add a new overload method which will let us send additional parameters.
  • Add(HttpContent content, string name, string fileName, object headerValues)
This is our Extension Method.
public static void Add(this MultipartFormDataContent form, HttpContent content, string name, string fileName, object headerValues)
{
    var header = new ContentDispositionHeaderValue("form-data")
    {
        Name = name,
        FileName = fileName
    };
 
    var headerParameters = new HttpRouteValueDictionary(headerValues);
    foreach (var parameter in headerParameters)
    {
        header.Parameters.Add(new NameValueHeaderValue(parameter.Key, parameter.Value.ToString()));
    }
 
    content.Headers.ContentDisposition = header;
    form.Add(content);
}
And this is how you can pass values.
using (var multipartFormDataContent = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
{
    multipartFormDataContent.Add(new StreamContent(new MemoryStream(data)), name, fileName, new
    {
        Parameter1="HelloWorld"
    });
}
This is how you can read the values.
MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
 
Func<ICollection<NameValueHeaderValue>, string, string> GetHeaderValueByKey = (headerValues, key) =>
{
   if (headerValues == null)
       return null;
 
   NameValueHeaderValue nameValueHeaderValue = headerValues.FirstOrDefault(x => x.Name.Equals(key, StringComparison.OrdinalIgnoreCase));
   return nameValueHeaderValue != null ? nameValueHeaderValue.Value : null;
}; 

HttpContent file = provider.Contents.FirstOrDefault();
string parameter1Value = GetHeaderValueByKey(file.Headers.ContentDisposition.Parameters, "Parameter1");

image
Result
Happy Coding.

Regards,
Jaliya