我正在使用Stripe支付网关进行电子商务交易。要沟通我必须使用webhook网址意味着我将提供一个网址,以便他们可以与我们联系。
我创建了一个控制器和Action [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 "";
}
答案 0 :(得分:10)
您的Stacktrace指向StreamWriter
的错误。根据您的代码,只有一个StreamWriter出现:
catch (Exception ex)
{
System.IO.File.WriteAllText(@"D:\exception.txt", ex.Message);
}
这意味着,发生异常并且您的catch块想要将此异常记录到本地驱动器。这会导致另一个异常,因为可能没有驱动器D. 从逻辑上讲,在你解决了这个异常后,会有另一个异常,你的catch块会正确记录。
检查您的测试机器,是否存在驱动器D(并且它不是CD驱动器)并且您的IIS用户帐户具有适当的权限。
旁注:我不会以这种方式捕捉异常。这只会污染带有错误消息的文本文件,没有堆栈跟踪,时间戳等。 您可以使用其中一个已经存在的Logging Frameworks(例如NLOG),让您的生活更轻松。
答案 1 :(得分:0)
“设备未就绪”错误似乎是根据http://referencesource.microsoft.com/#mscorlib/system/io/file.cs,56cd161c65ab07fe,references从Win32发出的。如果在确保给出权限后它仍然存在并且存在这样的驱动器则可能是一些硬件问题。在这种情况下,你可以在https://superuser.com/questions/364851/how-to-troubleshoot-the-device-is-not-ready-error-in-windows-xp检查,希望它有所帮助!
PS。是的,它不是保存日志(nlog,log4net或手动进入App_data
文件夹“)的好方法。