Background StreamSocketListener just run one time

1

I have a StreamSocketListener registered in a background task.

When it receives a request the Run method starts.

The first time it runs well, the second time I get that error:

O identificador de objeto não representa um objeto válido. (Exception from HRESULT: 0x800710D8)

The complete code is:

public async void Run(IBackgroundTaskInstance taskInstance)
{
    var deferral = taskInstance.GetDeferral();
    var details = (SocketActivityTriggerDetails)taskInstance.TriggerDetails;
    var socketInformation = details.SocketInformation;
    try
    {
        ShowToast(details.Reason.ToString());
        if (details.Reason == SocketActivityTriggerReason.ConnectionAccepted &&
            socketInformation.SocketKind == SocketActivityKind.StreamSocketListener)
        {
            using (IOutputStream output = socketInformation.StreamSocket.OutputStream)
            {
                string str = "Tudo certo\r\n";
                var conteudo = $"HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: {str.Length}\r\n\r\n{str}";
                using (Stream stream = output.AsStreamForWrite())
                {
                    var bodyArray = Encoding.UTF8.GetBytes(conteudo);
                    stream.Write(bodyArray, 0, bodyArray.Length);
                }
            }
        }
    }
    catch (Exception e)
    {
        ShowToast(e.Message);
        StreamSocketListener socket = new StreamSocketListener();
        socket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
        await socket.BindServiceNameAsync("8080");
        await socket.CancelIOAsync();
        socket.TransferOwnership("Teste");
    }
    deferral.Complete();
}
c#
uwp
asked on Stack Overflow Feb 25, 2017 by Jaedson Barbosa • edited Feb 28, 2017 by Dr Rob Lang

1 Answer

1

According to the remarks of TransferOwnership method of StreamSocket class:

Your app should call this method to transfer ownership of the StreamSocket to the socket brokering service when the app is about to be suspended, or at the end of a background task

So at the end of your background you should call TransferOwnership method. You only call it in catch block, if your code snippet try succesfully it will not invoke TransferOwnership. You should be able also invoke the method at the last of try block or after try and catch finally to invoke this method to ensure transfer ownership of the StreamSocket to the socket brokering service.

I reproduced your issue on my side and resolved with adding socket.TransferOwnership("Teste"); to the last of try block .

try
{
    ShowToast(details.Reason.ToString());
    if (details.Reason == SocketActivityTriggerReason.ConnectionAccepted &&
        socketInformation.SocketKind == SocketActivityKind.StreamSocketListener)
    {
        using (IOutputStream output = socketInformation.StreamSocket.OutputStream)
        {
            string str = "Tudo certo\r\n";
            var conteudo = $"HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: {str.Length}\r\n\r\n{str}";
            using (Stream stream = output.AsStreamForWrite())
            {
                var bodyArray = Encoding.UTF8.GetBytes(conteudo);
                stream.Write(bodyArray, 0, bodyArray.Length);
            }
        }
    }
    socket.TransferOwnership("Teste");
}

More details please reference the official sample.

answered on Stack Overflow Feb 28, 2017 by Sunteen Wu

User contributions licensed under CC BY-SA 3.0