2
It is quite simple, this code works, but only once, after the first time it is started no other request is answered, as if after it runs once the Streamsocketlistener stops.
public void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
var detalhes = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
if (detalhes.SocketInformation.SocketKind == SocketActivityKind.StreamSocketListener &&
detalhes.Reason == SocketActivityTriggerReason.ConnectionAccepted)
{
var list = detalhes.SocketInformation.StreamSocketListener;
list.ConnectionReceived += async (sender, args) =>
{
using (DataWriter escritor = new DataWriter(args.Socket.OutputStream))
{
escritor.WriteString("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nOK");
await escritor.StoreAsync();
}
};
}
deferral.Complete();
}
Background job logging is done with this code:
private void Registrar()
{
var tasks = BackgroundTaskRegistration.AllTasks;
int quant = tasks.Values.Count(x => x.Name == "SocketActivityBackgroundTask");
if (quant == 1)
{
task = tasks.Values.Single(x => x.Name == "SocketActivityBackgroundTask");
}
else
{
var socketTaskBuilder = new BackgroundTaskBuilder();
socketTaskBuilder.Name = "SocketActivityBackgroundTask";
socketTaskBuilder.TaskEntryPoint = "SocketActivityBackgroundTask.SocketActivityTask";
var trigger = new SocketActivityTrigger();
socketTaskBuilder.SetTrigger(trigger);
task = socketTaskBuilder.Register();
}
}
private async Task Iniciar()
{
var sockets = SocketActivityInformation.AllSockets;
if (!sockets.Keys.Contains(socketId))
{
StreamSocketListener socket = new StreamSocketListener();
socket.EnableTransferOwnership(task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
await socket.BindServiceNameAsync(serverPort);
await Task.Delay(500);
await socket.CancelIOAsync();
socket.TransferOwnership(socketId);
}
}
The record works and the task goes to the background, but it only works once, and this is the current problem, because the previous one was solved but this one was created.
Does it not mean "Create a SERVICE" ?
– Renato Afonso
It’s just that I use Streamsocketlistener to host a web page, so that’s why I call it a server.
– Jaedson Barbosa
Now the question is clear enough?
– Jaedson Barbosa
Enter the code of how the server/service call/record is made
– rubStackOverflow
@rubStackOverflow Ready, need some more detail?
– Jaedson Barbosa
Now it’s easier to understand and try to identify the problem.
– rubStackOverflow