Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将ServiceStack.GetAsync与ReactiveCommand(v6)一起使用_C#_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack_Async Await_Reactiveui - Fatal编程技术网 servicestack,async-await,reactiveui,C#,servicestack,Async Await,Reactiveui" /> servicestack,async-await,reactiveui,C#,servicestack,Async Await,Reactiveui" />

C# 如何将ServiceStack.GetAsync与ReactiveCommand(v6)一起使用

C# 如何将ServiceStack.GetAsync与ReactiveCommand(v6)一起使用,c#,servicestack,async-await,reactiveui,C#,servicestack,Async Await,Reactiveui,我正在尝试将ReactiveCommand与组合 从未调用x=>\u reactiveList.AddRange(x),测试res为空。我现在不知道如何将ServiceStack的任务GetAsync(IReturn requestDto)结果转换为反应式IObservable 解决了问题,但这不是一个选项。您在反应UI端做的一切都是正确的,ServiceStack出现了一些问题 好吧,有几件事可能是错的。一切看起来都应该运行,但你永远不知道 我确实知道,当直接处理任务和TestSchedule

我正在尝试将
ReactiveCommand
与组合

从未调用
x=>\u reactiveList.AddRange(x)
,测试
res
为空。我现在不知道如何将ServiceStack的
任务GetAsync(IReturn requestDto)
结果转换为反应式
IObservable


解决了问题,但这不是一个选项。

您在反应UI端做的一切都是正确的,ServiceStack出现了一些问题

好吧,有几件事可能是错的。一切看起来都应该运行,但你永远不知道

我确实知道,当直接处理任务和
TestScheduler
时,在ReactiveUI中通常会有一些奇怪的东西,所以在这方面有一些东西可以尝试

  • 您可以使用将
    任务
    转换为
    IObservable

  • 另一个想法是强制您的
    ServiceCommandTask()
    方法返回一个
    IObservable
    ,而不使用
    async/await
    。要做到这一点,您必须更改创建
    ReactiveCommand
    的方式,以使用
    ReactiveCommand.CreateAsyncObservable
    ,您只需在从ServiceStack调用返回的
    任务中使用
    ServiceCommandTask()


  • 任务
    转换为
    IObservable
    可能有效的原因是
    TestScheduler
    依赖虚拟时间等待事情发生。
    TestSchedluer
    无法与
    Task
    正确交互,因此当它向前“跳过”1秒时,在现实世界中实际上没有时间。如果ServiceStack调用没有立即返回,那么我认为情况会是这样,通过上述解决方案,该问题可能可以解决,也可能无法解决。

    我认为这是时间问题,但我不知道如何解决它。上的示例代码GitHub@Tomasito是的,我想你最大的问题是你是。这是一个集成测试,因为它使用ServiceStack依赖项而不是模拟它。因为您试图执行的操作需要实时执行,
    AdvanceByMs(1000)
    根本帮不上您的忙。相反,您必须等待实时,这意味着您必须使用
    Thread.Sleep(1000)
    Thread.Sleep(1000)
    仅在
    Main(string[]args)
    中工作,当在
    TestMethod1
    中时,它会给我
    ServiceStack.webserviceeexception:methodnotallowed
    异常。最后执行的方法是
    System.Reactive.ScheduledObserver
    1.Run(对象状态,动作
    1递归)
    。我所有的架构都错了吗?我的意思是从ReactiveCommand调用服务?@Tomasito我想说,当您进行单元测试时,从ReactiveCommand调用ServiceStack是不好的,因为此时您正试图同时测试ServiceStack和ReactiveCommand。我会将您的
    GetAsync
    方法移动到一个可以在单元测试中模拟(即用虚拟版本替换)的接口中,以便您测试的唯一功能是您的ReactiveCommand功能。如果您需要测试ServiceStack是否工作,那么您应该为此进行不同的单元测试。我需要至少一个测试来确保它们一起工作。是吗?
        public ReactiveCommand<IList<string>> ServiceReadCommand { get; protected set; }
    
        public ReactiveList<string> ReactiveList
        {
            get { return _reactiveList; }
            set { _reactiveList = this.RaiseAndSetIfChanged(ref _reactiveList, value); }
        }
    
        private ReactiveList<string> _reactiveList = new ReactiveList<string>();
    
        public TestViewModel(IScreen screen = null)
        {
            HostScreen = screen;
    
            ServiceReadCommand = ReactiveCommand.CreateAsyncTask(x => ServiceCommandTask(), RxApp.TaskpoolScheduler);
            ServiceReadCommand.ThrownExceptions.Subscribe(x => Console.WriteLine((object)x));
            ServiceReadCommand.Subscribe(x => _reactiveList.AddRange(x));
        }
    
        private async Task<IList<string>> ServiceCommandTask()
        {
            this.Log().Info("Service command task");
    
            var baseUri = "http://localhost:9010";
            var client = new JsonServiceClient(baseUri);
    
            // Works
            return await Observable.Return(new List<string> { "" });
    
            // Don't
            return await client.GetAsync(new TestRequest());
        }
    
        [TestMethod]
        public void TestMethod1()
        {
            IList<string> res = null;
            new TestScheduler().With(sched =>
            {
                var viewModel = new TestViewModel();
    
                viewModel.ServiceReadCommand.CanExecute(null).Should().BeTrue();
                viewModel.ServiceReadCommand.ExecuteAsync(null).Subscribe(x => res = x);
    
                sched.AdvanceByMs(1000);
    
                return viewModel.ReactiveList;
            });
    
            res.Should().NotBeEmpty();
        }
    
    viewModel.ServiceReadCommand.ExecuteAsync(null).Subscribe(x => res = x);
    //Thread.Sleep(1000);
    sched.AdvanceByMs(1000);
    
    using System.Reactive.Linq;
    // ...
    return await (client.GetAsync(new TestRequest()).ToObservable());