Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
schandan-ssactivewear opened this issue Feb 3, 2022 · 13 comments

Comments

@schandan-ssactivewear
Copy link

schandan-ssactivewear commented Feb 3, 2022

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 schandan-ssactivewear changed the title Issue and Proposed Fix for : the type initializer for 'AuthorizeNet.Util.HttpUtility' threw an exception Issue and Proposed Fix for : the type initializer for 'AuthorizeNet.Util.HttpUtility' error Feb 3, 2022
@gnongsie
Copy link
Contributor

gnongsie commented Apr 7, 2022

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
Copy link
Author

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

@dstelangre
Copy link

@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
Copy link
Author

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
Copy link

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
Copy link
Author

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

image

@dstelangre
Copy link

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
Copy link
Author

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
Copy link

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

@schandan-ssactivewear
Copy link
Author

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
Copy link

extinctsion commented Oct 31, 2023

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
Copy link
Author

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
Copy link

delviscovom commented Nov 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants