public void GetConsumer_Timed()
        {
            BufferOptions   options  = BufferOptions.TimedBuffer();
            IConsumer <int> consumer = ConsumerFactory <int> .GetConsumer(new IntConsumer(), null, options);

            Assert.IsType <TimedBufferingConsumer <int> >(consumer);
        }
        /// <summary>
        /// Adds the needed services for Google Cloud Tracing. Used with <see cref="UseGoogleTrace"/>.
        /// </summary>
        /// <param name="services">The service collection. Cannot be null.</param>
        /// /// <param name="setupAction">Action to set up options. Cannot be null.</param>
        public static void AddGoogleTrace(
            this IServiceCollection services, Action <TraceServiceOptions> setupAction)
        {
            GaxPreconditions.CheckNotNull(services, nameof(services));
            GaxPreconditions.CheckNotNull(setupAction, nameof(setupAction));

            var serviceOptions = new TraceServiceOptions();

            setupAction(serviceOptions);

            var client  = serviceOptions.Client ?? TraceServiceClient.Create();
            var options = serviceOptions.Options ?? TraceOptions.Create();
            var traceFallbackPredicate = serviceOptions.TraceFallbackPredicate ?? TraceDecisionPredicate.Default;
            var projectId = CommonUtils.GetAndCheckProjectId(serviceOptions.ProjectId);

            var consumer = ConsumerFactory <TraceProto> .GetConsumer(
                new GrpcTraceConsumer(client), MessageSizer <TraceProto> .GetSize,
                options.BufferOptions, options.RetryOptions);

            var tracerFactory = new ManagedTracerFactory(projectId, consumer,
                                                         RateLimitingTraceOptionsFactory.Create(options), TraceIdFactory.Create());

            services.AddScoped(CreateTraceHeaderContext);

            services.AddSingleton <IManagedTracerFactory>(tracerFactory);
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddSingleton(CreateManagedTracer);
            services.AddSingleton(CreateTraceHeaderPropagatingHandler);
            services.AddSingleton(traceFallbackPredicate);
        }
        public void GetConsumer_Sized()
        {
            BufferOptions   options  = BufferOptions.SizedBuffer();
            IConsumer <int> consumer = ConsumerFactory <int> .GetConsumer(new IntConsumer(), ConstantSizer <int> .GetSize, options);

            Assert.IsType <SizedBufferingConsumer <int> >(consumer);
        }
        /// <summary>
        /// Adds the needed services for Google Cloud Tracing. Used with <see cref="UseGoogleTrace"/>.
        /// </summary>
        /// <param name="services">The service collection. Cannot be null.</param>
        /// <param name="projectId">Optional if running on Google App Engine or Google Compute Engine.
        ///     The Google Cloud Platform project ID. If unspecified and running on GAE or GCE the project ID will be
        ///     detected from the platform.</param>
        /// <param name="config">Optional trace configuration, if unset the default will be used.</param>
        /// <param name="client">Optional Trace client, if unset the default will be used.</param>
        /// <param name="traceFallbackPredicate">Optional function to trace requests. If the trace header is not set
        ///     then this function will be called to determine if a given request should be traced.  This will
        ///     not override trace headers. If the function returns true the request will be traced, if false
        ///     is returned the trace will not be traced and if null is returned it will not affect the
        ///     trace decision.</param>
        public static void AddGoogleTrace(
            this IServiceCollection services, string projectId = null,
            TraceConfiguration config = null, TraceServiceClient client = null,
            Func <HttpRequest, bool?> traceFallbackPredicate = null)
        {
            GaxPreconditions.CheckNotNull(services, nameof(services));

            client = client ?? TraceServiceClient.Create();
            config = config ?? TraceConfiguration.Create();

            projectId = CommonUtils.GetAndCheckProjectId(projectId);

            var consumer = ConsumerFactory <TraceProto> .GetConsumer(
                new GrpcTraceConsumer(client), MessageSizer <TraceProto> .GetSize, config.BufferOptions);

            var tracerFactory = new ManagedTracerFactory(projectId, consumer,
                                                         RateLimitingTraceOptionsFactory.Create(config), TraceIdFactory.Create());

            services.AddScoped(CreateTraceHeaderContext);

            services.AddSingleton <IManagedTracerFactory>(tracerFactory);
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddSingleton(CreateManagedTracer);
            services.AddSingleton(CreateTraceHeaderPropagatingHandler);
            services.AddSingleton(new ShouldTraceRequest(traceFallbackPredicate));
        }
示例#5
0
        private static IMessageConsumer GetConsumer()
        {
            string primaryConnectionString = ConfigurationManager.AppSettings[YOUR_PRIMARY_CONNECTION_STRING];
            var    factory = new ConsumerFactory();

            return(factory.GetConsumer(new ConsumerConfiguration(primaryConnectionString, "testtopic3", "testsubsccription", TimeSpan.MaxValue)));
        }
        public void GetConsumer_InvalidOptions()
        {
            BufferOptions options = new BufferOptions((BufferType)5);

            Assert.Throws <ArgumentException>(
                () => ConsumerFactory <int> .GetConsumer(new IntConsumer(), null, options));
        }
        public void GetConsumer_None()
        {
            BufferOptions   options  = BufferOptions.NoBuffer();
            IConsumer <int> consumer = ConsumerFactory <int> .GetConsumer(new IntConsumer(), null, options);

            Assert.IsType <IntConsumer>(consumer);
        }
示例#8
0
        public void GetConsumer_InvalidOptions()
        {
            BufferOptions bufferOptions = new BufferOptions((BufferType)5);
            RetryOptions  retryOptions  = RetryOptions.NoRetry();

            Assert.Throws <ArgumentException>(
                () => ConsumerFactory <int> .GetConsumer(new IntConsumer(), ConstantSizer <int> .GetSize, bufferOptions, retryOptions));
        }
示例#9
0
        public void GetConsumer_Timed()
        {
            BufferOptions   bufferOptions = BufferOptions.TimedBuffer();
            RetryOptions    retryOptions  = RetryOptions.NoRetry();
            IConsumer <int> consumer      = ConsumerFactory <int> .GetConsumer(new IntConsumer(), ConstantSizer <int> .GetSize, bufferOptions, retryOptions);

            Assert.IsType <TimedBufferingConsumer <int> >(consumer);
        }
示例#10
0
        public static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            var rabbitMqSetting = new Restonode.Common.Settings.RabbitMQSettings();
            var mailSettings    = new Restonode.Common.Settings.MailSettings();

            configuration.GetSection("RabbitMQ").Bind(rabbitMqSetting);
            configuration.GetSection("Mail").Bind(mailSettings);

            var factory = new ConnectionFactory {
                HostName = rabbitMqSetting.HostName
            };

            Console.WriteLine("Starting Messaging Service...\n");

            ConsoleKeyInfo cki;

            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs ea) =>
            {
                Console.WriteLine("\nThe read operation has been interrupted.\n");

                Console.WriteLine($"Key pressed: {ea.SpecialKey}\n");

                ea.Cancel = true;

                Console.WriteLine("Press any key to resume...\n");
            };

            while (true)
            {
                Console.WriteLine("Press 'X' to quit or CTRL+C to interrupt the read operation.\n");

                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                        using (var consumerFactory = new ConsumerFactory(channel))
                        {
                            channel.BasicQos(0, 1, false);

                            foreach (var queue in rabbitMqSetting.Queues)
                            {
                                channel.QueueDeclare(queue.Name, true, false, false, null);

                                channel.BasicConsume(queue.Name, false, consumerFactory.GetConsumer(queue.Type, mailSettings));
                            }

                            cki = Console.ReadKey(true);

                            if (cki.Key == ConsoleKey.X)
                            {
                                break;
                            }
                        }
            }
        }
示例#11
0
        void btnGetPin_Click(object sender, EventArgs e)
        {
            string weiboType = ddlWeibo.Text;
            Consumer consumer = ConsumerFactory.GetConsumer(weiboType);

            _requestToken = consumer.GetRequestToken();
            string authorizeUri = _requestToken.GetNormalizedAuthorizeUri();

            Process.Start(authorizeUri);
        }
示例#12
0
        public void GetConsumer_None()
        {
            BufferOptions   bufferOptions = BufferOptions.NoBuffer();
            RetryOptions    retryOptions  = RetryOptions.NoRetry();
            IConsumer <int> consumer      = ConsumerFactory <int> .GetConsumer(new IntConsumer(), ConstantSizer <int> .GetSize, bufferOptions, retryOptions);

            Assert.IsType <RpcRetryConsumer <int> >(consumer);
            var retryConsumer = (RpcRetryConsumer <int>)consumer;

            Assert.IsType <IntConsumer>(retryConsumer._consumer);
        }
示例#13
0
        public ActionResult Login(string weiboType)
        {
            var consumer = ConsumerFactory.GetConsumer(weiboType);

            consumer.Callback = "http://localhost:4055/Home/Callback";

            var requestToken = consumer.GetRequestToken();

            Session["requestToken"] = requestToken;
            var authorizeUri = requestToken.GetNormalizedAuthorizeUri();

            return(Redirect(authorizeUri));
        }
        private CloudTrace(string projectId, TraceConfiguration config = null, Task <TraceServiceClient> client = null)
        {
            GaxPreconditions.CheckNotNull(projectId, nameof(projectId));

            // Create the default values if not set.
            client = client ?? TraceServiceClient.CreateAsync();
            config = config ?? TraceConfiguration.Create();

            var consumer = ConsumerFactory <TraceProto> .GetConsumer(
                new GrpcTraceConsumer(client), MessageSizer <TraceProto> .GetSize, config.BufferOptions);

            _tracerFactory = new ManagedTracerFactory(projectId, consumer,
                                                      RateLimitingTraceOptionsFactory.Create(config), TraceIdFactory.Create());
        }
示例#15
0
        private CloudTrace(string projectId, TraceConfiguration config = null, Task <TraceServiceClient> client = null)
        {
            _projectId = GaxPreconditions.CheckNotNull(projectId, nameof(projectId));

            // Create the default values if not set.
            client = client ?? TraceServiceClient.CreateAsync();
            config = config ?? TraceConfiguration.Create();

            _traceIdfactory = TraceIdFactory.Create();
            _consumer       = ConsumerFactory <TraceProto> .GetConsumer(
                new GrpcTraceConsumer(client), TraceSizer.Instance, config.BufferOptions);

            _rateFactory   = RateLimitingTraceOptionsFactory.Create(config);
            _headerFactory = TraceHeaderTraceOptionsFactory.Create();
        }
示例#16
0
        private CloudTrace(string projectId, TraceOptions options        = null, TraceServiceClient client = null,
                           TraceDecisionPredicate traceFallbackPredicate = null)
        {
            GaxPreconditions.CheckNotNull(projectId, nameof(projectId));

            // Create the default values if not set.
            client  = client ?? TraceServiceClient.Create();
            options = options ?? TraceOptions.Create();
            _traceFallbackPredicate = traceFallbackPredicate ?? TraceDecisionPredicate.Default;

            _consumer = ConsumerFactory <TraceProto> .GetConsumer(
                new GrpcTraceConsumer(client), MessageSizer <TraceProto> .GetSize, options.BufferOptions);

            _tracerFactory = new ManagedTracerFactory(projectId, _consumer,
                                                      RateLimitingTraceOptionsFactory.Create(options), TraceIdFactory.Create());
        }
示例#17
0
        private CloudTrace(string projectId, TraceConfiguration config      = null, TraceServiceClient client = null,
                           Func <HttpRequest, bool?> traceFallbackPredicate = null)
        {
            GaxPreconditions.CheckNotNull(projectId, nameof(projectId));

            // Create the default values if not set.
            client = client ?? TraceServiceClient.Create();
            config = config ?? TraceConfiguration.Create();

            _traceFallbackPredicate = traceFallbackPredicate;

            _consumer = ConsumerFactory <TraceProto> .GetConsumer(
                new GrpcTraceConsumer(client), MessageSizer <TraceProto> .GetSize, config.BufferOptions);

            _tracerFactory = new ManagedTracerFactory(projectId, _consumer,
                                                      RateLimitingTraceOptionsFactory.Create(config), TraceIdFactory.Create());
        }
        /// <summary>
        /// Adds a <see cref="GoogleLoggerProvider"/> for <see cref="GoogleLogger"/>s.
        /// </summary>
        /// <param name="factory">The logger factory. Cannot be null.</param>
        /// <param name="logTo">Where to log to. Cannot be null. Cannot be null.</param>
        /// <param name="options">Optional, options for the logger.</param>
        /// <param name="client">Optional, logging client.</param>
        public static ILoggerFactory AddGoogle(this ILoggerFactory factory, LogTo logTo,
                                               LoggerOptions options = null, LoggingServiceV2Client client = null)
        {
            // Check params and set defaults if unset.
            GaxPreconditions.CheckNotNull(factory, nameof(factory));
            GaxPreconditions.CheckNotNull(logTo, nameof(logTo));
            client  = client ?? LoggingServiceV2Client.Create();
            options = options ?? LoggerOptions.Create();

            // Get the proper consumer from the options and add a logger provider.
            GrpcLogConsumer      grpcConsumer = new GrpcLogConsumer(client);
            IConsumer <LogEntry> consumer     = ConsumerFactory <LogEntry> .GetConsumer(
                grpcConsumer, LogEntrySizer.Instance, options.BufferOptions);

            GoogleLoggerProvider provider = new GoogleLoggerProvider(consumer, logTo, options);

            factory.AddProvider(provider);
            return(factory);
        }
        /// <summary>
        /// Adds the needed services for Google Cloud Tracing. Used with <see cref="UseGoogleTrace"/>.
        /// </summary>
        /// <param name="services">The service collection. Cannot be null.</param>
        /// <param name="projectId">The Google Cloud Platform project ID. Cannot be null.</param>
        /// <param name="config">Optional trace configuration, if unset the default will be used.</param>
        /// <param name="clientTask">Optional task which produces the Trace client, if
        ///     unset the default will be used.</param>
        public static void AddGoogleTrace(
            this IServiceCollection services, string projectId,
            TraceConfiguration config = null, Task <TraceServiceClient> clientTask = null)
        {
            GaxPreconditions.CheckNotNull(services, nameof(services));
            GaxPreconditions.CheckNotNull(projectId, nameof(projectId));

            clientTask = clientTask ?? TraceServiceClient.CreateAsync();
            config     = config ?? TraceConfiguration.Create();

            IConsumer <TraceProto> consumer = ConsumerFactory <TraceProto> .GetConsumer(
                new GrpcTraceConsumer(clientTask), MessageSizer <TraceProto> .GetSize, config.BufferOptions);

            var tracerFactory = new ManagedTracerFactory(projectId, consumer,
                                                         RateLimitingTraceOptionsFactory.Create(config), TraceIdFactory.Create());

            services.AddSingleton <IManagedTracerFactory>(tracerFactory);
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddScoped(CreateTraceHeaderContext);
            services.AddScoped(CreateManagedTracer);
        }
示例#20
0
 public void GetConsumer_InvalidOptions_Null()
 {
     Assert.Throws <ArgumentNullException>(
         () => ConsumerFactory <int> .GetConsumer(new IntConsumer(), ConstantSizer <int> .GetSize, null, RetryOptions.NoRetry()));
 }
示例#21
0
 public void GetConsumer_InvalidConsumer_Null()
 {
     Assert.Throws <ArgumentNullException>(
         () => ConsumerFactory <int> .GetConsumer(null, ConstantSizer <int> .GetSize, BufferOptions.TimedBuffer(), RetryOptions.NoRetry()));
 }
 public void GetConsumer_InvalidSizer_Null()
 {
     Assert.Throws <ArgumentNullException>(
         () => ConsumerFactory <int> .GetConsumer(new IntConsumer(), null, BufferOptions.SizedBuffer()));
 }
 public void GetConsumer_InvalidOptions_Null()
 {
     Assert.Throws <ArgumentNullException>(
         () => ConsumerFactory <int> .GetConsumer(new IntConsumer(), null, null));
 }