locked
ASP.NET Web API message UWP application RRS feed

  • Question

  • User221513077 posted

    This is jut a general question.

    What's the best way to message a UWP application from a Web API  / Webservice application.?

    I need a webservice application to send messages to UWP applications, UWP applications do not need to send responses.  I can't use Azure.

    Thanks 

    Sunday, February 18, 2018 2:52 PM

Answers

  • User475983607 posted

    Yes this is running on intranet. And no you do not have to be in control of the network.  There all kinds of IoTs, Web Cams, car starters, thermostats that you can control from your phone or a computer.  It's a same thing, but a lot simpler, I just need a message nothing else. 

    You are making assumptions about the technology.

    I have a lot of experience in this field.  It is very easy to build devices that make requests. It is more complicated for the device to listen for messages.  Basically you need a message listener running on the device that accepts messages.  See UWP sockets reference docs.

    https://docs.microsoft.com/en-us/windows/uwp/networking/sockets

    The next problem finding the device.  A know static IP (and maybe port) is the easiest but not the most robust.   

    As Bruce said, you could use Web Sockets but web socket connections are established by the client and your design does not work this way.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 12:27 PM

All replies

  • User475983607 posted
    Web API (web apps) do not work like you think. Web API accepts a request and returns a response.
    Sunday, February 18, 2018 3:57 PM
  • User221513077 posted

    Computer A running UWP application sits and waits for a command.

    Computer B Calls Web API - And says send a message to one of your computers on your list.

    Web API program figures out what computer needs a message and sends a message to this UWP computer.

    Sunday, February 18, 2018 4:06 PM
  • User-474980206 posted
    Webapi does not support this. You either use signalr or have the UWP app poll the webapi for messages.
    Sunday, February 18, 2018 9:05 PM
  • User475983607 posted

    Computer A running UWP application sits and waits for a command.

    Computer B Calls Web API - And says send a message to one of your computers on your list.

    Web API program figures out what computer needs a message and sends a message to this UWP computer.

    That's only possible if you are in control of the network.  Computer A must have a socket server running (web server) and maybe DNS or port forwarding.  Again, it has to do with fundamentally how the web works. 

    Sunday, February 18, 2018 9:06 PM
  • User221513077 posted

    Yes this is running on intranet. And no you do not have to be in control of the network.  There all kinds of IoTs, Web Cams, car starters, thermostats that you can control from your phone or a computer.  It's a same thing, but a lot simpler, I just need a message nothing else.  I just don't know what is the option for UWP applications how Web API can talk to UWP application, WebSockets or something else. If I go to Nuget and try to download one of the WebSocket libraries in to the UWP application the compiler complains about wrong .NET framework, there must be a reason.  I am not looking for source code I just need someone to tell me you need to create THIS on Web Api and This On UWP app.  I can ask the same question on UWP forum, but then someone will tell me to go to ASP.NET forum.  But I will do that anyway.

    Thanks.

    Monday, February 19, 2018 1:40 AM
  • User-474980206 posted
    Webapi does not support websockets. You use signal/r for this support on the server. Unfortunately UWP does not have a supported client. There are source files on github, but you need to build the library yourself. You can write your own WebSocket server, see

    https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server

    Monday, February 19, 2018 3:19 AM
  • User475983607 posted

    Yes this is running on intranet. And no you do not have to be in control of the network.  There all kinds of IoTs, Web Cams, car starters, thermostats that you can control from your phone or a computer.  It's a same thing, but a lot simpler, I just need a message nothing else. 

    You are making assumptions about the technology.

    I have a lot of experience in this field.  It is very easy to build devices that make requests. It is more complicated for the device to listen for messages.  Basically you need a message listener running on the device that accepts messages.  See UWP sockets reference docs.

    https://docs.microsoft.com/en-us/windows/uwp/networking/sockets

    The next problem finding the device.  A know static IP (and maybe port) is the easiest but not the most robust.   

    As Bruce said, you could use Web Sockets but web socket connections are established by the client and your design does not work this way.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 12:27 PM
  • User221513077 posted

    Thanks for your help.

    I have a UWP application that needs to know when a record in a database table is added and the Web Api does the adding part, so I though maybe the same program can notify the UWP application. All of the applications are on intranet and the port and IP / Computer names are known. 

    I have tried the samples and those work, but only when I run the Client and the Server in the same application,  when I try the Client and the Server or different computers I get nothing - the client can not connect to the server, I tried turning off the fire wall, tried using port 80 and nothing  - connection times out on the client.  I will post the question about the Sample in the UWP forum.

    Thanks for your help

    Monday, February 19, 2018 3:14 PM
  • User-474980206 posted
    If it’s a internal app, the UWP could subscribe to the database notifications directly. If you want to use web sockets, both webapi and the UWP client connect to the WebSocket server. The webapi calls the WebSocket server to broadcast the change. The client listens for the broadcast.

    Monday, February 19, 2018 3:21 PM
  • User475983607 posted

    czupet

    Thanks for your help.

    I have a UWP application that needs to know when a record in a database table is added and the Web Api does the adding part, so I though maybe the same program can notify the UWP application. All of the applications are on intranet and the port and IP / Computer names are known. 

    I have tried the samples and those work, but only when I run the Client and the Server in the same application,  when I try the Client and the Server or different computers I get nothing - the client can not connect to the server, I tried turning off the fire wall, tried using port 80 and nothing  - connection times out on the client.  I will post the question about the Sample in the UWP forum.

    Thanks for your help

    It is not clear what you are calling a client and server. 

    Regardless of the technology, a device that receives asynchronous messages requires a listener as the device has no idea when a message will arrive.  The issue you are faced with has nothing to do with UWP or ASP.NET.  This more a TCP/UDP or networking question.  Once you understand the problem you'll be able to craft a solution using UWP.

    Monday, February 19, 2018 3:26 PM
  • User221513077 posted

    Computer A calls Web API (Computer B) 

    Computer B saves a record and Sends a message to Computer C - Which is running UWP application. - So Computer C is the Socket server.

    I have no problem with this scenario when Computer C is a Console Application or Windows Service application, but does not work with Computer C is UWP application.

    That's why I posted a question in this forum thinking maybe there's something in Web API application that has to be done in order to communicate with a UWP application.

     not necessarily using WebSockets but maybe some other way.

    Monday, February 19, 2018 3:37 PM
  • User475983607 posted

    If Computer C is truly running a socket server, then any device on the network can send a message.  

    Post the relevant code so we can stop guessing how you wired this up.

    Monday, February 19, 2018 4:25 PM
  • User-474980206 posted

    and as we keep saying, webapi can not notify a client app directly. it only listens for http requests. so UWP can call webapi. 

    for it to call a client machine (unless that machine is a known web server), it must call another service that has this feature. Your UWP program can poll (call every n seconds) for data, but there is no support in webapi to broadcast messages (notify a client). the microsoft solution is signal/r.

    if all your UWP applications are listing on a port and have dns names or fixed ipaddress (i.e., the UWP application is acting as a web server), then the webapi could use webclient to call the UWP application. the UWP would register its dns name and port number with the webapi, and on  db update the webapi server, would make web service call to every registered client. you would need to be sure the firewalls were open on the client. this is different than web socket, where the client opens and holds a connection to the web socket server.

     

    Monday, February 19, 2018 4:31 PM
  • User221513077 posted

    My original question was how do I message a UWP application from a Web API application?

    I though maybe a websocket or sokets will work, but I can't get even the UWP samples to work when on two different computers.  I will have to come up with something else beside polling the database.  Unfortunately I don't have time right now to do explore SignalR since I have never looked SignalR.

    You asked for source code,  The link you provided for the sample code for UWP does not work when the server and the client are not in the same application and are on different computers. The sample works when the Server and the Client are in the same Application and that does not do me any good.

    Thanks for your help

    Monday, February 19, 2018 4:59 PM
  • User475983607 posted

    My original question was how do I message a UWP application from a Web API application?

    And we explained several times why that is a problem.  Clearly you know better...

    czupet

    You asked for source code,  The link you provided for the sample code for UWP does not work when the server and the client are not in the same application and are on different computers. The sample works when the Server and the Client are in the same Application and that does not do me any good.

    Works for me...  Perhaps you need to open a firewall port.  We can only guess what could be wrong with the code or network configuration.  

    Try using a tool like WireShark.

    https://www.wireshark.org/

    Monday, February 19, 2018 6:42 PM
  • User221513077 posted

    Thanks for taking time to test it and letting me know. I appreciate it!

    That helped me to figure out the Sample program problem.

    I had 2 computers one was a Client and the second was the Server.

    when I ran both programs the Client would just time out.

    Then I switched the computers around - Made the Client Server and the Server Client. I ran the programs and got

    "Access denied. A network capability is required to access this network resource" on the second computer - I don't know why I did not get this error on the first computer.

    Once I checked Private Neworks (Client & Server) the Client worked on both computers.

    I don't know if that will help me with Web API talking to the UWP server, but at least I can try things out now.  Of course the best solution would be to subscribe to the database notifications directly, but I am connecting to DB2 running on Unix and I don't think DB2 supports that.

    Thanks

    Monday, February 19, 2018 7:16 PM
  • User221513077 posted

    Thank you for all your help!

    I got the program working using Sockets, it's not the most robust program, but I don't care right now, because it's just a demo and it's in a very controlled environment.

    The process is extremely fast, it's instantaneous. It feels like the Web Server is running locally.

    On the Web API (Web Server) I am running this code

    try
    {
    const int port = 1337;
    var ipep = new IPEndPoint(IPAddress.Parse(msg), port);
    var client = new TcpClient();

    client.Connect(ipep); 

    using (NetworkStream ns = client.GetStream())
    {
    using (var sw = new StreamWriter(ns))
    {
    sw.WriteLine(JsonConvert.SerializeObject(msg));
    sw.Flush();
    }
    }
    }
    catch (Exception e)
    {
    throw e;
    }

    and on the UWP program I am running

    private async void StartServer()
    {
    try
    {
    const string portNumber = "1337";

    var streamSocketListener = new Windows.Networking.Sockets.StreamSocketListener();

    // The ConnectionReceived event is raised when connections are received.
    streamSocketListener.ConnectionReceived += StreamSocketListener_ConnectionReceived;

    // Start listening for incoming TCP connections on the specified port. You can specify any port that's not currently in use.
    await streamSocketListener.BindServiceNameAsync(portNumber);

    }
    catch (Exception ex)
    {
    Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
    DriverCSRNotification.Text = webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message;
    }
    }

    private async void StreamSocketListener_ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender, Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args)
    {
    string request;
    using (var streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead()))
    {
    request = await streamReader.ReadLineAsync();
    }

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => DriverNotification.Text = request);

    }

    Tuesday, February 20, 2018 5:27 AM
  • User221513077 posted

    And we explained several times why that is a problem.  Clearly you know better...

    There is no problem, just a solution. I do not understand why you think it's a problem - just because you don't know the answer.

    Don't get me wrong I do appreciate your input and your time, after all it was I who came to this forum looking for an answer. But when you guys the 'experts' and I don't mean in derogatory way, you guys are truly the experts, tell me there is no way to do something it's either you don't want to take time to look for answer or more likely I have not explained myself correctly.  After all I have found the exact answer I was looking for with your guys help and the solution was extremely simple. 

    So yes in this case I do know better, because there is a solution and it's very simple.  Of course every question is easy if you know the answer. 

    Peter

    Thank You

    Tuesday, February 20, 2018 5:51 AM
  • User475983607 posted

    There is no problem, just a solution. I do not understand why you think it's a problem - just because you don't know the answer.

    Don't get me wrong I do appreciate your input and your time, after all it was I who came to this forum looking for an answer. But when you guys the 'experts' and I don't mean in derogatory way, you guys are truly the experts, tell me there is no way to do something it's either you don't want to take time to look for answer or more likely I have not explained myself correctly.  After all I have found the exact answer I was looking for with your guys help and the solution was extremely simple. 

    So yes in this case I do know better, because there is a solution and it's very simple.  Of course every question is easy if you know the answer. 

    Some day in the future you'll understand what we're talking about but until then I'm glad you figured out how to craft a client within Web API.

    Tuesday, February 20, 2018 11:24 AM