Dynamics CRM - 查询取消并接收部分结果

Dynamics CRM - query cancellation and receive partial results

我已经在 WPF 应用程序中准备了以下代码。此代码仅查询 CRM 联系人列表并将其放入集合中,然后显示在列表框控件中。

Xaml:

<Window x:Class="WPFDynamics365.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFDynamics365"
    xmlns:fa="http://schemas.fontawesome.io/icons/"
    mc:Ignorable="d"
    Title="MainWindow" Height="600" Width="800">

<Window.Resources>
    <Storyboard x:Key="WaitStoryboard">
        <DoubleAnimation
    Storyboard.TargetName="Wait"
    Storyboard.TargetProperty="(TextBlock.RenderTransform).(RotateTransform.Angle)"
    From="0"
    To="360"
    Duration="0:0:2"
    RepeatBehavior="Forever" />
    </Storyboard>
</Window.Resources>



<Grid Name="mainGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="2*"></RowDefinition>
        <RowDefinition Height="6*"></RowDefinition>
        <RowDefinition Height="2*"></RowDefinition>
    </Grid.RowDefinitions>

    <!-- fa to jest TextBlock -->
    <fa:FontAwesome
        Panel.ZIndex="999" Icon="Spinner" Name="Wait" 
        Grid.Column="0" Grid.Row="1"
        HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" 
        RenderTransformOrigin="0.5, 0.5" Margin="20" Width="100">
        <TextBlock.RenderTransform>
            <RotateTransform Angle="0" />
        </TextBlock.RenderTransform>
    </fa:FontAwesome>

    <Label Margin="0,20,0,0" FontSize="20" HorizontalAlignment="Center" Width="100" Grid.Row="0" Name="count"></Label>

    <ListBox 
        DisplayMemberPath="FullName" SelectedValuePath="Id" Name="contactList" 
        Grid.Column="0" Grid.Row="1" Panel.ZIndex="0"
        Width="300" Height="300" HorizontalAlignment="Center">

    </ListBox>

    <Button Grid.Column="0" Grid.Row="2" 
        Width="200" Height="50" Margin="0,10,0,0" 
        Name="cancel" Content="Stop downoloading Contacts">

    </Button>

</Grid>

代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += async (sender, args) =>
        {
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            cancel.AddHandler(Button.ClickEvent, new RoutedEventHandler((s, a) => 
            {
                tokenSource.Cancel();
            }));
            var context = App.CRM.Context;
            CrmFactor factor = CrmFactor.Create();

            ((Storyboard)FindResource("WaitStoryboard")).Begin();
            EntitiesExplorer exp = new EntitiesExplorer(factor);

            var contacts = await exp.GetContacts(tokenSource.Token);
            count.Content = contacts?.Count.ToString();
            contactList.ItemsSource = contacts;
            ((Storyboard)FindResource("WaitStoryboard")).Stop();
            Wait.Visibility = Visibility.Collapsed;
        };


    }
}

下载联系人的过程可以被用户中断。整个联系人下载是使用带有取消令牌的 TPL 完成的。 Getter:

public async Task<List<Contact>> GetContacts(CancellationToken ct)
    {
        List<Contact> list = new List<Contact>();
        return await System.Threading.Tasks.Task.Run(() =>
        {

            try
            {
                list = _crmFactor.Context.ContactSet.AsParallel().WithCancellation(ct).ToList();
            }
            catch { }

            return list;
        });
    }

在操作可能中断后,我收到一个空列表。目前还可以,但我很好奇是否有可能取消操作并获取部分联系人列表,而不仅仅是空列表或完整列表。

您可以将查询结果分页,页面大小较小(我想是 1-3),您将对系统造成重创,但可以实现您想要的结果。

我所说的锤击是什么意思:如果您有 100 条记录,您可以正常一次查询所有记录(1 个查询 -> 100 个结果:取消会使您空着)。如果页面大小为 2,您将查询 CRM 50 次(50x2 = 100 个结果。取消后您将获得在 运行 期间完成的许多查询的结果)。

如果你实施这个,观察网络,你可能会 DoS-ing 自己...