Wednesday 22 May 2019

Operation returned an invalid status code 'Forbidden'

Hi

Today I was getting the below error while getting secret value.

Operation returned an invalid status code 'Forbidden'

After some search found that I have added the Web APP to Access Policies but never clicked on Save. Just make sure you have added the application in the Access Policies.

Navigate to Keyvault in Azure --> Open the required Keyvault --> Access Policies and Configure the Policy as below.

Configure form Template : Key, Secret and Certificate Management
Select Principal : Select the Application to which you want to provide access.
Key, Secret and Certificate Permissions : Based on your requirement.

Hope this helps.

--
Happy Coding
Gopinath

Logging in Azure Web APP

Hi,

Today I was developing an Web APP and it was giving an error after I host it on Azure. To understand the error I have log the traces. Here is the way that explains that how we can do that.

We can directly use Trace class (System.Diagnostics.Trace) for write the logs. I have written the below piece of the code as a first line in the method.


System.Diagnostics.Trace.WriteLine("I am getting called...");

Now navigate to your Azure Account --> The app service where you have deployed the Web APP --> Diagnostics Logs.


You have something called "Application Logging (Filesystem)" and you can switch it on. There are four options available there.

Error - Error, Critical
Warning - Warning, Error, Critical
Information - Info, Warning, Error, Critical
Verbose - Trace, Debug, Info, Warning, Error, Critical (all categories)

I normally prefer Verbose as it gives everything.

Now, browse the URL for hitting the action where you have written the tracing.

Navigate to Advance Tools under you AppService and Click on Go


It opens a new tab as below - Click on Debug console and then CMD.


You will see the below screen, click on LogFiles --> Application, you will see the .txt file. Click on Edit icon to see the logs.


Note : For Application logging, you can turn on the file system option temporarily for debugging purposes. This option turns off automatically in 12 hours

--
Happy Coding
Gopinath

Monday 20 May 2019

Multiple actions were found that match the request

Hi,

I had written a new HTTPGet method and it worked well. After sometime, I got a requirement to write one more new HTTPGet method and I had troubles. The code started failing by throwing the below error.

"Multiple actions were found that match the request"

After some search I was able to identify the issue as we have to specify which action(method) we need send your HTTP request. For fixing it, we have to add routes in the WebApiConfig in the below order. The order plays important rule here. 

        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
            name: "ControllerAndActionOnly",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { },
            constraints: new { action = @"^[a-zA-Z]+([\s][a-zA-Z]+)*$" });

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

        }

Hope this helps.

--
Happy Coding
Gopinath

The return type of an async method must be void, Task or Task

Hi,

Today I was working on the some work related to KeyVault and I have written async method. The code was error as below. 

"The return type of an async method must be void, Task or Task<T>"



I tried internet and not able to get any answer for it and I was so worried :( as I was not able to understand the issue. It was in the early morning with half sleep I tried this and I thought, this needs some deep understanding and went for a coffee.

After sometime, I just opened the laptop and understood that I didn't the System.Threaing.Tasks namespace in the class file and it fixed the issue.

Moral - You need to take a small break when things are not working and start again if something is failing. Coffee helps :)

Hope this helps.

--
Happy Coding
Gopinath