.NET 8项目集成Authorize.Net SDK调用controller.Execute()触发HttpUtility类型初始化异常
我在.NET 8项目中集成了AuthorizeNet 2.0.4版本的SDK,尝试处理支付时,调用controller.Execute()触发了以下异常:
System.TypeInitializationException: 'The type initializer for 'AuthorizeNet.Util.HttpUtility' threw an exception.'
我的核心代码如下:
public static ANetApiResponse Run(string ApiLoginID, string ApiTransactionKey, decimal amount) { System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; var apiLoginId = ""; var transactionKey = ""; Console.WriteLine("Authorize Credit Card Sample"); ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX; ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType() { name = ApiLoginID, ItemElementName = ItemChoiceType.transactionKey, Item = ApiTransactionKey, }; ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX; ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType() { name = ApiLoginID, ItemElementName = ItemChoiceType.transactionKey, Item = ApiTransactionKey, }; var creditCard = new creditCardType { cardNumber = "4111111111111111", expirationDate = "1035", cardCode = "123" }; var billingAddress = new customerAddressType { firstName = "John", lastName = "Doe", address = "123 My St", city = "OurTown", zip = "98004" }; var merchantAuth = new merchantAuthenticationType { name = apiLoginId, ItemElementName = ItemChoiceType.transactionKey, Item = transactionKey }; var paymentType = new paymentType { Item = creditCard }; var lineItems = new lineItemType[2]; lineItems[0] = new lineItemType { itemId = "1", name = "t-shirt", quantity = 2, unitPrice = new Decimal(15.00) }; lineItems[1] = new lineItemType { itemId = "2", name = "snowboard", quantity = 1, unitPrice = new Decimal(450.00) }; var transactionRequest = new transactionRequestType { transactionType = transactionTypeEnum.authCaptureTransaction.ToString(), amount = 50.00m }; var request = new getHostedPaymentPageRequest { merchantAuthentication = merchantAuth, transactionRequest = transactionRequest, hostedPaymentSettings = new settingType[] { new settingType { settingName = "hostedPaymentReturnUrl", settingValue = "https://yourwebsite.com/payment-success" }, new settingType { settingName = "hostedPaymentReturnOptions", settingValue = "{ \"showReceipt\": false }" } } }; var controller = new getHostedPaymentPageController(request); controller.Execute(); // 异常发生在此处 }
我已经尝试了以下方法但没有解决问题:
- 确保TLS 1.2已启用
- 将Authorize.Net SDK降级到2.0.2版本
- 清理并重建解决方案
问题根源分析
这个类型初始化异常的核心原因是旧版AuthorizeNet SDK(2.x系列)依赖于.NET Framework中的System.Web.HttpUtility类,而.NET 8作为跨平台的.NET版本,已经移除了System.Web相关组件,转而使用System.Net.WebUtility。当SDK尝试初始化AuthorizeNet.Util.HttpUtility时,无法找到依赖的System.Web类,因此抛出异常。
可行的解决方案
1. 升级到支持.NET 8的官方SDK版本
Authorize.Net官方已经推出了适用于.NET Core/.NET 5+的SDK包,即**AuthorizeNet.APICore**(注意不是旧的AuthorizeNet包)。你可以通过NuGet安装最新版本:
Install-Package AuthorizeNet.APICore
这个版本完全适配跨平台.NET环境,不再依赖System.Web组件,从根本上解决兼容性问题。
2. 修复旧版SDK的HttpUtility依赖(不推荐,仅临时应急)
如果暂时无法升级SDK,可以尝试手动替换SDK中的HttpUtility实现:
- 找到项目中引用的
AuthorizeNet.Util.HttpUtility类,将其中调用System.Web.HttpUtility的方法替换为System.Net.WebUtility对应的方法(比如UrlEncode/UrlDecode等)。 - 注意:这种方式需要修改SDK源码,维护成本较高,仅作为临时方案。
3. 清理冗余代码,优化初始化逻辑
你的代码中存在重复设置RunEnvironment和MerchantAuthentication的问题,虽然不是直接引发异常的原因,但清理后可以避免潜在的配置冲突:
// 只需要设置一次即可 ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX; ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType() { name = ApiLoginID, ItemElementName = ItemChoiceType.transactionKey, Item = ApiTransactionKey, };
4. 移除不必要的TLS设置
在.NET 8中,默认已经启用了TLS 1.2及以上版本的协议,因此不需要手动设置System.Net.ServicePointManager.SecurityProtocol,移除该行代码可以避免潜在的协议冲突。
备注:内容来源于stack exchange,提问作者Tareq Mahmud

