private async Task CreateNewConnection()
        {
            lock (this.connectionLock)
            {
                //TODO: Enable transport security.
                var binding = new NetTcpBinding()
                {
                    MaxBufferSize          = int.MaxValue,
                    ReaderQuotas           = System.Xml.XmlDictionaryReaderQuotas.Max,
                    MaxReceivedMessageSize = int.MaxValue,
                    Security = new NetTcpSecurity()
                    {
                        Message = new MessageSecurityOverTcp()
                        {
                            ClientCredentialType = MessageCredentialType.None
                        },
                        Transport = new TcpTransportSecurity()
                        {
                            ClientCredentialType = TcpClientCredentialType.None
                        },
                        Mode = SecurityMode.None,
                    },
                    CloseTimeout   = this.timeout,
                    OpenTimeout    = this.timeout,
                    ReceiveTimeout = this.timeout,
                    SendTimeout    = this.timeout,
                };

                // TODO: put this in a configuration file somewhere
                var remoteAddress = new EndpointAddress(new Uri("net.tcp://MessageBrokerProxyService:8137/MessageBrokerProxyService"));

                this.serviceCallback = new ServiceCallback(this.eventAggregator);
                this.serviceClient   = new MessageBrokerProxyServiceClient(new InstanceContext(this.serviceCallback), binding, remoteAddress);
                this.serviceClient.InnerChannel.OperationTimeout = this.timeout;
                this.serviceClient.InnerChannel.Faulted         += this.OnServiceClientFaulted;
            }

            await this.serviceClient.SubscribeAsync();

            this.keepAliveWorker = Task.Run(this.KeepAliveWorkerLoop);
            await this.eventAggregator.PublishOnUIThreadAsync(new Connected());
        }
        private async Task Disconnect(bool isFault = false)
        {
            lock (this.connectionLock)
            {
                try
                {
                    if (this.serviceClient != null)
                    {
                        this.serviceClient.InnerChannel.Faulted -= this.OnServiceClientFaulted;
                        if (this.serviceClient.InnerChannel.State == CommunicationState.Faulted)
                        {
                            this.serviceClient.Abort();
                        }
                        else
                        {
                            this.serviceClient.UnsubscribeAsync();
                            this.serviceClient.CloseAsync();
                        }
                    }
                }
                catch
                {
                    // ignore
                }
                finally
                {
                    this.serviceClient   = null;
                    this.serviceCallback = null;
                }
            }

            if (isFault)
            {
                await this.eventAggregator.PublishOnUIThreadAsync(new Disconnected());
            }
        }
 public MessageBrokerProxyServiceClient(System.ServiceModel.InstanceContext callbackInstance, EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
     base(callbackInstance, MessageBrokerProxyServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
 {
     this.Endpoint.Name = endpointConfiguration.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }
 public MessageBrokerProxyServiceClient(System.ServiceModel.InstanceContext callbackInstance) :
     base(callbackInstance, MessageBrokerProxyServiceClient.GetDefaultBinding(), MessageBrokerProxyServiceClient.GetDefaultEndpointAddress())
 {
     this.Endpoint.Name = EndpointConfiguration.ServiceEndpoint.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }
 private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
 {
     return(MessageBrokerProxyServiceClient.GetEndpointAddress(EndpointConfiguration.ServiceEndpoint));
 }
 private static System.ServiceModel.Channels.Binding GetDefaultBinding()
 {
     return(MessageBrokerProxyServiceClient.GetBindingForEndpoint(EndpointConfiguration.ServiceEndpoint));
 }