0

Task:

  • Add some data to database - approx 5 minutes
  • Send a notification to client "Data added to database"
  • Process data - approx 15 minutes
  • Send a notification to client "The data is processed"

In code:

ASMX web services

[SoapDocumentMethod(OneWay = true)]
[WebMethod]
public void AddAndProcess(DataSet _DataToProcess)
{
    //inserts data to DB

    SendNotification("Data added to database");

    ProcessData(_DataToProcess);
}

[SoapDocumentMethod(OneWay = true)]
[WebMethod]
public void ProcessData(DataSet _DataToProcess)
{
    //Process data

    SendNotification("The data is processed");
}

public void SendNotification(string NotificationMessage)
{
    //do something to send a notification to client
}

ASP.NET MVC View

@using (Html.BeginForm("AddAndProcess", "DataProcessor", FormMethod.Post, new {@class = "form-horizontal", role = "form", enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()

<h1>Upload data file</h1>

    <div class="form-group">
        <div class="col-md-10">
            @Html.Label("Select data file", new { @class = "col-md-4 control-label" })
            @Html.TextBox("file", null, new { type = "file" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @Html.TextBox("Submit", "Process", new { type = "submit" })
        </div>
    </div>
    }

Data processor controller

public class DataProcessor : Controller
{
    public ActionResult AddAndProcess()
    {
        //Call data processor web services to 
        //1. Add some data to database - approx 5 minutes
        //2. Send a notification to client "Data added to database"
        //3. Process data - approx 15 minutes
        //4. Send a notification to client "The data is processed"
        return View();
    }
}

Description:

I have an ASP.NET MVC view on which I need to show function execution status notification as shown above.

To save our user's time, the web services are marked SoapDocumentMethod(OneWay = true). In this case I cannot return a NotificationMessage string and show on view.

Problem:

How to send a notification from ASMX web services to ASP.NET MVC view?

Community
  • 1
  • 1
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • 3
    Why use ASMX? Why not just expose and endpoint via MVC, or Web API? And if you are trying to push notifications from server to client in ASP.NET, [SignalR](http://signalr.net/) is probably your best bet. – mason Dec 04 '15 at 14:01
  • 1
    I linked to SignalR's in my comment. There's lots of documentation there. I think there's enough there that you can take a stab at it and then come back to SO if you get stuck on a specific part of your implementation. – mason Dec 04 '15 at 14:22
  • Yeah sure @mason! [This](http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc) will be a new learning. Since this is new for me, I was actually unable to *frame the solution for the problem*. I'll get back after doing some homework. – Zameer Ansari Dec 04 '15 at 14:38
  • 1
    ASMX cannot *send* data to a client. The client can *retrieve* data from ASMX. So if you were deadset on using ASMX, you'd end up having the client constantly poll the ASMX to say "hey, send me all the notifications". And 99% of the time, the ASMX will say "sorry, I got nothing for you". Whereas SignalR, the server can tap the client on the shoulder and say "hey, here's some notifications." Much less chatty, and the code becomes closer to the diagram of how the message should flow from server to client. – mason Dec 04 '15 at 17:11
  • Thanks @mason - I'm still trying to understand this **SignalR** thing. I also agree that `asmx` is old lagacy. I'm planning to learn and implement either `WebAPI` or `WCF` for taking care of `asmx` functionality. Btw, which is better in both? – Zameer Ansari Dec 04 '15 at 17:29
  • 2
    Web API is in ASP.NET 5. WCF isn't. So you can see which one seems to get more attention. But it also depends on what you need it for. Web API can do JSON and XML over HTTP, but WCF is much more flexible in formats and transports. If you don't need all that functionality, I'd go with Web API. – mason Dec 04 '15 at 17:34

0 Answers0