Need help implementing IHttpAsyncHandler on my methods to make an API call

278 views Asked by At

So I have a DNN module that is basically just a square box to drop files into which then get resized, renamed, saved locally, and uploaded to an Azure blob container. Now what I need to do is to make an API call (that I made myself) to get some info from a database based on the user's DNN ID. All methods I've found so far to make API calls need to be run asynchronously, so I need to change my interface that this class derives from, from IHttpHandler to IHttpHandlerAsync.

However finding help with IHttpHandlerAsync has proven quite difficult. I'm quite new to web development so please excuse any obvious mistakes or misunderstandings.

    public class FileUploader : IHttpAsyncHandler
{
    private async Task<int> GetDogAsync()
    {
        int currentUserDnnId = UserController.Instance.GetCurrentUserInfo().UserID;

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

        Dog serializedDog = new Dog
        {
            DogTableID = -1
        };
        using (var client = new HttpClient())
        {

            var content = await client.GetStringAsync("https://localhost:5566/api/dogs/getlatestdog/fromdnnid/" + currentUserDnnId);
            if (content != null)
            {
                serializedDog = (new JavaScriptSerializer().Deserialize<Dog>(content));
                return serializedDog.DogTableID;

            }
        }
        return serializedDog.DogTableID;

    }


    #region IHttpAsyncHandler Members



    private AsyncProcessorDelegate _Delegate;

    protected delegate void AsyncProcessorDelegate(HttpContext context);



    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
    {
        _Delegate = new AsyncProcessorDelegate(ProcessRequest);

        return _Delegate.BeginInvoke(context, cb, extraData);

    }



    public void EndProcessRequest(IAsyncResult result)

    {

        _Delegate.EndInvoke(result);

    }



    #endregion



    #region IHttpHandler Members



    public bool IsReusable

    {

        get { return true; }

    }



    public async void ProcessRequest(HttpContext context)

    {

        await UploadFile(context);

    }

    #endregion

public async Task UploadFile(HttpContext context)

{
  //file gets resized, renamed, saved and uploaded to Azure
  int serializedDogTableId = await GetDogAsync();

}

The UploadFile method initiates GetDogAsync() to store the returned info in a table.

I got a sample for IHttpAsyncHandler here: https://madskristensen.net/blog/how-to-use-the-ihttpasynchandler-in-aspnet/

But I'm not sure exactly how to implement it? As it is now I get a "System.NullReferenceException: 'Object reference not set to an instance of an object.'" exception when dragging in an image into the upload box. I wish I could tell you exactly where this exception is originating from but debugging is difficult because the module can only be tested on a locally hosted DNN website.

I'm sure I'm just not using the IHttpAsyncHandler interface correctly, so any pointers in the right direction would be greatly appreciated.

0

There are 0 answers