2015-06-15 140 views
2

我正在使用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 

任何一個可以幫我解決這個問題。

寫操作下面的代碼:

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

你能告訴我們你的StreamWriter的代碼,哪裏出錯? – Marco

+0

我已添加代碼 –

+0

在你的問題請使用編輯按鈕 – Marco

回答

7

你的堆棧跟蹤指向與StreamWriter的錯誤。根據你的代碼,有如果的StreamWriter只有一個occurence:

catch (Exception ex) 
{ 
    System.IO.File.WriteAllText(@"D:\exception.txt", ex.Message); 
} 

這意味着,發生異常時,你的catch塊要此異常登錄到本地驅動器。這會導致另一個異常,因爲可能沒有驅動器D. 從邏輯上講,在解決此異常之後,將會有另一個異常,您的catch塊將正確記錄。

如果驅動器D存在(並且它不是cd驅動器)並且您的IIS用戶帳戶具有適當的權限,請檢查您的測試機器。

在旁註:我不會以這種方式捕捉異常。這只是污染了帶有錯誤消息的文本文件,沒有堆棧跟蹤,時間戳等。 您可以從已有的許多Logging Frameworks中抽取一個(例如NLOG),並讓您的生活方式更輕鬆。

+0

你是對的,這是我的錯誤,catch中的代碼只是爲了檢查條帶返回的東西,保存在文件中,我想刪除代碼但我錯過了,是的,我希望這裏有例外,非常感謝。 –

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文件夾「)。

相關問題