我正在使用 Stripe 支付网关进行电子商务交易。为了进行通信,我必须使用 webhook url 意味着我将提供一个 url,以便他们可以与我们通信。 我创建了一个控制器和动作,它是
[AllowAnonymus]
。当我在本地运行应用程序并在浏览器上键入控制器和操作时,它就会执行该操作。但是当我在测试服务器上部署并执行相同操作时,会出现以下错误
“/”应用程序中的服务器错误。 设备尚未准备好。
堆栈跟踪:
[IOException: The device is not ready.
]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +14840940
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1430
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +211
System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) +210
System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost) +87
SchoolManagement.DTO.Entities.OrderItem.SavePartialCharge(String invoiceData) in c:\Atlassian\Bamboo-home\xml-data\build-dir\SMS-PPINBOX-JOB1\SchoolDTO\Entities\OrderItem.cs:185
SchoolWeb.Controllers.StripeWebhookController.GetStripeResponse() in c:\Atlassian\Bamboo-home\xml-data\build-dir\SMS-PPINBOX-JOB1\SchoolWeb\Controllers\StripeWebhookController.cs:24
lambda_method(Closure , ControllerBase , Object[] ) +79
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +270
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +120
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +452
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +33
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
谁能帮我解决这个问题。
Action中编写的代码如下:
public class StripeWebhookController : BaseController
{
//
// GET: /StripeWebhook/
[AllowAnonymous]
public JsonResult GetStripeResponse()
{
Stream req = Request.InputStream;
req.Seek(0, System.IO.SeekOrigin.Begin);
string json = new StreamReader(req).ReadToEnd();
DTO.OrderItem.SavePartialCharge(json);
return Json("", JsonRequestBehavior.AllowGet);
}
}
添加方法 SavePartialCharge
public static String SavePartialCharge(string invoiceData)
{
try
{
string stripeEventType = BL.PaymentGateway.StripeReturn.GetStripeType(invoiceData);
var obj = JObject.Parse(invoiceData);
var dataObj = obj.SelectToken("data.object");
var subscriptionID = dataObj.SelectToken("subscription").ToString();
var customerID = dataObj.SelectToken("customer").ToString();
var chargeID = dataObj.SelectToken("charge").ToString();
var amount = Convert.ToDecimal(!dataObj.SelectToken("subtotal").ToString().IsNullOrEmpty() ?dataObj.SelectToken("subtotal").ToString() : "0");
using (var db = new SchoolEntities())
{
switch (stripeEventType)
{
case "invoice.payment_failed":
var resultorderFailed =
db.OrderItems.Where(oi => oi.MerchantServiceSubscriptionID == subscriptionID)
.Select(oi => new {oi.ID, oi.Order.CreditCard4Digits})
.FirstOrDefault();
if (resultorderFailed != null)
OrderItemInstallment.AddOrderItemInstallment(db, resultorderFailed.ID, amount,
resultorderFailed.CreditCard4Digits, chargeID, false);
return "";
case "invoice.payment_succeeded":
var resultOrderSucceed =
db.OrderItems.Where(oi => oi.MerchantServiceSubscriptionID == subscriptionID)
.Select(oi => new {oi.ID, oi.Order.CreditCard4Digits, oi.TotalInstallments})
.FirstOrDefault();
if (resultOrderSucceed != null)
{
OrderItemInstallment.AddOrderItemInstallment(db, resultOrderSucceed.ID, amount,
resultOrderSucceed.CreditCard4Digits, chargeID, true);
var count =
db.OrderItemInstallments.Count(
oii => oii.OrderItemID == resultOrderSucceed.ID && oii.Success);
if (count == resultOrderSucceed.TotalInstallments)
BL.PaymentGateway.StripePaymentGateway.CancelSubscription(customerID,
subscriptionID);
}
return "";
}
}
}
catch (Exception ex)
{
System.IO.File.WriteAllText(@"D:\exception.txt", ex.Message);
}
return "";
}
您的堆栈跟踪指向一个带有
StreamWriter
的错误。根据您的代码,StreamWriter只出现一次:
catch (Exception ex)
{
System.IO.File.WriteAllText(@"D:\exception.txt", ex.Message);
}
这意味着,发生了异常,并且您的 catch 块想要将此异常记录到本地驱动器。这会导致另一个异常,因为可能没有驱动器 D。 从逻辑上讲,解决此异常后,将会出现另一个异常,您的 catch 块将正确记录该异常。
检查您的测试机器,是否存在驱动器 D(并且不是 CD 驱动器)并且您的 IIS 用户帐户是否具有适当的权限。
旁注:我不会用这种方式捕获异常。这只会用错误消息污染文本文件,而没有堆栈跟踪、时间戳等。 您可以利用现有的众多日志框架之一(例如 NLOG),让您的生活变得更轻松。
在我的情况下,当我将无效的无效参数名称从 .net 代码传递到存储过程时,就会出现此问题。
在我的存储过程中,我将输入参数声明为“caseId”,但在代码中我调用输入参数为“case_Id”(带下划线)的存储过程。
“设备未就绪”错误似乎来自 Win32,根据 http://referencesource.microsoft.com/#mscorlib/system/io/file.cs,56cd161c65ab07fe,references。如果在确保授予权限并且存在此类驱动器后仍然存在,则可能是某些硬件问题。在这种情况下,您可以检查https://superuser.com/questions/364851/how-to-troubleshoot-the-device-is-not-ready-error-in-windows-xp
PS。是的,这不是保存日志的好方法(nlog、log4net 或手动保存到
App_data
文件夹中”)。