ProfK
ProfK

Reputation: 51063

Problem with null object reference in Url.Action in MVC3 project

I am trying to set up a mocking scenario for my payment processor on a web site. Normally, my site redirects to the processor site, where the user pays. The processor then redirects back to my site, and I wait for an immediate payment notification (IPN) from the processor. The processor then posts to my NotifyUrl, which routes to the Notify action on my payments controller (PayFastController). To mock, I redirect to a local action, which after a conformation click, spawns a thread to post the IPN, as if posted by the processor, and redirects back to my registration process.

My mock processor controller uses the following two methods to simulate the processor's response:

[HttpGet]
public RedirectResult Pay(string returnUrl, string notifyUrl, int paymentId)
{
    var waitThread = new Thread(Notify);
    waitThread.Start(new { paymentId, ipnDelay = 1000 });

    return new RedirectResult(returnUrl);
}

public void Notify(dynamic data)
{
    // Simulate a delay before PayFast 
    Thread.Sleep(1000);

    // Delegate URL determination to the model, vs. directly to the config.
    var notifyUrl = new PayFastPaymentModel().NotifyUrl;
    if (_payFastConfig.UseMock)
    {
        // Need an absoluate URL here just for the WebClient.
        notifyUrl = Url.Action("Notify", "PayFast", new {data.paymentId}, "http");
    }

    // Use a canned IPN message.
    Dictionary<string, string> dict = _payFastIntegration.GetMockIpn(data.paymentId);
    var values = dict.ToNameValueCollection();
    using (var wc = new WebClient())
    {
        // Just a reminder we are posting to Trocrates here, from PayFast.
        wc.UploadValues(notifyUrl, "POST", values);
    }
}

However, I get an 'Object reference not set to an instance of an object.' exception on the following line:

notifyUrl = Url.Action("Notify", "PayFast", new {data.paymentId}, "http");

data.paymentId has a valid value, e.g. 112, so I'm not passing any null references to the Url.Action method. I suspect I have lost some sort of context somewhere by calling Notify on a new thread. However, if I use just notifyUrl = Url.Action("Notify", "PayFast");, I avoid the exception, but I get a relative action URL, where I need the overload that takes a protocol parameter, as only that overload gives me the absolute URL that WebClient.UploadValues says it needs.

Upvotes: 2

Views: 2217

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

When you are inside the thread you no longer have access to the HttpContext and the Request property which the Url helper relies upon. So you should never use anything that relies on HttpContext inside threads.

You should pass all the information that's needed to the thread when calling it, like this:

waitThread.Start(new { 
    paymentId, 
    ipnDelay = 1000,
    notifyUrl = Url.Action("Notify", "PayFast", new { paymentId }, "http")
});

and then inside the thread callback:

var notifyUrl = new PayFastPaymentModel().NotifyUrl;
if (_payFastConfig.UseMock)
{
    // Need an absoluate URL here just for the WebClient.
    notifyUrl = data.notifyUrl;
}

Upvotes: 3

Related Questions