Ejemplo n.º 1
0
        public async Task <Guid> OpenAsync()
        {
            if (this.ServiceState != ServiceState.None)
            {
                throw new InvalidOperationException();
            }
            this.ServiceState = ServiceState.Opening;
            this.Dispatcher   = await Dispatcher.CreateAsync(this);

            try
            {
                await this.Dispatcher.InvokeAsync(() =>
                {
                    this.token = ServiceToken.NewToken();
                    this.serializerProvider = this.componentProvider.GetserializerProvider(this.SerializerType);
                    this.serializer         = this.serializerProvider.Create(this, this.componentProvider.DataSerializers);
                    this.Debug($"{this.serializerProvider.Name} Serializer created.");
                    this.adpatorHostProvider = this.componentProvider.GetAdaptorHostProvider(this.AdaptorHostType);
                    this.adaptorHost         = this.adpatorHostProvider.Create(this, this.instanceContext, token);
                    this.Debug($"{this.adpatorHostProvider.Name} Adaptor created.");
                    this.adaptorHost.Disconnected += AdaptorHost_Disconnected;
                });

                await this.instanceContext.InitializeInstanceAsync();

                foreach (var item in this.ServiceHosts)
                {
                    await item.OpenAsync(token);

                    await this.DebugAsync($"{item.Name} Service opened.");
                }
                await this.adaptorHost.OpenAsync(this.Host, this.Port);

                await this.DebugAsync($"{this.adpatorHostProvider.Name} Adaptor opened.");

                await this.Dispatcher.InvokeAsync(() =>
                {
                    this.Debug($"Service Context opened.");
                    this.ServiceState = ServiceState.Open;
                    this.OnOpened(EventArgs.Empty);
                });

                return(this.token.Guid);
            }
            catch
            {
                this.ServiceState = ServiceState.None;
                await this.AbortAsync();

                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task CloseAsync(Guid token, int closeCode)
        {
            if (token == Guid.Empty || this.token.Guid != token)
            {
                throw new ArgumentException($"invalid token: {token}", nameof(token));
            }
            if (this.ServiceState != ServiceState.Open)
            {
                throw new InvalidOperationException();
            }
            if (closeCode == int.MinValue)
            {
                throw new ArgumentException($"invalid close code: '{closeCode}'", nameof(closeCode));
            }
            try
            {
                await this.adaptorHost.CloseAsync(closeCode);

                await this.DebugAsync($"{this.adpatorHostProvider.Name} Adaptor closed.");

                foreach (var item in this.ServiceHosts.Reverse())
                {
                    await item.CloseAsync(this.token);

                    await this.Dispatcher.InvokeAsync(() =>
                    {
                        this.Debug($"{item.Name} Service closed.");
                    });
                }
                await this.instanceContext.ReleaseInstanceAsync();

                await this.Dispatcher.InvokeAsync(() =>
                {
                    this.adaptorHost.Disconnected -= AdaptorHost_Disconnected;
                    this.adaptorHost = null;
                    this.serializer  = null;
                    this.Dispatcher.Dispose();
                    this.Dispatcher   = null;
                    this.token        = ServiceToken.Empty;
                    this.ServiceState = ServiceState.None;
                    this.OnClosed(new CloseEventArgs(closeCode));
                    this.Debug($"Service Context closed.");
                });
            }
            catch (Exception e)
            {
                await this.AbortAsync();

                throw e;
            }
        }