任务<列表<T>&GT;返回List.Count = 0但有116个元素

时间:2015-10-12 13:40:28

标签: c# wcf task windows-applications multitasking

我真的不知道问题所在。我是一个win8.1应用程序,它调用WCF方法获取一些数据。当我调试WCF时,它会在列表中返回Task<List<T>> List.Count=116个元素,但是当列表(任务)&#34;到达时#34;在我的应用程序中,它总是说List.Count=0

使用WCF-Testclient测试 - 结果: enter image description here

我在Win8应用程序中得到的结果:

enter image description here

呼叫:

private List<Person> getPersonList()
{
    IService client = CustomServiceBinding.GetClientForIService();
    try
    {
        if (client != null && ((IClientChannel)client).State == CommunicationState.Opened))
        {
             var personList = client.GetPersonStateList(terminalName);

             personList.Wait(Timeout.Infinite);

             if (personList.IsCompleted)
             {          
                 if (personList.Result == null || personList.Result.Count <= 0)
                 {                            
                      // i'm ALWAYS landing here because the List has no Elements
                      // but when the method in the WCF returns the List had 116 Elements
                      // where do they get lost?
                 }
                 else
                 {                            
                      foreach (var item in personList.Result)
                      {
                           loginTextbox.Text += "Yeah";
                      }
                 }
             }
        }
    }     
    catch (CommunicationObjectFaultedException cofe)
    {
         ExceptionHandling(cofe);

         if (client != null)
         {
              ((IClientChannel)client).Abort();
         }
    }
    catch (Exception ex)
    {
         ExceptionHandling(ex);

         if (client != null)
         {
              ((IClientChannel)client).Abort();
         }
    }
    finally
    {
         if (client != null)
         {
              ((IClientChannel)client).Close();
              ((IClientChannel)client).Dispose();
         }
    }
 }

WCF中的方法:

public async Task<List<Person>> GetPersonStateList(string terminal)
{
    Task<List<Person>> t = Task.Run(() =>
    {
        List<Person> personList = new List<Person>();
        try
        {                        
             //calling another WCF - returns 116 Elements
             //validatePerson always returns true (hardcoded for testing)    
             currentPersonList = ServiceBinding.getCurrentPersonList().GetPersonEnumeration().Where(x => validatePerson(x));
        }
        catch (Exception ex)
        {
             currentPersonList = null;
             ExceptionContainer myException = new ExceptionContainer("Message: " + ex.Message);
             throw new FaultException<ExceptionContainer>(myException);
        }


        if (currentPersonList != null && currentPersonList.Count() > 0)
        {                            
            foreach (var item in currentPersonList)
            {                                
                 TimeSpan ts;

                 var dailyTimes = item.GetDailyTimeEnumeration();

                 foreach (var day in dailyTimes)
                 {                                    
                       ts = new TimeSpan(0, 0, (int)day.RawValue);

                       personList.Add(new Person() { Name = item.firstname + " " + item.lastname, dailyTime = ts });
                 }
            }
        }
        else
        {               
              // i never get in here             
              personList = null;
        }

        // at this point the personList has 116 valid Elements
        return personList;
    });

    // this "t" has a List with 116 Elements at the return point
    return await t;
}

我在这里缺少什么?我忘记了什么吗?我是否必须锁定某些东西(如果是,我应该锁定什么以及在哪里?)?

** 已编辑 **

在WCF方法中添加了更多代码示例,并在WCF和Win8 App中获得了两个截图。

** 已编辑 **

现在,我已经在上面给出的方法中改变了一些编码方式。

第二个不会返回Task<List<Person>>而只返回List<Person>

缩短第一个方法,如下所示:

private async void updateUI(object sender, object e)
    {
        try
        {
            List<Person> list = new List<Person>();
            TM2K_Service client = IAGServiceBinding.GetClientForTM2K();
            try
            {
                if (client != null)
                {
                    string terminalName = getTerminalName().ToLower();

                    // asynchronous
                    var personList = await client.GetPersonStateList(terminalName);


                    if (personList == null || personList.Count <= 0)
                    {
                        // BUT I'M STILL STUCK HERE WITH A LIST WITH ZERO ELEMENTS (Count=0)
                    }
                    else
                    {
                         foreach (var item in personList.Result)
                         {
                             loginTextbox.Text += "Yeah";
                         }
                    }
                }
            }
            catch (Exception x)
            {
                ExceptionHandling(x);
            }                
        }
        catch (Exception ex)
        {                
            ExceptionHandling(ex);
        }
    }

当我调用IIS发布的WCF时,当我调用WCF-Testclient var personList Count=0时,我得到相同的结果。

** 编辑 **

我发现在集合或通用列表的反序列化方面存在问题。当您通过visual studio(单击)添加服务引用时,它会设置用于反序列化的默认集合类型。我正在使用ChannelFactory,所以我必须将其设置为KnownType(typeof)?但是我对WCF中的这个编码方法还是比较新的,或者在客户端上以编程方式设置服务引用 - 我如何以及在何处设置这些属性?我也尝试了已知类型IList<>ArrayObservableCollection。没有机会反序列化收到的数据。

我必须为反序列化设置如何以及使用哪些属性?

0 个答案:

没有答案