AuthorizeNet / sdk-dotnet

.Net SDK for Authorize.Net API
Other
149 stars 206 forks source link

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

Open schandan-ssactivewear opened 2 years ago

schandan-ssactivewear commented 2 years ago

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

gnongsie commented 2 years ago

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.

schandan-ssactivewear commented 2 years ago

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

dstelangre commented 2 years ago

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

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

schandan-ssactivewear commented 2 years ago

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.

dstelangre commented 2 years ago

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)

schandan-ssactivewear commented 2 years ago

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

image

dstelangre commented 2 years ago

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));

schandan-ssactivewear commented 2 years ago

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 ?

dstelangre commented 2 years ago

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

schandan-ssactivewear commented 2 years ago

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 .

extinctsion commented 1 year ago

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

schandan-ssactivewear commented 1 year ago

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.

delviscovom commented 1 year ago

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()