Exemplo n.º 1
0
        public async Task SendMessageAsync(string message)
        {
            using (var connection = new AppServiceConnection())
            {
                connection.AppServiceName    = "com.cninnovation.bridgesample";
                connection.PackageFamilyName = "6d982834-6814-4d82-b331-8644a7f54418_2dq4k2rrbc0fy";

                AppServiceConnectionStatus status = await connection.OpenAsync();

                if (status == AppServiceConnectionStatus.Success)
                {
                    var valueSet = new ValueSet();
                    valueSet.Add("command", message);
                    AppServiceResponse response = await connection.SendMessageAsync(valueSet);

                    if (response.Status == AppServiceResponseStatus.Success)
                    {
                        string answer = string.Join(", ", response.Message.Values.Cast <string>().ToArray());
                        await _dialogService.ShowMessageAsync($"received {answer}");
                    }
                    else
                    {
                        await _dialogService.ShowMessageAsync($"error sending message {response.Status.ToString()}");
                    }
                }
                else
                {
                    await _dialogService.ShowMessageAsync(status.ToString());
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Open the connection to the App service
        /// </summary>
        /// <returns>The AppServiceConnectionStatus object giving the status of the connection</returns>
        public IAsyncOperation <AppServiceConnectionStatus> OpenConnectionAsync()
        {
            _log.Information("OpenConnectionAsync: called");
            return(Task <AppServiceConnectionStatus> .Run(async() =>
            {
                _log.Information("OpenConnectionAsync: Creating App Service Connection");
                AppServiceConnection connection = new AppServiceConnection();

                // Here, we use the app service name defined in the app service provider's Package.appxmanifest file in the <Extension> section.
                connection.AppServiceName = ServiceName;

                // Use Windows.ApplicationModel.Package.Current.Id.FamilyName within the app service provider to get this value.
                connection.PackageFamilyName = ServiceFamilyName;

                Status = await connection.OpenAsync();
                bool bRet = Status == AppServiceConnectionStatus.Success;
                _log.Information($"OpenConnectionAsync: Connection Status = {Status.ToString()}");

                if (bRet)
                {
                    Connection = connection;
                }
                return Status;
            }).AsAsyncOperation <AppServiceConnectionStatus>());
        }
Exemplo n.º 3
0
        private async void InitializeAppServiceConnection()
        {
            try
            {
                connection = new AppServiceConnection
                {
                    AppServiceName    = "CommunicationService",
                    PackageFamilyName = Package.Current.Id.FamilyName
                };
                connection.RequestReceived += Connection_RequestReceived;
                connection.ServiceClosed   += Connection_ServiceClosed;


                AppServiceConnectionStatus status = await connection.OpenAsync();

                if (status != AppServiceConnectionStatus.Success)
                {
                    await SendMessage("response", $"AppServiceConnectionStatus : {status.ToString()}");

                    Current.Shutdown();
                }


                await SendMessage("response", "connet ok");
            }
            catch (Exception ex)
            {
                await SendMessage("response", ex.ToString());

                Current.Shutdown();
            }
        }
Exemplo n.º 4
0
        static async System.Threading.Tasks.Task <bool> ConnectToAppService()
        {
            bool bResult = false;

            LogMessage("Win32Proc: Connect To AppService");
            connection = new AppServiceConnection();
            connection.AppServiceName    = AppServiceTaskConstant.APPSERVICENAME;
            connection.PackageFamilyName = AppServiceTaskConstant.APPSERVICEPACKAGEFAMILY;
            connection.RequestReceived  += Connection_RequestReceived;
            connection.ServiceClosed    += Connection_ServiceClosed;

            AppServiceConnectionStatus status = await connection.OpenAsync();

            if (status == AppServiceConnectionStatus.Success)
            {
                bResult = true;
                // TODO: error handling
                LogMessage("Win32Proc: Connect To AppService Successful");
            }
            else
            {
                LogMessage("Win32Proc: Failed to Connect To AppService - Status: " + status.ToString());
            }
            return(bResult);
        }
        private async void OnWPFAppService(object sender, RoutedEventArgs e)
        {
            _appServiceConnection = new AppServiceConnection();
            _appServiceConnection.AppServiceName    = "com.cninnovation.wpfbridgesample";
            _appServiceConnection.PackageFamilyName = "676efced-1da0-481b-8db0-97f991f1c4d0_2dq4k2rrbc0fy";

            _appServiceConnection.RequestReceived += OnRequestReceived;
            AppServiceConnectionStatus status = await _appServiceConnection.OpenAsync();

            if (status == AppServiceConnectionStatus.Success)
            {
                var valueSet = new ValueSet();
                valueSet.Add("command", "test");
                AppServiceResponse response = await _appServiceConnection.SendMessageAsync(valueSet);

                if (response.Status == AppServiceResponseStatus.Success)
                {
                    string answer = string.Join(", ", response.Message.Values.Cast <string>().ToArray());
                    await new MessageDialog($"received {answer}").ShowAsync();
                }
                else
                {
                    await new MessageDialog("error send").ShowAsync();
                }
            }
            else
            {
                await new MessageDialog(status.ToString()).ShowAsync();
            }
        }
        public override async Task <object> ExecuteAsync(ValueSet parameters)
        {
            if (_serviceName.IsNullorEmpty())
            {
                throw new InvalidProgramException("Extension is not a service");
            }
            try
            {
                // do app service call
                using (var connection = new AppServiceConnection())
                {
                    // service name was in properties
                    connection.AppServiceName = _serviceName;

                    // package Family Name is in the extension
                    connection.PackageFamilyName = this.AppExtension.Package.Id.FamilyName;

                    // open connection
                    AppServiceConnectionStatus status = await connection.OpenAsync();

                    if (status != AppServiceConnectionStatus.Success)
                    {
                        throw new InvalidOperationException(status.ToString());
                    }
                    else
                    {
                        // send request to service
                        // get response
                        AppServiceResponse response = await connection.SendMessageAsync(parameters);

                        if (response.Status == AppServiceResponseStatus.Success)
                        {
                            ValueSet message = response.Message as ValueSet;
                            if (message.ContainsKey("status") && (int)message["status"] == 1)
                            {
                                if (message.ContainsKey("search_result") && message["search_result"] is string s)
                                {
                                    return(GetGenericMusicItem(s));
                                }
                                if (message.ContainsKey("song_result") && message["song_result"] is string t)
                                {
                                    return(GetOnlineSong(t));
                                }
                                if (message.ContainsKey("album_result") && message["album_result"] is string r)
                                {
                                    return(GetAlbum(r, message["songs"] as string, message["album_artists"] as string));
                                }
                            }
                        }
                    }
                }
                return(null);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        async private void OnAppServiceShareFile(object sender, RoutedEventArgs e)
        {
            _name_count++;
            //create or replace the file
            var folder = ApplicationData.Current.LocalFolder;
            var file   = await folder.CreateFileAsync("customer.xml", CreationCollisionOption.ReplaceExisting);

            //write the message
            var xml = $"<customer><name>john_{_name_count}</name>"
                      + @"<dob>10/19/1990</dob>
                <email>[email protected]</email>
            </customer>";
            await file.AppendTextAsync(xml);

            //share the file
            var token = SharedStorageAccessManager.AddFile(file);

            AppServiceConnection connection = new AppServiceConnection();

            connection.AppServiceName    = "bmx-service";
            connection.PackageFamilyName = "748f8ea9-5de1-48be-a132-323bdc3c5fcc_hxmhn7vrhn3vp";

            AppServiceConnectionStatus status = await connection.OpenAsync();

            if (status == AppServiceConnectionStatus.Success)
            {
                //Send data to the service 
                var message = new ValueSet();
                message.Add("command", "customer-file-share");
                message.Add("token", token);

                //Send message and wait for response 
                AppServiceResponse response = await connection.SendMessageAsync(message);

                if (response.Status == AppServiceResponseStatus.Success)
                {
                    await new MessageDialog("Sent").ShowAsync();
                }
                else
                {
                    await new MessageDialog("Failed when sending message to app").ShowAsync();
                }
            }
            else
            {
                await new MessageDialog(status.ToString()).ShowAsync();
            }
            connection.Dispose();
        }
Exemplo n.º 8
0
        private async void InitializeAppServiceConnection()
        {
            connection = new AppServiceConnection();
            connection.AppServiceName    = "SampleInteropService";
            connection.PackageFamilyName = Package.Current.Id.FamilyName;
            connection.RequestReceived  += Connection_RequestReceived;
            connection.ServiceClosed    += Connection_ServiceClosed;

            AppServiceConnectionStatus status = await connection.OpenAsync();

            if (status != AppServiceConnectionStatus.Success)
            {
                MessageBox.Show(status.ToString());
                this.IsEnabled = false;
            }
        }
Exemplo n.º 9
0
        private async Task SendToUWP(ValueSet message)
        {
            if (connection == null)
            {
                connection = new AppServiceConnection();
                connection.PackageFamilyName = Package.Current.Id.FamilyName;
                connection.AppServiceName    = "SystrayExtensionService";
                connection.ServiceClosed    += Connection_ServiceClosed;
                AppServiceConnectionStatus connectionStatus = await connection.OpenAsync();

                if (connectionStatus != AppServiceConnectionStatus.Success)
                {
                    MessageBox.Show("Status: " + connectionStatus.ToString());
                    return;
                }
            }

            await connection.SendMessageAsync(message);
        }
Exemplo n.º 10
0
        public async Task SendMessageAsync(string message)
        {
            try
            {
                if (_connection == null)
                {
                    _connection = new AppServiceConnection
                    {
                        AppServiceName    = "com.cninnovation.desktopbridgesample",
                        PackageFamilyName = "6d982834-6814-4d82-b331-8644a7f54418_2dq4k2rrbc0fy"
                    };

                    AppServiceConnectionStatus status = await _connection.OpenAsync();

                    _connection.RequestReceived += Connection_RequestReceived;

                    if (status == AppServiceConnectionStatus.Success)
                    {
                        var valueSet = new ValueSet();
                        valueSet.Add("command", message);
                        AppServiceResponse response = await _connection.SendMessageAsync(valueSet);

                        if (response.Status == AppServiceResponseStatus.Success)
                        {
                            string answer = string.Join(", ", response.Message.Values.Cast <string>().ToArray());
                            MessageReceived?.Invoke(this, $"received {answer}");
                        }
                        else
                        {
                            MessageReceived?.Invoke(this, $"error sending message { response.Status.ToString()}");
                        }
                    }
                    else
                    {
                        await _dialogService.ShowMessageAsync(status.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                await _dialogService.ShowMessageAsync(ex.Message);
            }
        }
Exemplo n.º 11
0
        private async Task SendMessage(ValueSet message)

        {
            if (connection == null)
            {
                connection = new AppServiceConnection
                {
                    PackageFamilyName = Package.Current.Id.FamilyName,
                    AppServiceName    = "FluentTerminalAppService"
                };
                connection.ServiceClosed += Connection_ServiceClosed;
                AppServiceConnectionStatus connectionStatus = await connection.OpenAsync();

                if (connectionStatus != AppServiceConnectionStatus.Success)
                {
                    MessageBox.Show("Status: " + connectionStatus.ToString());
                    return;
                }
            }
            await connection.SendMessageAsync(message);
        }
        public Task StartTaskAsync()
        {
            async void Run()
            {
                try
                {
                    _appServiceConnection = new AppServiceConnection();
                    _appServiceConnection.AppServiceName    = "com.cninnovation.wpfbridgesample";
                    _appServiceConnection.PackageFamilyName = Package.Current.Id.FamilyName;
                    _appServiceConnection.RequestReceived  += OnRequestReceived;

                    AppServiceConnectionStatus status = await _appServiceConnection.OpenAsync();

                    if (status == AppServiceConnectionStatus.Success)
                    {
                        await _dialogService.ShowMessageAsync("Successfully started App-Service");

                        var message = new ValueSet();
                        message.Add("command", "Hello");
                        var response = await _appServiceConnection.SendMessageAsync(message);

                        await _dialogService.ShowMessageAsync(response.Status.ToString());
                    }
                    else
                    {
                        await _dialogService.ShowMessageAsync(status.ToString());
                    }
                }
                catch (InvalidOperationException)
                {
                    await _dialogService.ShowMessageAsync("invalid operation - are you running with a package?");
                }
            }

            var t1 = new Task(Run, TaskCreationOptions.LongRunning);

            t1.Start();
            return(t1);
        }
Exemplo n.º 13
0
        private async Task ConnectToRemoteAppServiceAsync()
        {
            RemoteSystem selectedDevice = DeviceListComboBox.SelectedItem as RemoteSystem;

            if (selectedDevice != null)
            {
                // Create a remote system connection request.
                RemoteSystemConnectionRequest connectionRequest = new RemoteSystemConnectionRequest(selectedDevice);

                // Set up a new app service connection. The following app service name and package family name
                // are used in this sample to work with AppServices provider SDK sample on a remote system.
                using (AppServiceConnection connection = new AppServiceConnection
                {
                    AppServiceName = "com.microsoft.randomnumbergenerator",
                    PackageFamilyName = "Microsoft.SDKSamples.AppServicesProvider.CS_8wekyb3d8bbwe"
                })
                {
                    UpdateStatus("Opening connection to remote app service...", NotifyType.StatusMessage);
                    AppServiceConnectionStatus status = await connection.OpenRemoteAsync(connectionRequest);

                    if (status == AppServiceConnectionStatus.Success)
                    {
                        UpdateStatus("Successfully connected to remote app service.", NotifyType.StatusMessage);
                        await SendMessageToRemoteAppServiceAsync(connection);
                    }
                    else
                    {
                        UpdateStatus("Attempt to open a remote app service connection failed with error - " + status.ToString(), NotifyType.ErrorMessage);
                    }
                }
            }
            else
            {
                UpdateStatus("Select a device for remote connection.", NotifyType.ErrorMessage);
            }
        }
Exemplo n.º 14
0
        private async void OnAppService(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // don't dispose the connection now! This closes the app!
            if (_appServiceConnection == null)
            {
                _appServiceConnection = new AppServiceConnection
                {
                    AppServiceName    = "com.cninnovation.desktopbridgesample",
                    PackageFamilyName = "6d982834-6814-4d82-b331-8644a7f54418_2dq4k2rrbc0fy"
                };

                AppServiceConnectionStatus status = await _appServiceConnection.OpenAsync();

                if (status != AppServiceConnectionStatus.Success)
                {
                    throw new InvalidOperationException($"App service connection failed with status {status.ToString()}");
                }
            }

            var valueSet = new ValueSet
            {
                { "command", "data" },
                { "data", textData.Text }
            };
            AppServiceResponse response = await _appServiceConnection.SendMessageAsync(valueSet);

            if (response.Status == AppServiceResponseStatus.Success)
            {
                string answer = string.Join(", ", response.Message.Values.Cast <string>().ToArray());
                await new MessageDialog($"received {answer}").ShowAsync();
            }
            else
            {
                await new MessageDialog("error send").ShowAsync();
            }
        }