c#.NET WebServices Connection丢失了将数据传递给Windows Phone应用程序

时间:2015-06-04 14:07:06

标签: c# mysql web-services windows-phone-8

我们正在开发一个Windows Phone 8应用程序,它可以从.NET WebService获取数据。 我们必须从大记录列表中检查哪一个已被修改或删除,并将其传递回应用程序。 目前记录超过46000;我解释说:

我们的Windows手机应用会执行此调用:

    private const int HUGE_OPERATION_TIMEOUT_MINUTES = 10;

    public static Task<GetCustomersCompletedEventArgs> GetClientiAsyncTask(this GridwaySoapClient client,
    Guid AuthenticationToken, DateTime LastUpdate, 
    int i, string jsonEntityKeyList)
    {
        var tcs = new TaskCompletionSource<GetCustomersCompletedEventArgs>();

        client.Endpoint.Address = WsAddress();
        client.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, HUGE_OPERATION_TIMEOUT_MINUTES, 0);
        client.Endpoint.Binding.OpenTimeout = new TimeSpan(0, HUGE_OPERATION_TIMEOUT_MINUTES, 0);
        client.Endpoint.Binding.CloseTimeout = new TimeSpan(0, HUGE_OPERATION_TIMEOUT_MINUTES, 0);
        client.Endpoint.Binding.SendTimeout = new TimeSpan(0, HUGE_OPERATION_TIMEOUT_MINUTES, 0);           
        client.InnerChannel.OperationTimeout = new TimeSpan(0, HUGE_OPERATION_TIMEOUT_MINUTES, 0);
        client.GetCustomersCompleted += (s, e) => TransferCompletion<GetCustomersCompletedEventArgs>(tcs, e, () => e);

        try
        {
            client.OpenAsync();
            client.GetCustomersAsync(AuthenticationToken, LastUpdate, jsonEntityKeyList, string.Empty, i);
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return tcs.Task;
    }

GetCustomersAsync调用另一个方法,该方法调用WS传递ID列表;使用下面的脚本在数据库上检查这些ID。

这就是我们所做的:

  • 我们从app传递了一个id列表,它们是已经传递的记录的ID
  • db check,通过获取db上的所有记录ID,如果传递的ID仍在此列表中
  • WS传回一个新的id列表,这些id是从db

    中删除或编辑的id
    [WebMethod(BufferResponse = false)]
    public string GetCustomers(Guid AuthenticationToken, DateTime LastUpdate,
        string jsonEntityKeyList, ref string jsonDeleteKeyList, int chiamataNumero)
    {
        List<CUSTOMER> customers = new List<CUSTOMER>();
    
        if (GridwayAuthenticationToken.Exist(AuthenticationToken))
        {
    
            List<string> EntityKeyList = JsonConvert.DeserializeObject<List<string>>(jsonEntityKeyList);
            var crmCtx01 = new CrmGridwayAdapter().GetXrmServiceContext();
    
            // if it's the first time we don't ned to remove anything from app
            // 01/01/1900 is the defauld date used if there's no lastUpdate's date
            if (LastUpdate == null || LastUpdate == new DateTime(1900, 01, 01))
            {
    
    
                List<Account> clienti = new List<Account>();
                try
                {
                    clienti = crmCtx01.AccountSet.Skip(chiamataNumero * 500).Take(500).ToList();
                }
                catch
                {
                    return Serializer.Serialize(customers);
                }
                var clientiToPick = clienti.Where(x => !EntityKeyList.Contains(x.Id.ToString())).ToList();
    
                if (clientiToPick.Count == 0 && clienti.Count > 0)
                {
                    List<CUSTOMER> fake = new List<CUSTOMER>();
                    CUSTOMER fakeS = null;
                    fake.Add(fakeS);
                    return Serializer.Serialize(fake);
                }
    
                if (clientiToPick != null)
                {
                    customers = clientiToPick.Select(x => new CUSTOMER()
                    {
                        ID = (Guid)x.AccountId,
                        FULL_NAME = x.Name,
                        PHONE_NO = x.Telephone1,
                        PIVA_CF = x.new_piva,
                        MODIFIED_ON = (x.ModifiedOn == null ? CRMMinDate() : Convert.ToDateTime(x.ModifiedOn))
                    }).ToList();
                }
            }
            //if it's not the first time we sync there might be datas on app
            else
            {
                //we do this step only the first time
                if (chiamataNumero == 0 && EntityKeyList.Count() > 0)
                {
                    try
                    {
                // THIS is the step stopping connection
                        List<string> customersId = crmCtx01.AccountSet
                            .Select(x => x.AccountId.ToString()).ToList();
                        List<string> noMoreExistingRecords = EntityKeyList.Where(x => !customersId.Contains(x)).ToList();
                        List<string> editedRecords = crmCtx01.AccountSet.Where(x => x.ModifiedOn > LastUpdate).Select(x => x.AccountId.ToString()).ToList();
                        List<string> AddressCessatiIds = crmCtx01.AccountSet.Where(x => x.StateCode != 0).Select(x => x.AccountId.ToString()).ToList();
                        noMoreExistingRecords.AddRange(editedRecords);
                        noMoreExistingRecords.AddRange(AddressCessatiIds);
                        EntityKeyList.RemoveAll(x => editedRecords.Contains(x));
    
                        jsonDeleteKeyList = Serializer.Serialize(noMoreExistingRecords);
                    }
                    catch (Exception ex) { }
                }
    
    
                List<Account> clienti = new List<Account>();
                try
                {
                    clienti = crmCtx01.AccountSet.Where(x => x.ModifiedOn > LastUpdate).Skip(chiamataNumero * 500).Take(500).ToList();
                }
                catch
                {
                    return Serializer.Serialize(customers);
                } 
                var clientiToPick = clienti.Where(x => !EntityKeyList.Contains(x.Id.ToString())).ToList();
    
                if (clientiToPick.Count == 0 && clienti.Count > 0)
                {
                    List<CUSTOMER> fake = new List<CUSTOMER>();
                    CUSTOMER fakeS = null;
                    fake.Add(fakeS);
                    return Serializer.Serialize(fake);
                }
    
                if (clientiToPick != null)
                {
                    customers = clientiToPick.Select(x => new CUSTOMER()
                    {
                        ID = (Guid)x.AccountId,
                        FULL_NAME = x.Name,
                        PHONE_NO = x.Telephone1,
                        PIVA_CF = x.new_piva,
                        MODIFIED_ON = (x.ModifiedOn == null ? CRMMinDate() : Convert.ToDateTime(x.ModifiedOn))
                    }).ToList();
                }
            }
    
            return Serializer.Serialize(customers);
        }
        else
        {
            throw GridwayAuthenticationToken.AuthenticationExceptions.AuthenticationRequired();
        }
    }
    

所以:此代码不会仅执行我所说的电话,而且还会拨打电话,向应用提供尚未下载的记录。 我们遇到了同样的问题所以我们每次都要传递500条记录,我们解决了连接丢失的问题;它很慢但是有效。

我们无法为ids执行此操作,我们将其全部通过,我们无法检查超过46000条记录的列表是否包含500条而没有所有4600条记录。

任何人都可以提供帮助?我们不知道如何解决它

0 个答案:

没有答案