我使用Kentico CMS 8.0创建了一个Web服务我已将实例升级到8.2。当我尝试从我的Web服务调用方法时,我在screenshot中收到错误。
这是我的方法:
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string ProcessOrderGeneralDonation(Dictionary<string, object> request)
{
Dictionary<string, object> response;
ProcessDonationOrder(request, out response);
return JSONResponse(true, "Data Populated", response);
}
private void ProcessDonationOrder(Dictionary<string, object> request, out Dictionary<string, object> response, bool isChampionOfCare = false)
{
var dictionary = new Dictionary<string, object> { };
#region Customer
var customer = GetNewCustomerInfo(request);
var customerType = ValidationHelper.GetInteger(request["customer_type"], 0);
if (customerType == 2) //company
{
customer.CustomerCompany = ValidationHelper.GetString(request["customer_organization"], String.Empty);
}
CustomerInfoProvider.SetCustomerInfo(customer);
var country = CountryInfoProvider.GetCountryInfo(ValidationHelper.GetString(request["customer_country"], String.Empty).Replace(" ", ""));
//var state = StateInfoProvider.GetStateInfo(ValidationHelper.GetString(request["customer_province"], String.Empty));
// Create new address object
var customerAddress = GetNewCustomerAddressInfo(request, customer);
if (country != null)
customerAddress.AddressCountryID = country.CountryID;
// Create the address
AddressInfoProvider.SetAddressInfo(customerAddress);
customerAddress.SetValue("AddressPhone", ValidationHelper.GetString(request["customer_phone"], String.Empty));
customerAddress.SetValue("AddressStateOthers", ValidationHelper.GetString(request["customer_province"], String.Empty));
customerAddress.Update();
// Create order addresses from customer address
OrderAddressInfo orderBillingAddress = OrderAddressInfoProvider.CreateOrderAddressInfo(customerAddress);
OrderAddressInfo orderShippingAddress = OrderAddressInfoProvider.CreateOrderAddressInfo(customerAddress);
// Set the order addresses
OrderAddressInfoProvider.SetAddressInfo(orderBillingAddress);
OrderAddressInfoProvider.SetAddressInfo(orderShippingAddress);
#endregion
var guid = ValidationHelper.GetGuid(request["guid"], Guid.Empty);
string message;
#region Shopping Cart
//Get Product
var product = GetProductSKUInfo(guid, out message);
if (product == null)
{
response = null;
return;
}
//Get Shopping Cart
var cart = GetShoppingCart();
// Add item to cart object
var param = new ShoppingCartItemParameters(product.SKUID, 1)
{
ProductOptions = new List<ShoppingCartItemParameters>
{
new ShoppingCartItemParameters(ValidationHelper.GetInteger(request["designation_id"], 0), 1),
new ShoppingCartItemParameters(ValidationHelper.GetInteger(request["donation_amount_id"], 0), 1)
}
};
if (isChampionOfCare)
{
//Get Champions Of Care Option Id
var options = OptionCategoryInfoProvider.GetProductOptionCategories(product.SKUID, true, OptionCategoryTypeEnum.Text);
var opt = options.Tables[0].AsEnumerable().SingleOrDefault(x => x["CategoryName"].ToString().ToLower() == "coc");
if (opt != null)
{
var categoryId = ValidationHelper.GetInteger(opt["CategoryID"], 0);
var skuOpt = GetSKUOptions(product.SKUID, categoryId) as DataTable;
if (skuOpt != null)
{
var cocId = ValidationHelper.GetInteger(skuOpt.Rows[0]["SKUID"], 0);
var cocParam = new ShoppingCartItemParameters(cocId, 1)
{
Text = ValidationHelper.GetString(request["coc_text"], string.Empty)
};
param.ProductOptions.Add(cocParam);
}
}
}
ShoppingCartItemInfo cartItem = cart.SetShoppingCartItem(param);
cartItem.CartItemCustomData["other_amount"] = ValidationHelper.GetDouble(request["donation_other_amount"], 0);
cart.ShoppingCartCustomData["donation_type"] = "GEN";
// Save item to database
ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(cartItem);
cart.ShoppingCartBillingAddress = orderBillingAddress;
cart.ShoppingCartShippingAddress = orderShippingAddress;
cart.Customer = customer;
cart.Update();
ShoppingCartInfoProvider.SetOrder(cart);
dictionary.Add("order_id", cart.OrderId);
UpdateDonor(request, cart.OrderId);
if (EnablePurchase)
{
Receipt responseReceipt;
request.Add("product_sku", product.SKUID);
Purchase(request, cart, customer, customerAddress, out responseReceipt);
var paymentSuccessed = !responseReceipt.GetMessage().Contains("DECLINED");
if (paymentSuccessed)
{
//HandleOrderNotification(cart);
SendInvoiceEmail(cart.OrderId);
}
dictionary.Add("moneris_message", responseReceipt.GetMessage());
}
#endregion
response = dictionary;
}
答案 0 :(得分:0)
我认为问题在于JSONResponse()
不知道如何将Dictionary<string,object>
序列化为有效的JSON格式。
查看其中一些答案可帮助您解决此问题。他们中的大多数建议使用像JSON.NET这样的JSON库:
答案 1 :(得分:0)
我已经将JSON.NET添加到我的项目和JSONResponse()函数
private string JSONResponse(bool success, string message, object data = null)
{
var dictionary = new Dictionary<string, object> { { "success", success }, { "message", message }, { "data", data } };
return JsonConvert.SerializeObject(dictionary, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
}