eaglei22
eaglei22

Reputation: 2831

How to fire off SignalR hub from external application (that doesn't use SignalR)

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.

Upvotes: 0

Views: 437

Answers (1)

William Xifaras
William Xifaras

Reputation: 5312

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))
   {
      ...
   }
}

Upvotes: 1

Related Questions