Beispiel #1
0
        public void Sample_Level100_BasicUsage_ConsumeMethod()
        {
            using (var service = ServiceConsumerFactory.Create <IFakeService>(() => new FakeServiceClient()))
            {
                IOperationResult <int> result = service.Consume(operation => operation.AddIntegers(1, 1));

                if (result.HasNoException)
                {
                    int actual = result.Value;
                    Console.WriteLine("Actual: " + actual);
                    Assert.AreEqual(2, actual);
                }
                else
                {
                    // if (result.HasFaultExceptionOfType<MyBusinessLogicException>())
                    if (result.HasFaultException)
                    {
                        Console.WriteLine("Service operation threw a fault: " + result.Exception.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Technical error occurred while calling the service operation: " + result.Exception.ToString());
                    }

                    Assert.Fail("Service operation call threw an exception");
                }
            }
        }
Beispiel #2
0
        public void Sample3_Level300_AutomaticRetry_StaticServiceConsumerRetryPolicy_UsingMicrosoftTransientFaultHandling_WithConsumeMethod()
        {
            var microsoftRetryStrategy = new Incremental(5, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
            var microsoftRetryPolicy   = new RetryPolicy <SoapFaultWebServiceTransientErrorDetectionStrategy>(microsoftRetryStrategy);

            ServiceConsumerFactory.DefaultRetryPolicy = microsoftRetryPolicy.ForServiceConsumer(); // extension method
            // or ServiceConsumerFactory.DefaultRetryPolicy = new ChannelAdam.TransientFaultHandling.RetryPolicyAdapter(microsoftRetryPolicy);
            // or ServiceConsumerFactory.DefaultRetryPolicy = (ChannelAdam.TransientFaultHandling.IRetryPolicyFunction)microsoftRetryPolicy;

            using (var service = ServiceConsumerFactory.Create <IFakeService>(() => new FakeServiceClient()))
            {
                var result = service.Consume(operation => operation.AddIntegers(1, 1));

                if (result.HasNoException)
                {
                    Console.WriteLine("Actual: " + result.Value);
                    Assert.AreEqual(2, result.Value);
                }
                else
                {
                    if (result.HasFaultException)
                    {
                        Console.WriteLine("Service operation threw a fault: " + result.Exception.ToString());
                    }
                    else if (result.HasException)
                    {
                        Console.WriteLine("Technical error occurred while calling the service operation: " + result.Exception.ToString());
                    }

                    Assert.Fail("Service operation was not successfully called");
                }
            }
        }
Beispiel #3
0
        public void Sample_Level100_BasicUsage_OperationsProperty()
        {
            //using (var service = ServiceConsumerFactory.Create<IFakeService>("BasicHttpBinding_IFakeService"))
            using (var service = ServiceConsumerFactory.Create <IFakeService>(() => new FakeServiceClient()))
            {
                try
                {
                    int actual = service.Operations.AddIntegers(1, 1);
                    Console.WriteLine("Actual: " + actual);
                    Assert.AreEqual(2, actual);

                    return;
                }
                // catch (FaultException<MyBusinessLogicType> fe)
                catch (FaultException fe)
                {
                    Console.WriteLine("Service operation threw a fault: " + fe.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Technical error occurred while calling the service operation: " + ex.ToString());
                }

                Assert.Fail("Service operation was not successfully called");
            }
        }
Beispiel #4
0
        public static void Main(string[] args)
        {
            // 服务特定依赖注册点
            Container.RegisterSingleton <SteamBotCoordinatorCallback>();
            Container.RegisterSingleton(() => ServiceConsumerFactory.Create <ISteamBotCoordinator>(
                                            () =>
                                            new SteamBotCoordinatorClient(
                                                new InstanceContext(Container.GetInstance <SteamBotCoordinatorCallback>()))
            {
                ClientCredentials =
                {
                    UserName     =
                    {
                        UserName = "******",
                        Password = "******"
                    }
                }
            }, new RetryPolicyAdapter(Container.GetInstance <RetryPolicy>()),
                                            new NullServiceConsumerExceptionBehaviourStrategy()));

            Lifestyle.Transient.CreateRegistration <BotCookieManager>(Container)
            .SuppressDisposableTransientComponentWarning();
            Lifestyle.Transient.CreateRegistration <BotInstance>(Container).SuppressDisposableTransientComponentWarning();

            KeylolService.Run <SteamBot>(args, Container);
        }
Beispiel #5
0
        public void Sample3_Level300_AutomaticRetry_ServiceConsumerRetryPolicy_UsingMicrosoftTransientFaultHandling_UsingOperationsProperty()
        {
            var microsoftRetryStrategy = new Incremental(5, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
            var microsoftRetryPolicy   = new RetryPolicy <SoapFaultWebServiceTransientErrorDetectionStrategy>(microsoftRetryStrategy);

            var serviceConsumerRetryPolicy = microsoftRetryPolicy.ForServiceConsumer(); // extension method

            // or var serviceConsumerRetryPolicy = new ChannelAdam.TransientFaultHandling.RetryPolicyAdapter(microsoftRetryPolicy);
            // or var serviceConsumerRetryPolicy = (ChannelAdam.TransientFaultHandling.IRetryPolicyFunction)microsoftRetryPolicy;
            using (var service = ServiceConsumerFactory.Create <IFakeService>(() => new FakeServiceClient(), serviceConsumerRetryPolicy))
            {
                try
                {
                    int actual = service.Operations.AddIntegers(1, 1);

                    Console.WriteLine("Actual: " + actual);
                    Assert.AreEqual(2, actual);

                    return;
                }
                catch (FaultException fe)
                {
                    Console.WriteLine("Service operation threw a fault: " + fe.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Technical error occurred while calling the service operation: " + ex.ToString());
                }

                Assert.Fail("Service operation was not successfully called");
            }
        }
Beispiel #6
0
        public void Sample_Level200_Instance_ExceptionBehaviourStrategy()
        {
            // Apply the exception handling strategy only for this created service consumer instance
            using (var service = ServiceConsumerFactory.Create <IFakeService>(
                       () => new FakeServiceClient(),
                       new StandardOutServiceConsumerExceptionBehaviourStrategy()))
            {
                try
                {
                    int actual = service.Operations.AddIntegers(1, 1);

                    Console.WriteLine("Actual: " + actual);
                    Assert.AreEqual(2, actual);

                    return;
                }
                catch (FaultException fe)
                {
                    Console.WriteLine("Service operation threw a fault: " + fe.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Technical error occurred while calling the service operation: " + ex.ToString());
                }

                Assert.Fail("Service operation was not successfully called");
            }
        }
        /// <summary>
        /// Регистрация WCF сервисов
        /// </summary>
        private static void RegisterServices(IUnityContainer unityContainer)
        {
            var clientEndpoints = new ClientEndpoints();

            string fileConvertingEndpoint = clientEndpoints.GetEndpointByInterfaceFullPath(typeof(IFileConvertingClientService));

            unityContainer.RegisterFactory <IServiceConsumer <IFileConvertingClientService> >(unity =>
                                                                                              ServiceConsumerFactory.Create <IFileConvertingClientService>(fileConvertingEndpoint));

            string signatureEndpoint = clientEndpoints.GetEndpointByInterfaceFullPath(typeof(ISignatureClientService));

            unityContainer.RegisterFactory <IServiceConsumer <ISignatureClientService> >(unity =>
                                                                                         ServiceConsumerFactory.Create <ISignatureClientService>(signatureEndpoint));

            string serverStateEndpoint = clientEndpoints.GetEndpointByInterfaceFullPath(typeof(IServerStateClientService));

            unityContainer.RegisterFactory <IServiceConsumer <IServerStateClientService> >(unity =>
                                                                                           ServiceConsumerFactory.Create <IServerStateClientService>(serverStateEndpoint));

            string historyEndpoint = clientEndpoints.GetEndpointByInterfaceFullPath(typeof(IHistoryClientService));

            unityContainer.RegisterFactory <IServiceConsumer <IHistoryClientService> >(unity =>
                                                                                       ServiceConsumerFactory.Create <IHistoryClientService>(historyEndpoint));

            string likeEndpoint = clientEndpoints.GetEndpointByInterfaceFullPath(typeof(ILikeClientService));

            unityContainer.RegisterFactory <IServiceConsumer <ILikeClientService> >(unity =>
                                                                                    ServiceConsumerFactory.Create <ILikeClientService>(likeEndpoint));

            unityContainer.RegisterFactory <IWcfClientServicesFactory>(unity =>
                                                                       new WcfClientServicesFactory(() => unity.Resolve <IServiceConsumer <IFileConvertingClientService> >(),
                                                                                                    () => unity.Resolve <IServiceConsumer <ISignatureClientService> >(),
                                                                                                    () => unity.Resolve <IServiceConsumer <IServerStateClientService> >(),
                                                                                                    () => unity.Resolve <IServiceConsumer <IHistoryClientService> >()));
        }
Beispiel #8
0
        /// <inheritdoc />
        /// <exception cref="T:System.Exception">
        /// ServiceConsumerFactory.Create returned null/invalid IHmsCloudService reference.
        /// Check WCF HMS configuration.
        /// </exception>
        public void SendCasinoDataReport(IReportable reportable)
        {
            var dataReport = reportable as CasinoDataReport;

            if (null == dataReport)
            {
                return;
            }

            using (
                var cloudServiceProxy = ServiceConsumerFactory.Create <IHmsCloudService>(() => new HmsCloudServerProxy())
                )
            {
                try
                {
                    if (cloudServiceProxy?.Operations == null)
                    {
                        throw new Exception(
                                  "ServiceConsumerFactory.Create returned null/invalid IHmsCloudService reference. Check WCF HMS configuration.");
                    }

                    // The TransactionScope here will provide behavior such that - if/when the call to
                    // DataAggregator.SuccessfulCasinoDataReport fails for some reason (e.g., problem
                    // writing to the database) - the transactional WCF/MSMQ message delivery will be
                    // rolled back (i.e., the messages will be discarded from the client-side MQ)
                    var txnOptions = new TransactionOptions {
                        IsolationLevel = IsolationLevel.RepeatableRead
                    };
                    using (var txnScope = new TransactionScope(TransactionScopeOption.Required, txnOptions))
                    {
                        cloudServiceProxy.Operations.ReportCasinoData(dataReport);
                        DataAggregator.SuccessfulCasinoDataReport(reportable.ReportGuid);

                        txnScope.Complete();
                    }
                }
                catch (FaultException fe)
                {
                    Logger.Warn($"Service operation ReportCasinoData threw a fault: [{fe.Message}]");
                    DataAggregator.UnsuccessfulCasinoDataReport(reportable.ReportGuid);
                }
                catch (Exception ex)
                {
                    Logger.Warn(
                        $"An unexpected error occurred while calling the ReportCasinoData service operation: [{ex.Message}]");
                    var innerEx = ex.InnerException;
                    while (null != innerEx)
                    {
                        Logger.Warn($"[{innerEx.Message}]");
                        innerEx = innerEx.InnerException;
                    }

                    DataAggregator.UnsuccessfulCasinoDataReport(reportable.ReportGuid);
                    Logger.Warn($"Stack Trace: [{Environment.StackTrace}]");
                }
            }
        }
Beispiel #9
0
        public void Sample_Level100_BasicUsage_MultipleCalls()
        {
            using (var service = ServiceConsumerFactory.Create <IFakeService>(() => new FakeServiceClient()))
            {
                IOperationResult <int> result = service.Consume(operation => operation.AddIntegers(1, 1));
                AssertOperationResult(2, result);

                // Even if the channel had a communication exception and went into the fault state or was aborted,
                // you can still use the service consumer!

                result = service.Consume(operation => operation.AddIntegers(1, 3));
                AssertOperationResult(4, result);
            }
        }
Beispiel #10
0
        public static void Main(string[] args)
        {
            // 服务特定依赖注册点
            Container.RegisterSingleton(() => ServiceConsumerFactory.Create <IImageGarageCoordinator>(
                                            () => new ImageGarageCoordinatorClient
            {
                ClientCredentials =
                {
                    UserName     =
                    {
                        UserName = "******",
                        Password = "******"
                    }
                }
            }, new RetryPolicyAdapter(Container.GetInstance <RetryPolicy>()),
                                            new NullServiceConsumerExceptionBehaviourStrategy()));

            KeylolService.Run <ImageGarage>(args, Container);
        }
        /// <summary>
        /// Регистрация WCF сервисов
        /// </summary>
        public static void RegisterServices(IUnityContainer container)
        {
            var    clientEndpoints        = new ClientEndpoints();
            string fileConvertingEndpoint = clientEndpoints.GetEndpointByInterfaceFullPath(typeof(IFileConvertingServerService));

            container.RegisterFactory <IAccessService>(unity => new AccessService(TIME_OUT_MINUTES_OPERATION));

            container.RegisterFactory <IServiceConsumer <IFileConvertingServerService> >(unity =>
                                                                                         ServiceConsumerFactory.Create <IFileConvertingServerService>(fileConvertingEndpoint));

            string signatureEndpoint = clientEndpoints.GetEndpointByInterfaceFullPath(typeof(ISignatureServerService));

            container.RegisterFactory <IServiceConsumer <ISignatureServerService> >(unity =>
                                                                                    ServiceConsumerFactory.Create <ISignatureServerService>(signatureEndpoint));

            container.RegisterFactory <IWcfServerServicesFactory>(unity =>
                                                                  new WcfServerServicesFactory(() => unity.Resolve <IServiceConsumer <IFileConvertingServerService> >(),
                                                                                               () => unity.Resolve <IServiceConsumer <ISignatureServerService> >()),
                                                                  new SingletonLifetimeManager());
        }
Beispiel #12
0
        public void Sample3_Level300_AutomaticRetry_Manual_Naive_UsingOperationProperty()
        {
            int       retryCount    = 1;
            Exception lastException = null;

            using (var service = ServiceConsumerFactory.Create <IFakeService>(() => new FakeServiceClient()))
            {
                while (retryCount > 0)
                {
                    Console.WriteLine("#### Retry count: " + retryCount);

                    try
                    {
                        int actual = service.Operations.AddIntegers(1, 1);

                        Console.WriteLine("Actual: " + actual);
                        Assert.AreEqual(2, actual);

                        return;
                    }
                    catch (FaultException fe)
                    {
                        lastException = fe;
                        Console.WriteLine("Service operation threw a fault: " + fe.ToString());
                    }
                    catch (Exception ex)
                    {
                        lastException = ex;
                        Console.WriteLine("Technical error occurred while calling the service operation: " + ex.ToString());
                    }

                    retryCount--;
                }
            }

            Assert.Fail("Service operation was not successfully called");
        }
 public AgenteAdministracion()
 {
     proxy = ServiceConsumerFactory.Create <IServicioAdministracion>(() => new ServicioAdministracionClient());
 }
Beispiel #14
0
        /// <inheritdoc />
        /// <exception cref="T:System.Exception">
        /// ServiceConsumerFactory.Create returned null/invalid IHmsCloudService reference.
        /// Check WCF HMS configuration.
        /// </exception>
        public void SendCasinoDiagnosticData(IDictionary <string, IList <byte[]> > diagnosticData)
        {
            if (null == diagnosticData || 0 >= diagnosticData.Count)
            {
                return;
            }

            var casinoCode = Settings.Default.CasinoCode;

            using (
                var cloudServiceProxy = ServiceConsumerFactory.Create <IHmsCloudService>(() => new HmsCloudServerProxy())
                )
            {
                try
                {
                    if (cloudServiceProxy?.Operations == null)
                    {
                        throw new Exception(
                                  "ServiceConsumerFactory.Create returned null/invalid IHmsCloudService reference. Check WCF HMS configuration.");
                    }

                    foreach (var diagnosticFilename in diagnosticData.Keys)
                    {
                        var diagnosticFileChunks = diagnosticData[diagnosticFilename];
                        if (null == diagnosticFileChunks || 0 >= diagnosticFileChunks.Count)
                        {
                            continue;
                        }

                        // create a new ReportGuid - one for each filename being sent (in either one or
                        // multiple messages/chunks - based on size of diagnosticFileChunks IList)
                        var reportGuid = Guid.NewGuid();
                        var reportedAt = DateTime.UtcNow;

                        // The TransactionScope here will provide behavior such that - if/when the call to
                        // DataAggregator.SuccessfulCasinoDiagnosticReport fails for some reason - the transactional
                        // WCF/MSMQ message delivery will be rolled back (i.e., the messages will be discarded
                        // from the client-side MQ).
                        //
                        // Additionally, we bundle all the chunks for each diagnostic file into a single
                        // transaction. Thus, we will roll back if not all chunks are successfully
                        // processed/sent.
                        var txnOptions = new TransactionOptions {
                            IsolationLevel = IsolationLevel.RepeatableRead
                        };
                        using (var txnScope = new TransactionScope(TransactionScopeOption.Required, txnOptions))
                        {
                            for (var iChunk = 0; iChunk < diagnosticFileChunks.Count; ++iChunk)
                            {
                                var chunk = diagnosticFileChunks[iChunk];
                                if (null == chunk || 0 >= chunk.LongLength)
                                {
                                    continue;
                                }

                                var casinoDiagnosticData = new CasinoDiagnosticData
                                {
                                    Filename   = diagnosticFilename,
                                    Chunk      = chunk,
                                    ChunkIndex = iChunk,
                                    NumChunks  = diagnosticFileChunks.Count,
                                    CasinoCode = casinoCode,
                                    ReportGuid = reportGuid,
                                    ReportedAt = reportedAt
                                };

                                cloudServiceProxy.Operations.ReportCasinoDiagnostics(casinoDiagnosticData);
                            }

                            DataAggregator.SuccessfulCasinoDiagnosticReport(diagnosticFilename, reportGuid);
                            txnScope.Complete();
                        }
                    }
                }
                catch (FaultException fe)
                {
                    Logger.Warn($"Service operation ReportCasinoDiagnostics threw a fault: [{fe.Message}]");
                }
                catch (Exception ex)
                {
                    Logger.Warn(
                        $"An unexpected error occurred while calling the ReportCasinoDiagnostics service operation: [{ex.Message}]");
                    var innerEx = ex.InnerException;
                    while (null != innerEx)
                    {
                        Logger.Warn($"[{innerEx.Message}]");
                        innerEx = innerEx.InnerException;
                    }

                    Logger.Warn($"Stack Trace: [{Environment.StackTrace}]");
                }
            }
        }