Dynamically determine if running on Azure or not? - azure

Working on an ASP.Net Core MVC app and wondering what the best way is to dynamically determine at run time if my code is running on Azure or not?
Thanks
Donal

See Kudu's Environment page:
https://sitename.scm.azurewebsites.net/Env.cshtml
Check for the existence of App Service specific environment variables:
WEBSITE_SKU
WEBSITE_SCM_SEPARATE_STATUS
WEBSITE_HOSTNAME = __sitename__.azurewebsites.net

Related

Set up Azure Application Insights for local environment

We have set up Application Insights for our Dev & Prod environments, not with the SDK but through the Portal. We're now in the process of installing the SDK so we can have more control over customizing logging, what to measure in performance, etc.
I have found how to separate the environments in code (separate Instrumentation Key in different config files etc), but I have found nothing when it comes to my local environment. Which instrumentation key should I use there, the dev one? Wouldn't then this skew our dev metrics everytime one of the developers runs the app locally?
And also, doesn't it make sense to have a separate App Service slot just for the local environment, so I can test everything and see the logs I'm trying out locally, and not have to deploy to dev everytime I want to see what I'm doing?
I've tried creating a separate slot for local, but it generates a weird url based on the name I give, which I can't change later.
I've googled for a couple of days already and couldn't find any (or very little) helpful advice when it comes to this.
I realise there is a "Just add the SDK to try local only mode" option in Visual Studio, but then I would have to use it exclusively locally. What I want is to use all three - my local, dev & prod.
We're using .net core 2.2 for our backend and Angular 7 for front end.
I'm an idiot.
The url set up automatically basically means nothing. I solved the problem by just adding another App Service slot (created from our App Service production one, just like dev), and added this key to the local settings in our project.
Now we can use this key to get real time results as we debug, and use the other two for dev & production.

Azure WebJob-Project - access parent project artifacts

I am new to Azure WebJobs. For a mobile app we need some WebJobs that can run scheduled. My question is about the "Projects-Architecture" when it comes to WebJobs.
As far as i know, a new project is created for every WebJob. I am doing this by righclicking the main project -> add new azure webjob project.
But how are these projects "combined". There should be some reference to the parent project that i cannot find. I need to access the entities from the main project inside the WebJob. How should this be done?
Furthermore i am not sure wheter to implement Logic+Data access directly inside the WebJob or let the WebJob instead call another Controller to handle this?
Documentation on this is horrible, every help would be great.
They are not really "combined". There is a reference in the Web App (the webjobs-list.json) in properties that tells VS to publish the webjob when you publish the web app. If you need to access entity models in the webjob, then you need to add a reference to your other projects just like you would in any other project (add the project reference and then add a using statement in the webjob). The reference will get compiled into the webjob.
May I ask what are you trying to achieve via the web job? this would help in identifying why the need for accessing the models of your MVC app.
One suggestion would be to push the object from the MVC app to an azure queue and then access it via your job. Set it up in a way to identify new messages in a queue. (trigger). - not sure if this helps cause I am not sure how quick the web job is run and what are the other constraints you are working with.
When you add a new or existing WebJob Project to your solution, Visual Studio will take care of this for you. It is not something new, as if you are adding a console/windows service project to your web/MVC application.
If you need these projects to communicate, you till need to add references to your other projects just like you would in any other project which will in turn get compiled.
If you need to learn more and check some examples, this tutorial https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-get-started/ shows how to write code for a simple multi-tier ASP.NET MVC 5 application that uses the WebJobs SDK. Maybe that can give you some information on how to architect the application.

Updating code of managed vm on google compute engine

I understand this might be an easy solution, but I am very new to this so any help would be appreciated.
I have been running through the hello world application for node.js with managed vms on google compute engine, and I have just done this stage
gcloud preview app deploy app.yaml --promote
Which has allowed me to put up the app, and it works.
BUT HOW do I now update that code? If I run that command again it starts up new instances and essentially treats it like a new upload.
You can deploy the updated version of your app by running the same command you used to deploy the app the first time, as indicate in this article:
If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources. For information about deleting or stopping your VM instances, see Cleaning up.
Just in case anyone found this question looking for the same information, I finally seemingly worked out how to do it.
You need to attach the --version flag when you are deploying, instead of using --promote.
You can find the default version in google cloud console, by going menu (burger icon) -> app engine -> versions and you will see in that list one item with (default) by it.
so then when deploying put that version string after --version and it will deploy without needlessly creating new things

How to reuse Azure startup tasks?

I have an app which needs to be installed for all the services I have. Unfortunately, installation requires many files and task related input parameters (e.g. RoleInstanceValue xpath=...).
The way I came up with to share this task among cloud service projects is through Azure Plugins (here is a link to a library of them http://richorama.github.io/AzurePluginLibrary/).
The problems I see with this approach are:
It is not recommended/supported by Microsoft, which means they can
change support for that any time.
It requires copying files to C:\Program Files\Microsoft
SDKs\Azure\.NET SDK\v2.6\bin\plugins\NxlogAzureForwarder, adding an
extra step to build setup.
Microsoft recommends startup tasks. But I could not figure out a way to share them among cloud services.
So, my question is: how to easily reuse startup tasks?
As recommended your best chance would be a startup task that starts your app, which would be encapsulated into a Windows service. That service is going to be your reusable task/app/service (whatever you call it).
What you need extra is the following:
Project reference to your Windows service
An install script for the service
Installer tool
(Optional) app config for your service
To start your app/service you need to update cloud service definition by adding start up task and making runtime context elevated, and start your service in OnStart entry point.
That being said, you can place parameters of your app in the cloud config and read them in your WebRole and pass them to your service.
For more detail check out this post.

Accessing Azure ServiceConfiguration settings in Node.js

I'm coming to the beta deployment phase of building a Node.js application running through iisnode on Windows Azure. Having set up multiple instances, as well as production/staging separation, my research has led me to the following conclusion:
If I want to change configuration settings on the fly without redeploying code, I need to use Service Configuration .cscfg files.
My problem is that I've stored various configuration settings in the <appSettings> and <iisnode> elements of web.config that I might want to change, which are currently exposed in my Node application via the process global object.
I've looked around MSDN, Node documentation and SO (the usual), and can't find if Node does or can expose Service Configuration settings in the same way. If not, is there a way I can expose them to my application?
Edit: To be more specific, I'm really only looking to move the node_env setting from web.config to Service Configuration, since I'd like to be able to switch from staging to production setup with just a config change. The reason - we're using IP switching to swap between staging and prod, and there are some very minor differences between the two (the URL of the RESTful web service it consumes, for example).
I also know I can configure node_env in an iisnode.yaml file read by iisnode, but that will still recycle the application, and I don't want to modify x yaml files and redeploy, where x is the number of instances of the production/staging application.
You can access the configuration settings via the Azure SDK for Node.js.
To install:
npm install azure
To get the configuration settings:
var azure = require('azure');
azure.RoleEnvironment.getConfigurationSettings(function(error, settings) {
if (!error) {
// You can get the value of setting "setting1" via settings['setting1']
}
});
These settings seem to be held in an XML file located in c:\Config\ folder of the Azure machine.
I'm certain that you're not supposed to access these directly, but you could easily parse this XML and read the settings when node starts.

Resources