Error on azure web app deployment - azure

I am new in azure application development. As per the requirements given to me, I had developed an application on azure and successfully deployed it on azure cloud.Blob storage was also being used in that application. Everything was working fine.
When I was deploying it on cloud then that time I was not very much aware about azure deployment so I had deployed it as a cloud service. It worked fine but the only issue was about slow loading at the very first time. Then after doing lot of research I found some solutions so I have deployed it as web app then the slow loading problem was resolved. But with web app deployment, I am facing another problem with a single page which is using blob storage. Below is the error I am getting when opening that specific page :
Below is the code which I had written :
public List<string> ListContainer()
{
List<string> blobs = new List<string>();
if (!Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable) return null;
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("FileStorageAccount"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();
foreach (CloudBlobContainer item in containers)
{
foreach (IListBlobItem blob in item.ListBlobs())
{
blobs.Add(string.Format("{0}", blob.Uri));
}
}
return blobs;
}
It is working fine when running on visual studio but on deployment If I am going with cloud service deployment then I am not getting this specific error. Other pages are working fine with web deployment. The page which causing an error uses blob storage.
I had done lot of research but no luck. Please help!!!

I suspect you have not defined the "FileStorageAccount" Setting in the CSCFG file. Try using the CloudConfigurationManager.GetSetting method. It will read configuration values from the web.config or service configuration file. See also https://stackoverflow.com/a/19643516/5382426.

Related

Application insights not generated for .Net core app deployed on service fabric Linux cluster

I have a .Net core application that is deployed on service fabric Linux cluster. Application insights are configured in the app.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions aiOptions
= new ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
{
EnableAdaptiveSampling = false,
EnableQuickPulseMetricStream = false,
InstrumentationKey = "xxx"
};
services.AddApplicationInsightsTelemetry(aiOptions);
I have a controller class that has some action methods and logs the information.
[HttpPost]
public ActionResult actionMethod(...)
{
TraceLine("------------------------------------");
//some code
}
private static void TraceLine(string msg)
{
msg = $">> {DateTime.UtcNow.ToString("o")}: {msg}";
Log.Information(msg);
}
I am using Serilog, configured in appsettings.json & Program.cs
When I hit action method directly from local (without hosting it on even local sf cluster), via Postman, I see app insights getting generated and pushed to azure.
azure app insights snapshot
But when I hit the action method that is deployed on Azure service fabric I don't see any insight getting generated.
What am I missing here?
Any help is much appreciated!
Well, we need to check a few things here:
1) The app insights URL and the instrumentation key in the deployment parameter files for cluster hosted on cloud (Cloud.xml)
2) After checking the Cloud.xml, the best way is to access the log files and check what is the actual problem.
There's a description here which explains how to discover where the log files are stored.
You can use RDP to access the machine, which is explained here.
I was able to solve the issue by using Microsoft.ApplicationInsights.ServiceFabric.Native SDK in my application to log app insights.
Refer .NetCore section in ApplicationInsights-ServiceFabric on how to configure insights for service fabric application.

Console application Logs to store in Azure storage

I am using a console application that runs on on-premise servers triggered by a task scheduler. This console application performs the various actions and needed to be logged these. It would generate logs of around 200kb per run and the console app runs every hour.
Since the server is not accessible to us, I am planning to store the logs to Azure. I read about blob/table storage.
I would like to know what is the best strategy to store the logs in Azure.
Thank you.
Though you can write logging data in Azure Storage (both Blobs and Tables), it would actually make more sense if you use Azure Application Insights for logging this data.
I recently did the same for a console application I built. I found it incredibly simple.
I created an App Insight Resource in my Azure Subscription and got the instrumentation key. I then installed App Insights SDK and referenced appropriate namespaces in my project.
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
This is how I initialized the telemetry client:
var appSettingsReader = new AppSettingsReader();
var appInsightsInstrumentationKey = (string)appSettingsReader.GetValue("AppInsights.InstrumentationKey", typeof(string));
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = appInsightsInstrumentationKey;
telemetryClient = new TelemetryClient(configuration);
telemetryClient.InstrumentationKey = appInsightsInstrumentationKey;
For logging trace data, I simply did the following:
TraceTelemetry telemetry = new TraceTelemetry(message, SeverityLevel.Verbose);
telemetryClient.TrackTrace(telemetry);
For logging error data, I simply did the following:
catch (Exception excep)
{
var message = string.Format("Error. {0}", excep.Message);
ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(excep);
telemetryClient.TrackException(exceptionTelemetry);
telemetryClient.Flush();
Task.Delay(5000).Wait();//Wait for 5 seconds before terminating the application
}
Just keep one thing in mind though: Make sure you wait for some time (5 seconds is good enough) to flush the data before terminating the application.
If you're still keen on writing logs to Azure Storage, depending on the logging library you're using you will find suitable adapters that will write directly into Azure Storage.
For example, there's an NLog target for Azure Tables: https://github.com/harouny/NLog.Extensions.AzureTableStorage (though this project is not actively maintained).

Do I need an Azure Storage Account to run a WebJob?

So I'm fairly new to working with Azure and there are some things I can't quite wrap my head around. One of them being the Azure Storage Account.
My web jobs keeps stopping with the following error "Unhandled Exception: System.InvalidOperationException: The account credentials for '[account_name]' are incorrect." Understanding the error however is not the problem, at least that's what I think. The problem lies in understanding why I need an Azure Storage Account to overcome it.
Please read on as I try to take you through the steps taken thus far. Hopefuly the real question will become more clear to you.
In my efforts to deploy a WebJob on Azure we have created the following resources so far:
App Service Plan
App Service
SQL server
SQL database
I'm using the following code snippet to prevent my web job from exiting:
JobHostConfiguration config = new JobHostConfiguration();
config.DashboardConnectionString = null;
new JobHost(config).RunAndBlock();
To my understanding from other sources the Dashboard connection string is optional but the AzureWebJobsStorage connection string is required.
I tried setting the required connection string in portal using the configuration found here.
DefaultEndpointsProtocol=[http|https];AccountName=myAccountName;AccountKey=myAccountKey
Looking further I found this answer that clearly states where I would get the values needed, namely an/my missing Azure Storage Account.
So now for the actualy question: Why do I need an Azure Storage Account when I seemingly have all the resources I need place for the WebJob to run? What does it do? Is it a billing thing, cause I thought we had that defined in the App Service Plan. I've tried reading up on Azure Storage Accounts over here but I need a bit more help understanding how it relates to everything.
From the docs:
An Azure storage account provides resources for storing queue and blob data in the cloud.
It's also used by the WebJobs SDK to store logging data for the dashboard.
Refer to the getting started guide and documentation for further information
The answer to your question is "No", it is not mandatory to use Azure Storage when you are trying to setup and run a Azure web job.
If you are using JobHost or JobHostConfiguration then there is indeed a dependency for Storage accounts.
Sample code snippet is give below.
class Program
{
static void Main()
{
Functions.ExecuteTask();
}
}
public class Functions
{
[NoAutomaticTrigger]
public static void ExecuteTask()
{
// Execute your task here
}
}
The answer is no, you don't. You can have a WebJob run without being tied to an Azure Storage Account. Like Murray mentioned, your WebJob dashboard does use a storage account to log data but that's completely independent.

web application in stage can't connect to azure cloud, working fine in development environment

While working on one of my project, I am uploading documents from our content server(as we are using documentum) to azure cloud. As we want to save some space on our content server. I have written below code to upload my zipped files on azure cloud. I development environment it is working fine but in stage environment it is throwing timeout errors, it shows
"An unknown failure occurred : Connection timed out: connect"
Also when I try to download the file(it works fine in development environment), but in stage it shows "An error occurred while enumerating the result". It doesn't throw any exception nor any error is there.
My code is :
if (Boolean.parseBoolean(azureCloudUseDefaultContainer)) {
container = client.getContainerReference(azureCloudDefaultContainer);
}else {
container = client.getContainerReference(DEFAULT_CONTAINER);
}
container.createIfNotExists();
CloudBlockBlob blob = container.getBlockBlobReference(assetName);
BlobRequestOptions blobRequestOptions = new BlobRequestOptions();
blobRequestOptions.setTimeoutIntervalInMs(10000);
blobRequestOptions.setRetryPolicyFactory(new RetryLinearRetry(3000,3));
//blob.upload(new FileInputStream(file), file.length());
// If blob already exist on cloud it means asset was uploaded in past, so no need to upload it again
// to avoid duplicate blobs on cloud
if(!blob.exists()){
blob.upload(new FileInputStream(file), file.length(), null, blobRequestOptions, null);
}
Is this any configuration issue or network problem. what is your opinion
If you are using SSL certificate (it is not clearly stated in your question so it is only my assumption) then you must also configure it for staging slot. Have you seen Azure documentation about configuring deployment slots? Some settings are not swapped when using slotsin App Service.
Settings that are swapped:
General settings - such as framework version, 32/64-bit, Web sockets
App settings (can be configured to stick to a slot)
Connection strings (can be configured to stick to a slot)
Handler mappings
Monitoring and diagnostic settings
WebJobs content
Settings that are not swapped:
Publishing endpoints
Custom Domain Names
SSL certificates and bindings
Scale settings
WebJobs schedulers

Using Azure Blobs with Azure Website

I'm in the process of making an MVC Windows Azure website which involves users uploading images.
I wanted to store the images in blobs.
I searched for tutorials but most of them deal with Webapps rather than MVC websites.
The only useful tutorial I found was: http://www.codeproject.com/Articles/490178/How-to-Use-Azure-Blob-Storage-with-Azure-Web-Sites
I'm new to the whole MVC/Windows Azure/Visual Studio scene and I got confused at Step 7 where I had to connect to Cloud Storage Account.
var storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
I can't figure out where I'm supposed to put that bit of a code.
Same for the code in Step 8: Creating a container
blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("productimages");
if (container.CreateIfNotExist())
{
// configure container for public access
var permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
}
And Step 9: Save the Image to a BLOB
string uniqueBlobName = string.Format("productimages/image_{0}{1}",
Guid.NewGuid().ToString(), Path.GetExtension(image.FileName));
CloudBlockBlob blob = blobStorage.GetBlockBlobReference(uniqueBlobName);
blob.Properties.ContentType = image.ContentType;
blob.UploadFromStream(image.InputStream);
Any help will be really appreciated. Any other links to relevant tutorials are also welcome.
Thanks a lot!
I assume you've added the Windows Azure Storage NuGet package to your Web App project? That didn't seem explicit in the reference you quoted, but if you hadn't you'd be seeing a bunch of unresolved compiler errors.
In terms of where the code goes, it's rather up to you. The assignment to storageAccount is like setting a database connection string; nothing is going across the wire, so you could set it once perhaps and make it available throughout your application as part of some static class instance.
In terms of creating a container, you'd do this whenever you want to store data - consider it tantamount to creating a folder in a file system or maybe a table in a relational database. If your application is always storing information in the same "hard-coded" container, you could set your container up ahead of time in the portal and not create in code.
Putting code like this in though provides a bit of resiliency in case the container got deleted through some external process; the same way that an app using SQL Server could check for the existence of an Employee table before doing updates/retrieves, since it's possible (but not likely) someone deleted the Employee table via some other SQL tool.
Saving the image to the blob would be put directly behind whatever controller is responsible for initiating that action. This is where you're doing the 'real' work. The code in your sample specifically references a container productimages, so it could be immediately preceded by the code in Step 8 if you wanted to be certain that there no chance the container was deleted (through the portal, for instance). Again, that would be similar to checking for the existence of a database table in traditional client/server code everytime you accessed data in it (which most of us might consider overkill and we'd resort to exception handling to cover that contingency).
Old question but still may help noobs!
Azure Webapps do accept MVC apps. Webapps on Azure are just a container which Im sure can take different types of ASP.NET based applications, Forms or MVC, C# or VB.net.
Assuming you know the basics of an MVC application you can just create the Azure Webapp and publish it to there. It's as straight forward as that.
This code:
var storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
And this:
blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("productimages");
if (container.CreateIfNotExist())
{
// configure container for public access
var permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
}
Can go in a new class specifically for Blob services e.g. call it something like blobServices.cs and save it in the root of your MVC app and it can look like so:
var storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("productimages");
if (container.CreateIfNotExist())
{
// configure container for public access
var permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
}
Which is probably the best practice. From there on in the Controller/s of your MVC app you can do something like this:
namespace blabla.cs
{
public controller myBlobController
{
//instantiate the blob service
BlobProfileImageServices _blobServices = new BlobProfileImageServices();
public public ActionResult Index()
{
//call your blob storage service
//and loop through each blob
CloudBlobContainer blobContainer = _blobServices.GetCloudBlobContainer();
List<string> blobs = new List<string>();
foreach (var blobItem in blobContainer.ListBlobs())
{
blobs.Add(blobItem.Uri.ToString());
}
}
}
}

Resources