I am trying to create Container based on the user-id and within one container I want to upload 5 blobs as text files. The client side is Windows app (UWP).
The user-id and text files are sent to a post request (webapi2) and from here container is created and within that files are uploaded. At the same time, the container name is given to the service bus queue which should be read by worker role.
Below is the client code:
foreach (StorageFile file in files)
{
filesListView.Items.Add(file.DisplayName);
IInputStream inputStream = await file.OpenAsync(FileAccessMode.Read);
HttpMultipartFormDataContent multipartContent = new HttpMultipartFormDataContent();
//if (i == false)
//{
multipartContent.Add(new HttpStringContent(txtbox.Text), "myText");
i = true;
// }
multipartContent.Add(new HttpStreamContent(inputStream), "myFile", file.Name);
HttpClient client = new HttpClient();
Uri uri = new Uri("http://localhost:35095/api/upload");
HttpResponseMessage response = await client.PostAsync(uri,multipartContent);
}
The post method is as follows which also includes the function to send container name to the service bus queue.
foreach (HttpContent ctnt in prvdr.Contents)
{
// You would get hold of the inner memory stream here
Stream stream = ctnt.ReadAsStreamAsync().Result;
stream.Position = 0;
var type1 = ctnt.GetType();
if (ctnt.Headers.ContentDisposition.Name == "\"myFile\"")
{
var filename = ctnt.Headers.ContentDisposition.FileName;
var a = filename.Replace("\"", "");
CloudBlockBlob blob = imagesContainer.GetBlockBlobReference(a);
if (ctnt.Headers.ContentType != null)
{
// Set appropriate content type for your uploaded file
blob.Metadata["FileType"] = "Text";
blob.Properties.ContentType = ctnt.Headers.ContentType.MediaType;
}
// this.FileData.Add(new MultipartFileData(ctnt.Headers, blob.Name));
StreamReader rdr = new StreamReader(stream);
var a1 = rdr;
blob.UploadFromStream(stream);
}
else
{
string aaa = ctnt.Headers.ContentDisposition.ToString();
StreamReader rdr = new StreamReader(stream);
Container = rdr.ReadLine();
string z = Container;
imagesContainer = blobClient.GetContainerReference(z);
imagesContainer.CreateIfNotExists();
string msg = imagesContainer.Name;
sendMessage(msg);
}
}
});
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
public void sendMessage(String containerName)
{
// getting connection string from App setting
var connString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
// creating service Bus
var namespaceManager = NamespaceManager.CreateFromConnectionString(connString);
if (!namespaceManager.QueueExists("testqueue"))
{
namespaceManager.CreateQueue("testqueue");
}
// creating service bus client
QueueClient clnt = QueueClient.CreateFromConnectionString(connString, "TestQueue");
string msg = containerName;
BrokeredMessage message = new BrokeredMessage(msg);
clnt.Send(message);
I am successful in creating the container, uploading text files and passing the container name to the service bus queue.
The issue is as follows;
- From the client side since two data is passed (container, i.e. user-id and its corresponding 5text files), the else part is executed 5times.
- Because of that 5 messages (same container) are sent to the service bus queue.
- I only need to send one message to queue i.e the container name.
Any help will be kindly appreciated. I am new to Azure.