public async static void GetMerchantRequest()
{
DataTable dtListofTransaction = new DataTable();
string sconnection = ConfigurationManager.AppSettings[ConfigurationManager.AppSettings["BUToProcess"].ToString()].ToString();
string query = "select query from interface.MES where TP = 0 and RS = 'A' and TyT = 'IE'";
using (SqlConnection sqlConn = new SqlConnection(sconnection))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(query, sqlConn))
{
dtListofTransaction.Load(cmd.ExecuteReader());
}
}
if (dtListofTransaction.Rows.Count > 0)
{
var distinctListOfFamilies = (from row in dtListofTransaction.AsEnumerable()
select row.Field<Guid>("PS_GUID")).Distinct().ToList();
int countOfFamilies = distinctListOfFamilies.Count();
int threadsToUse = Convert.ToInt32(ConfigurationManager.AppSettings["ThreadsToUse"]);
int FamiliesToBeProcessedByThread = (countOfFamilies > 50 ? (countOfFamilies / threadsToUse + 1) : countOfFamilies);
var listOfSubscriberLists = splitList<Guid>(distinctListOfFamilies, FamiliesToBeProcessedByThread);
if (countOfFamilies > 0)
{
foreach (var list in listOfSubscriberLists)
{
Task<int>[] tasks = new Task<int>[listOfSubscriberLists.Count()];
int taskIndex = 0;
Task taskResult = null;
try
{
foreach (var listOfSubscribersToProcess in listOfSubscriberLists)
{
tasks[taskIndex] = SendTransactionsToMerChantAsync(listOfSubscribersToProcess, dtListofTransaction);
taskIndex++;
}
await (taskResult = Task.WhenAll(tasks));
Console.WriteLine("Finished both methods.\n ");
}
catch (Exception ex)
{
Console.WriteLine("handled {0}", ex.Message);
foreach (var innerEx in taskResult.Exception.InnerExceptions)
{
Console.WriteLine("inner exception {0}", innerEx.Message);
}
}
}
}
}
}
public static Task<int> SendTransactionsToMerChantAsync(List<Guid> listOfSubscribersToProcess, DataTable dtListofTransaction)
{
return Task.Run<int>(() =>
{
foreach (var guid in listOfSubscribersToProcess)
{
var dbMembersToProcess = dtListofTransaction.AsEnumerable().Where(p => object.Equals(p["PS_GUID"], guid) && p["oxml"] != null);
foreach (var member in dbMembersToProcess)
{
SendTransactionsToMerchant(member["oxml"].ToString(), member["TyT"].ToString(), member["MES_ID_PKID"].ToString(), Convert.ToInt32(member["Error_Counter"].ToString()));
}
}
return 1;
});
}
public static int SendTransactionsToMerchant(string sMerchantRequest, string sTransactionType, string sPKID = "", int ErrorCounter = 0)
{
switch (sTransactionType)
{
case TransactionType.INITIALENROLLMENT:
try
{
CMICSMerchantPortTypeClient svcICSClientProxy = ClientMerchantProxy<CMICSMerchantPortTypeClient>();
CMICSMerchant ICSRequest = Deserialize<CMICSMerchant>(sMerchantRequest);
ICSRequest.dateTimeTagFormat = null;
Console.WriteLine("Executed Thread.. " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Before: " + sPKID);
svcICSClientProxy.CMICSMerchant(ICSRequest);
Console.WriteLine("After: " + sPKID);
}
catch (System.ServiceModel.CommunicationException svcExcpt)
{
createException<System.ServiceModel.FaultException<MerchantWebSvcCMICSMerchant.Fault>, MerchantWebSvcCMICSMerchant.Fault>(svcExcpt, sPKID, ErrorCounter);
}
break;
case TransactionType.UPDATESUBINFO:
try
{
CMUpdateSubDepInfoWWPortTypeClient srvcUpdateSubInfoClientProxy = ClientMerchantProxy<CMUpdateSubDepInfoWWPortTypeClient>();
CMUpdateSubDepInfoWW updateSubInfoRequest = Deserialize<CMUpdateSubDepInfoWW>(sMerchantRequest);
updateSubInfoRequest.dateTimeTagFormat = null;
srvcUpdateSubInfoClientProxy.CMUpdateSubDepInfoWW(updateSubInfoRequest);
}
catch (System.ServiceModel.CommunicationException svcExcpt)
{
createException<System.ServiceModel.FaultException<MerchantWebSvcCMUpdateSubDepMerchant.Fault>, MerchantWebSvcCMUpdateSubDepMerchant.Fault>(svcExcpt, sPKID, ErrorCounter);
}
break;
default:
break;
}
return UpdateProcessedTransaction(sPKID, true, ErrorCounter, "");
}
public static int UpdateProcessedTransaction(string MemberPkid, bool flag, int errorCounter, string MerchantException)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["GOV"].ToString()))
{
conn.Open();
using (SqlCommand sc = new SqlCommand())
{
sc.Connection = conn;
if (flag)
{
sc.CommandText = "Update interface.MES set TP = 1,Date_Time_Modified='" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff") + "' where MES_ID_PKID =" + "'" + MemberPkid + "'";
}
else if (errorCounter.Equals(ConfigurationManager.AppSettings["ErrorCounter"].ToString()))
{
sc.CommandText = "Update interface.MES set TP = 1, Merchant_Response=" + "'" + @MerchantException.Replace(" \'", " ").Replace("\' ", " ") + "'" + ",Error_Counter =" + "'" + errorCounter + "',Date_Time_Modified = '" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff") + "'" + " where MES_ID_PKID =" + "'" + MemberPkid + "'";
}
else
{
sc.CommandText = "Update interface.MES set Merchant_Response=" + "'" + @MerchantException.Replace(" \'", " ").Replace("\' ", " ") + "'" + ", Error_Counter=" + "'" + errorCounter + "'" + ",Date_Time_Modified = '" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff") + "'" + " where MES_ID_PKID =" + "'" + MemberPkid + "'";
}
sc.CommandType = System.Data.CommandType.Text;
return sc.ExecuteNonQuery();
}
}
}
GetMerchantRequest从程序中调用,此方法的目的是基于查询获取一些数据,然后从结果集中通过调用外部服务来处理每个成员,然后在服务返回响应后,使用UpdateProcessedTransaction方法更新数据库表
但是,当我调试时,它正在遍历记录并调用SendTransactionsToMerChantAsync。但是,此后程序因错误终止
程序“ [12400] ServiceV2.exe”已退出,代码为0(0x0)。
我不怎么想。.Task。WhenAll是我认为的罪魁祸首...但是,请您检查一下代码,让我知道我做错了什么。
我还更新了SendTransactionsToMerChantAsync,使其仅使用Console.WriteLine(),但仍然退出并出现代码0错误
public static Task<int> SendTransactionsToMerChantAsync(List<Guid> listOfSubscribersToProcess, DataTable dtListofTransaction)
{
return Task.Run<int>(() =>
{
foreach (var guid in listOfSubscribersToProcess)
{
Console.WriteLine("Executed Thread.. " + Thread.CurrentThread.ManagedThreadId);
}
return 1;
});
}
谢谢,感谢您的帮助。