0

I'm trying to make an application with an extended boot screen that needs to connect to a server to start the application. Then thereafter, the application will receive and send data in the background.

I am experiencing a problem when compiling my code, Visual displays the error below:

Erreur de validation. error 80080204: App manifest validation error: Line 35, Column 12, Reason: If it is not an audio background task, it is not allowed to have EntryPoint="PhonieMarthaBackground.SocketActivityTask" without ActivatableClassId in windows.activatableClass.inProcessServer.    PhonieMartha    C:\Users\PhonieMARTHA\source\repos\PhonieMartha\PhonieMartha\bin\x64\Debug\AppxManifest.xml 

I don't understand what's wrong with the manifest.

AppxManifest.xml (Short part):

  <Applications>
    <Application Id="App" Executable="PhonieMartha.exe" EntryPoint="PhonieMartha.App">
  <uap:VisualElements DisplayName="PhonieMartha" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="PhonieMartha" BackgroundColor="transparent">
    <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
    <uap:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="lightGray" />
  </uap:VisualElements>
  <Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="PhonieMarthaBackground.SocketActivityTask">
      <BackgroundTasks>
        <Task Type="systemEvent" />
      </BackgroundTasks>
    </Extension>
  </Extensions>
</Application>

ExtendedSplash.xaml.cs:

private const string socketId = "SampleSocket";
private StreamSocket socket = null;
private IBackgroundTaskRegistration task = null;
private const string port = "40404";
private const string adress = "172.16.161.80";

try
        {

            foreach (var current in BackgroundTaskRegistration.AllTasks)
            {
                if (current.Value.Name == "PhonieMarthaBackground")
                {
                    task = current.Value;
                    break;
                }
            }

            // Si il n'y a pas de tâche déjà crée alors, on en crée une nouvelle 
            if (task == null)
            {
                var socketTaskBuilder = new BackgroundTaskBuilder();
                socketTaskBuilder.Name = "PhonieMarthaBackground";
                socketTaskBuilder.TaskEntryPoint = "PhonieMarthaBackground.SocketActivityTask";
                var trigger = new SocketActivityTrigger();
                socketTaskBuilder.SetTrigger(trigger);
                task = socketTaskBuilder.Register();
            }

            SocketActivityInformation socketInformation;
            if (SocketActivityInformation.AllSockets.TryGetValue(socketId, out socketInformation))
            {
                // Application can take ownership of the socket and make any socket operation
                // For sample it is just transfering it back.
                socket = socketInformation.StreamSocket;
                socket.TransferOwnership(socketId);
                socket = null;
                Debug.WriteLine("Connecté");

            }
        }
        catch (Exception exception)
        {
            Debug.WriteLine(exception.Message);
        }

SocketActivityTask.cs:

namespace PhonieMarthaBackground
{
public sealed class SocketActivityTask : Windows.ApplicationModel.Background.IBackgroundTask
{
    private const string socketId = "SampleSocket";

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        var deferral = taskInstance.GetDeferral();
        try
        {
            var details = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
            var socketInformation = details.SocketInformation;
            switch (details.Reason)
            {
                case SocketActivityTriggerReason.SocketActivity:
                    var socket = socketInformation.StreamSocket;
                    DataReader reader = new DataReader(socket.InputStream);
                    reader.InputStreamOptions = InputStreamOptions.Partial;
                    await reader.LoadAsync(250);
                    var dataString = reader.ReadString(reader.UnconsumedBufferLength);
                    ShowToast(dataString);
                    socket.TransferOwnership(socketInformation.Id);
                    break;
                case SocketActivityTriggerReason.KeepAliveTimerExpired:
                    socket = socketInformation.StreamSocket;
                    DataWriter writer = new DataWriter(socket.OutputStream);
                    writer.WriteBytes(Encoding.UTF8.GetBytes("Keep alive"));
                    await writer.StoreAsync();
                    writer.DetachStream();
                    writer.Dispose();
                    socket.TransferOwnership(socketInformation.Id);
                    break;
                case SocketActivityTriggerReason.SocketClosed:
                    socket = new StreamSocket();
                    socket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.Wake);
                    if (ApplicationData.Current.LocalSettings.Values["hostname"] == null)
                    {
                        break;
                    }
                    var hostname = (String)ApplicationData.Current.LocalSettings.Values["hostname"];
                    var port = (String)ApplicationData.Current.LocalSettings.Values["port"];
                    await socket.ConnectAsync(new HostName(hostname), port);
                    socket.TransferOwnership(socketId);
                    break;
                default:
                    break;
            }
            deferral.Complete();
        }
        catch (Exception exception)
        {
            ShowToast(exception.Message);
            deferral.Complete();
        }
    }

    public void ShowToast(string text)
    {
        var toastNotifier = ToastNotificationManager.CreateToastNotifier();
        var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
        var textNodes = toastXml.GetElementsByTagName("text");
        textNodes.First().AppendChild(toastXml.CreateTextNode(text));
        var toastNotification = new ToastNotification(toastXml);
        toastNotifier.Show(new ToastNotification(toastXml));
    }

}
}

I would like to have a connection to the server and be able to send/receive data in the background so that I can perform the necessary actions for the proper functioning of the HMI.

ValentinDP
  • 323
  • 5
  • 14
  • A communication channel has a server/slave and client/master. So when sending you have the client send a message and then wait for a response from the client. So any asynchronous code at the client must wait for a complete response. With TCP the maximum size datagram is 1500 bytes and an router can split/combine datagrams during transmission. A TCP receive must wait for end of message before doing any processing. So every message must do one of following so receiver can wait for the end. 1) Add a End Of character 2) Add Byte Count to beginning of message 3) Send fixed size messages – jdweng Apr 23 '19 at 08:56
  • 1
    Looks like you're having the [same issue as this answer solves](https://stackoverflow.com/a/50550806/33051). – Zhaph - Ben Duguid Apr 23 '19 at 09:34

0 Answers0