0

I have an Asp.Net MVC 4 application (Application A) that will use SignalR for real time updates to users. I am using SignalR version 1.1.4 due to older .net framework version we are working with.

There is an external application (Application B) that when an order is submitted, I want to notify application A to send a notification of order.

My initial thoughts are to either create an ASP.NET Web Service to host SignalR, but because application B will not use SignalR I figure that just making a call to the controller of application A passing necessary data will work for what is needed.

So, from application B, how would I call application A's controller to pass the data necessary? Can Ajax be used to make a call to an external app? If so, what would the controller method look like?

These are both intranet applications with windows authentication.

eaglei22
  • 2,589
  • 1
  • 38
  • 53
  • Sounds like you need a message queue (any kind will do). – Matt Johnson-Pint May 03 '17 at 20:22
  • @MattJohnson, Thanks Matt. That will be a bit of a learning curve for me, so I will try William's first. But thank you for introducing me to that topic as it's definitely something to look into for the future. – eaglei22 May 04 '17 at 13:01

1 Answers1

1

I figure that just making a call to the controller of application A passing necessary data will work for what is needed

You can use the HttpClient in Application B to call a Controller Action in Application A.

Example of creating an Order and sending the order to another MVC application controller (not tested).

private HttpClient client = new HttpClient();

public HomeController()
{
    client.BaseAddress = new Uri("http://localhost:49277");
    client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));
}

[HttpPost]
public ActionResult Insert(Order order)
{
     HttpResponseMessage response = client.PostAsJsonAsync<Order>("/Order/Post" + $"/{order.OrderID}", order).Result;
     return RedirectToAction("Index");
}

EDIT

You can use - UseDefaultCrendentials or pass the credentials directly.

using (var handler = new HttpClientHandler {UseDefaultCredentials = true})
{
   using (var client = new HttpClient(handler))
   {
    ...
   }
}       

OR

var credentials = new NetworkCredential(userName, password);
using (var handler = new HttpClientHandler {Credentials = credentials })
{
   using (var client = new HttpClient(handler))
   {
      ...
   }
}
William Xifaras
  • 5,212
  • 2
  • 19
  • 21
  • Perfect, thank you. When this happens, this will use the user's windows account for authentication correct? This is an intranet application where the user is originally authenticated using windows authentication. – eaglei22 May 04 '17 at 13:03
  • Is there anything different I should be doing if I just want to pass a string? Using here, "https://weblog.west-wind.com/posts/2012/Mar/21/ASPNET-Web-API-and-Simple-Value-Parameters-from-POSTed-data" as a reference I tried [FromBody] the call to method, and also passed the string and I keep getting null. – eaglei22 May 04 '17 at 19:17
  • I wasn't able to open that link. – William Xifaras May 04 '17 at 19:33
  • https://weblog.west-wind.com/posts/2012/Mar/21/ASPNET-Web-API-and-Simple-Value-Parameters-from-POSTed-data – eaglei22 May 04 '17 at 19:34
  • How are you passing the string? Take a look at the following https://stackoverflow.com/questions/15176538/net-httpclient-how-to-post-string-value/15176685 and https://blog.jayway.com/2012/03/13/httpclient-makes-get-and-post-very-simple/ – William Xifaras May 04 '17 at 19:44
  • Like this, "client.PostAsJsonAsync(requestUri, "Testing").Result;" and okay, I will look at those. – eaglei22 May 04 '17 at 19:49
  • Also, use await vs. Result. Result is a blocking call and can cause deadlocks. Just await the call: var response = await client.PostAsJsonAsync ... – William Xifaras May 04 '17 at 19:53
  • I was going to, but we are only on 4.0, and using visual studio 2010. I threw it in a separate thread though. – eaglei22 May 04 '17 at 19:55
  • Thank you, I was able to get it working with the stackoverflow link you suggested. – eaglei22 May 04 '17 at 20:22