sdk-dotnet icon indicating copy to clipboard operation
sdk-dotnet copied to clipboard

Issue and Proposed Fix for : the type initializer for 'AuthorizeNet.Util.HttpUtility' error

Open schandan-ssactivewear opened this issue 2 years ago • 13 comments

Hi Team,

We are one of the heavy users of this framework for our retail application . We see this below issue intermittently 3 or 4 times a month which blocks the users from checking out . The only way to fix it is restarting the app pool. Until the app pool is restarted out checkout screen breaks image

Analysis :

To replicate the issue in lower environment we started hitting the service from our app while the app is getting recycled or redeployed. (in sandbox)

The HTTPUtility is the entry point class for any API calls that goes to Authorize and it has this Static Properties which invokes the AuthorizeNet.Envvironment.getBooleanProperty()

image

The issue seems to be coming from this class AuthorizeNet. Environment.GetProperty() (https://github.com/AuthorizeNet/sdk-dotnet/blob/master/Authorize.NET/Environment.cs) When the app is getting recycled or deployed , the ConfigurationManager.AppSettings native object is in the process of getting populated with appsettings propery from Web.config file , Since it is a NameValue collection , (which is not ThreadSafe )if any other process tried to read the ConfigurationManager.AppSettings.AllKeys , the below exception is thrown . And the Problem with Static attributes is that if gets corrupted due to an error , the only way to fix that is by getting app pool restarted

image

image

Proposed Fix:

Please let us know if there is a way to get around this issue . If not this is what we tried and it seems if you make this change in the Environment.cs File it might work We tested by bombarding requests the below snippet of code when the app is getting recycled , an error gets thrown when the “ConfigurationManager.AppSettings.AllKeys” gets invoked. Microsoft documentation says it’s a NamedValue collection by design (which is not thread safe). Hence when other process is trying to access it when it is written it throws an error

image

We tried this snippet and bombarded with similar load and it seems to do the trick as it doesn’t access AllKeys variable and right after we added the snippet that is similar to Environment.CS file and it fails

image

Conclusion :

Changing the way ConfigurationManager.AppSettings is read might fix the issue (By not accessing AllKeys) Or Please let us know if there is a different way to handle this issue

schandan-ssactivewear avatar Feb 03 '22 15:02 schandan-ssactivewear

This fix has been incorporated, along with a more standard mutex lock, in v2.0.3.

@schandan-ssactivewear : Kindly validate the fix and close this issue if you are satisfied.

gnongsie avatar Apr 07 '22 08:04 gnongsie

Thanks so much @gnongsie . will check and let you know.

schandan-ssactivewear avatar Apr 07 '22 13:04 schandan-ssactivewear

@gnongsie I am facing same issue , I am using AuthorizeNet version 2.0.3

The type initializer for 'AuthorizeNet.Util.HttpUtility' threw an exception.

dstelangre avatar Jun 09 '22 06:06 dstelangre

we haven't seen this problem since last 3 months after we started using AuthorizeNet version 2.0.3. Your cause of the issue seems different ,can you please share the entire stack trace.

schandan-ssactivewear avatar Jun 09 '22 10:06 schandan-ssactivewear

Inner exception:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. File name: 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' at AuthorizeNet.Environment.GetProperty(String propertyName) at AuthorizeNet.Environment.getBooleanProperty(String propertyName) at AuthorizeNet.Util.HttpUtility..cctor()

Stack trace :

at AuthorizeNet.Util.HttpUtility.GetPostUrl(Environment env) at AuthorizeNet.Util.HttpUtility.PostData[TQ,TS](Environment env, TQ request) at AuthorizeNet.Api.Controllers.Bases.ApiOperationBase`2.Execute(Environment environment)

dstelangre avatar Jun 09 '22 10:06 dstelangre

I might need more -are you seeing "Index was outside the bounds of Array" ?

image

schandan-ssactivewear avatar Jun 09 '22 10:06 schandan-ssactivewear

I am not able to see that

In class HttpUtility , GetPostUrl method at below line throwing error HttpUtility.Logger.debug(string.Format("Creating PostRequest Url: '{0}'", (object) postUrl));

dstelangre avatar Jun 09 '22 11:06 dstelangre

This seems a different issue- Does it break during the first time load ? or It loads fine the first time and breaks after few transactions goes thru ?

schandan-ssactivewear avatar Jun 09 '22 13:06 schandan-ssactivewear

I am not able to do single transaction due to this error , I am using NuGet package AuthorizeNet version 2.0.3

dstelangre avatar Jun 09 '22 13:06 dstelangre

Ok , then it is a different issue . The issue that was fixed in 2.0.3 to handle "AuthorizeNet.Util.HttpUtility" error when the application recycles while it is running. If your app is not working for the first time load then it should be some proxy or connectivity issue from the client side .

schandan-ssactivewear avatar Jun 09 '22 14:06 schandan-ssactivewear

We can eliminate this issue by installing System.Net.Http.Formatting.Extension package from the NuGet package manager. I hope it solves the issue.

extinctsion avatar Oct 31 '23 05:10 extinctsion

Sorry, it didn’t work. Maybe the cause of your error wasn’t due to thread conflict from multiple instances initializing at the same time.

schandan-ssactivewear avatar Oct 31 '23 10:10 schandan-ssactivewear

I am seeing the same issue with version 2.0.3 .NET 7.0

The type initializer for 'AuthorizeNet.Util.HttpUtility' threw an exception. Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.

at AuthorizeNet.Environment.GetProperty(String propertyName) at AuthorizeNet.Environment.getBooleanProperty(String propertyName) at AuthorizeNet.Util.HttpUtility..cctor()

delviscovom avatar Nov 04 '23 16:11 delviscovom