Example #1
0
        private static void Main(string[] args)
        {
            // 1. 引用 Interfaces 类库和 Service Fabric 类库
            // 2. 创建客户端,这里使用的是非安全的方式,在网络隔离的情况下,
            //    客户端和服务端可以使用非安全方式通信

            ICounterService counterService = ServiceProxy.Create <ICounterService>(
                new Uri("fabric:/CounterDemo/CounterStateless"));


            //FabricTransportSettings fabricTransportSettings = new FabricTransportSettings
            //{
            //    // 仅是设置单次的重试时间,如果设置为 2 秒,可以重现错误,抛出 System.TimeoutException
            //    OperationTimeout = TimeSpan.FromMinutes(5)
            //};

            //// 这样设置重试的次数,从而控制整体超时时间
            //// 部分内置错误会无限重试,参考 IExceptionHandler
            //// 如果遇到错误,会再重试 defaultMaxRetryCount 次
            //OperationRetrySettings operationRetrySettings = new OperationRetrySettings(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), 3);

            ////ServiceProxyFactory serviceProxyFactory = new ServiceProxyFactory(c =>
            ////    new FabricTransportServiceRemotingClientFactory(fabricTransportSettings), operationRetrySettings);

            //ServiceProxyFactory serviceProxyFactory = new ServiceProxyFactory(c =>
            //    new FabricTransportServiceRemotingClientFactory(fabricTransportSettings, null, null, new[] { new MyExceptionHandler() }), operationRetrySettings);

            //ICounterService counterService = serviceProxyFactory.CreateServiceProxy<ICounterService>(
            //    new Uri("fabric:/CounterDemo/CounterStateless"));

            counterService.ResetAsync().Wait();

            do
            {
                try
                {
                    Console.WriteLine(counterService.CountAsync().Result);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            } while (true);
        }
        private static void Main(string[] args)
        {
            // 1. 引用 Interfaces 类库和 Service Fabric 类库
            // 2. 创建客户端,这里使用的是非安全的方式,在网络隔离的情况下,
            //    客户端和服务端可以使用非安全方式通信

            string command = Console.ReadKey().KeyChar.ToString().ToLowerInvariant();

            Console.WriteLine();

            if (command == "0")
            {
                ICounterService counterService = ServiceProxy.Create <ICounterService>(
                    new Uri("fabric:/CounterDemo/CounterStateful"), ServicePartitionKeyFactory.Build(0));

                counterService.ResetAsync().Wait();

                ICounterService shService = ServiceProxy.Create <ICounterService>(
                    new Uri("fabric:/CounterDemo/CounterStateful"), ServicePartitionKeyFactory.Build("Shanghai"));

                ICounterService bjService = ServiceProxy.Create <ICounterService>(
                    new Uri("fabric:/CounterDemo/CounterStateful"), ServicePartitionKeyFactory.Build("Beijing"));

                shService.ResetAsync().Wait();
                bjService.ResetAsync().Wait();
            }

            if (command == "1")
            {
                ICounterService counterService = ServiceProxy.Create <ICounterService>(
                    new Uri("fabric:/CounterDemo/CounterStateful"), ServicePartitionKeyFactory.Build(0));

                do
                {
                    try
                    {
                        Console.WriteLine(counterService.CountAsync().Result);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                } while (true);
            }

            if (command == "2")
            {
                ICounterService shService = ServiceProxy.Create <ICounterService>(
                    new Uri("fabric:/CounterDemo/CounterStateful"), ServicePartitionKeyFactory.Build("Shanghai"));

                ICounterService bjService = ServiceProxy.Create <ICounterService>(
                    new Uri("fabric:/CounterDemo/CounterStateful"), ServicePartitionKeyFactory.Build("Beijing"));

                do
                {
                    try
                    {
                        Console.WriteLine(shService.CountAsync().Result);
                        Console.WriteLine(shService.CountAsync().Result);
                        Console.WriteLine(bjService.CountAsync().Result);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                } while (true);
            }
        }