1

I do have a C# ASP.NET Web API on .NET Framework app running perfectly fine on local machine. However, when I deploy this to Azure Web App Service, and when attempting to access one of the endpoints, I am getting an error:

The error message as received in the Application Log Stream What I am not understanding is where the folder \api\print in the error coming from? My endpoint is doing two things:

  1. Create a PDF file. This is working fine since I can see the created file in the wwwroot\app_data folder
  2. Upload the created PDF file to Google Drive. This is where the code is failing. I am suspecting because the app can not open the file.

Below is the code for the actions above:

    public GoogleDrivePDFFile CreateTicket()
    {
        //directory for created files as per stakeoverflow question https://stackoverflow.com/questions/1268738.
        string tickets_path = HostingEnvironment.MapPath("~/App_Data/");

        //current date and time 
        var current_date_time = DateTime.UtcNow;

        string google_file_name = string.Concat(
             current_date_time.Year, 
             current_date_time.Month, 
             current_date_time.Day, 
             current_date_time.Hour, 
             current_date_time.Minute, 
             current_date_time.Second,
            "_", "XXXX",
            ".pdf"
           ); //the filename to use for the new created file
        
        //full path for the new file in the filesytem
        string filName = Path.Combine(tickets_path, google_file_name); 

        //file is being created okay in the filesystem. I can see it in Azure app file system.
        PdfFormatProvider provider = new PdfFormatProvider();
        using (Stream output = File.Open(filName, FileMode.CreateNew))
        {
           provider.Export(document, output);
        }

        GoogleUploads googleUploads = new GoogleUploads();

        //It is failing here.....
        var returned_file = googleUploads.UploadTicket(google_file_name, filName, requiredDocument);

        /*
         * I have tested the endpoint with the below and this works fine.
         * 
            var working_example = new GoogleDrivePDFFile();
            working_example.DocumentId = "Document ID .... okay";
            working_example.DownloadLink = "Download Link .... okay";
            working_example.WebViewLink = "Web View Link .... okay";

            return working_example;
        */

        return returned_file;
    }

I am not sure what am I doing wrong.

Hakuna N
  • 183
  • 2
  • 17
  • Is this ASP.NET on .NET Framework (i.e. `System.Web.*) or ASP.NET Core? (`Microsoft.AspNetCore.*`? (Also, you're using `DateTime.UtcNow` incorrectly: you need to cache `UtcNow` into a local because subsequent calls to `UtcNow` will return differing values). – Dai Aug 02 '22 at 22:31
  • `HttpContext.Current` <-- **never use `HttpContext.Current`**: it means your ASP.NET code won't be async-safe (or even thread-safe). Instead always pass `HttpContext` as a parameter from controller or page code. – Dai Aug 02 '22 at 22:32
  • Also, you don't need `HttpContext.Current` just to use `MapPath`. Instead use `HostingEnvironment.MapPath`. – Dai Aug 02 '22 at 22:33
  • @Dai this is ASP.NET on .NET Framework. – Hakuna N Aug 02 '22 at 22:33
  • Have you looked at your logs? – Flydog57 Aug 02 '22 at 22:47
  • @Dai I changed the DateTime.UtcNow. I understand you. I also changed from `HttpContext.Current` to `HostingEnvironment.MapPath` and issue still persist. – Hakuna N Aug 02 '22 at 22:54
  • @Flydog57 The screenshot attached on the question is the error log saved as html coming from the `Monitoring` -> `Log stream` -> `Application logs`. I am wondering if maybe there is any area I can see more advanced errors to guide me on the actual issue. – Hakuna N Aug 02 '22 at 22:59
  • 1
    You are making a HTTP connection to the server and probably have limited access to the server file system. Recommend putting files on a network drive that users have access. By default an HTTP connection has GUEST privilege with limited access to the server resources. You could run service As Admin but that opens the door to hackers get access to the server. It is wrong to say "attempting to access one of the endpoints" because that implies the failure occur while attempting to make the connection. You are making the connection but failing while processing the request at the server. – jdweng Aug 03 '22 at 01:21
  • Hello, I have encountered the same problem, could you tell me how to solve it? – YurongDai Dec 07 '22 at 02:17
  • @YurongDai I posted my answer. My security key file was not in Azure Web App folder. I uploaded my security key files and my web app functioned properly. – Hakuna N Dec 07 '22 at 17:36

1 Answers1

1

In my case, my Google Security Keys file was not uploaded to Azure Web App File System. This is the reason why my code was not working. I upload the keys file and my app worked.

Hakuna N
  • 183
  • 2
  • 17