Inheritance: IDisposable
Example #1
0
        public async Task <string> OpenAsync(CancellationToken cancellationToken)
        {
            Logger.Debug(nameof(this.OpenAsync));

            try
            {
                EndpointResourceDescription endpoint = this.serviceContext
                                                       .CodePackageActivationContext.GetEndpoint(this.serviceEndpoint);
                int port = endpoint.Port;

                //拼接字符串
                this.listeningAddress = string.Format(
                    CultureInfo.InvariantCulture,
                    "http://+:{0}/{1}",
                    port,
                    string.IsNullOrWhiteSpace(this.appRoot)
                    ? string.Empty
                    : this.appRoot.TrimEnd('/') + '/');

                if (this.serviceContext is StatefulServiceContext)
                {
                    StatefulServiceContext sip = (StatefulServiceContext)this.serviceContext;
                    this.listeningAddress += sip.PartitionId + "/" + sip.ReplicaId + "/";
                }

                this.publishAddress = this.listeningAddress.Replace("+", FabricRuntime.GetNodeContext().IPAddressOrFQDN);

                this.publishAddress = this.publishAddress.Replace("http", "ws");

                Logger.Info("Starting websocket listener on {0}", this.listeningAddress);
                this.webSocketApp = new WebSocketApp(this.listeningAddress);
                this.webSocketApp.Init();

                this.mainLoop = this.webSocketApp.StartAsync(this.ProcessConnectionAsync);
                Logger.Info("Started websocket listener on {0}", this.listeningAddress);

                return(await Task.FromResult(this.publishAddress));
            }
            catch (Exception ex)
            {
                Logger.Error(ex, nameof(this.OpenAsync));
                throw;
            }
        }
        public async Task<string> OpenAsync(CancellationToken cancellationToken)
        {
            Logger.Debug(nameof(this.OpenAsync));

            try
            {
                EndpointResourceDescription endpoint = this.serviceContext
                    .CodePackageActivationContext.GetEndpoint(this.serviceEndpoint);
                int port = endpoint.Port;

                this.listeningAddress = string.Format(
                    CultureInfo.InvariantCulture,
                    "http://+:{0}/{1}",
                    port,
                    string.IsNullOrWhiteSpace(this.appRoot)
                        ? string.Empty
                        : this.appRoot.TrimEnd('/') + '/');

                if (this.serviceContext is StatefulServiceContext)
                {
                    StatefulServiceContext sip = (StatefulServiceContext) this.serviceContext;
                    this.listeningAddress += sip.PartitionId + "/" + sip.ReplicaId + "/";
                }

                this.publishAddress = this.listeningAddress.Replace("+", FabricRuntime.GetNodeContext().IPAddressOrFQDN);

                this.publishAddress = this.publishAddress.Replace("http", "ws");

                Logger.Info("Starting websocket listener on {0}", this.listeningAddress);
                this.webSocketApp = new WebSocketApp(this.listeningAddress);
                this.webSocketApp.Init();

                this.mainLoop = this.webSocketApp.StartAsync(this.ProcessConnectionAsync);
                Logger.Info("Started websocket listener on {0}", this.listeningAddress);

                return await Task.FromResult(this.publishAddress);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, nameof(this.OpenAsync));
                throw;
            }
        }