dotnetcore / CAP

Distributed transaction solution in micro-service base on eventually consistency, also an eventbus with Outbox pattern
http://cap.dotnetcore.xyz
MIT License
6.71k stars 1.29k forks source link

Question about using CAP in the console application #111

Closed dlancerzz closed 6 years ago

dlancerzz commented 6 years ago

我在core控制台使用时遇到一个奇怪问题,数据能够写入数据库但是不能推送到消息队列(kafka),回调函数也没有执行。 获取Publisher代码:

            var services = new ServiceCollection();
            services.AddLogging();

            services.AddCap(x =>
            {
                x.UseSqlServer(ConnectionString);
                x.UseKafka(ServerList);
            });

            var provider = services.BuildServiceProvider();
            return provider.GetService<ICapPublisher>();

执行代码:

                //发布数据修改
                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    using (var tran = conn.BeginTransaction())
                    {
                        publisher.Publish(“mytopicname”, postData, tran,"SuccessCallBack");
                        tran.Commit();
                    }

                }

回调函数:

        [CapSubscribe("SuccessCallBack")]
        public void KafkaTestCallback(Person p)
        {
            Console.WriteLine("0");
        }
dlancerzz commented 6 years ago

然后我使用一个aspdotnet web站点几乎相同的代码,消息能够正确推送到消息队列,沉积的消息也同步推送了,那么新问题是,我怎样使用控制台来使cap能够正确运行呢?

yang-xiaodong commented 6 years ago

Have you startup CAP using IBootStrapper ? see #75

dlancerzz commented 6 years ago

谢谢@yuleyule66 thanks,进行如下修改我发送成功了

            var services = new ServiceCollection();
            services.AddTransient<ISubscriberService, Program>();
            services.AddSingleton<IApplicationLifetime, ApplicationLifetime>();//添加
            services.AddLogging();
            services.AddCap(x =>
            {
                x.UseSqlServer(ConnectionString);
                x.UseKafka(ServerList);
            });
            var provider = services.BuildServiceProvider();
            provider.GetRequiredService<IBootstrapper>().BootstrapAsync();//添加
            return provider.GetService<ICapPublisher>();

,但是callback方法没有被调用。

chaiziqi commented 6 years ago

请问ApplicationLifetime 从哪里定义的?谢谢。 @yuleyule66 @dlancerzz

dlancerzz commented 6 years ago

@chaiziqi 在程序集 Microsoft.AspNetCore.Hosting.Abstractions,通过nuget下载就可以了。

chaiziqi commented 6 years ago

但我是在Microsoft.AspNetCore.Hosting.Abstractions中只有定义接口IApplicationLifetime,但是没有找到ApplicationLifetime 的实现 @dlancerzz

dlancerzz commented 6 years ago

ApplicationLifetime 通过安装Microsoft.AspNetCore.Hosting,在命名空间 Microsoft.AspNetCore.Hosting.Internal;