이 문제를 해결하고 해결책을 찾았습니다.
시작시 SignalR 구성.웹 API에서 웹 API 웹 API 추가 허브에서
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR("/signalr", new Microsoft.AspNet.SignalR.HubConfiguration());
}
}
public class ServiceStatusHub : Hub
{
private static IHubContext hubContext =
GlobalHost.ConnectionManager.GetHubContext<ServiceStatusHub>();
public static void GetStatus(string message)
{
hubContext.Clients.All.acknowledgeMessage(message);
}
}
의 CS 활동 방법
public IEnumerable<string> Get()
{
// Query service to check status
ServiceStatusHub.GetStatus("Please check status of the LDAP!");
return new string[] { "val1", "val2" };
}
콘솔 응용 프로그램에서 지금 우체부의 웹 API를 실행 Program.cs 클래스
class Program
{
static void Main(string[] args)
{
var client = new SignalRMasterClient("http://localhost:9321/signalr");
// Send message to server.
client.SayHello("Message from client to Server!");
Console.ReadKey();
// Stop connection with the server to immediately call "OnDisconnected" event
// in server hub class.
client.Stop();
}
}
에서 SignalR 클라이언트에게
public class SignalRMasterClient
{
public string Url { get; set; }
public HubConnection Connection { get; set; }
public IHubProxy Hub { get; set; }
public SignalRMasterClient(string url)
{
Url = url;
Connection = new HubConnection(url, useDefaultUrl: false);
Hub = Connection.CreateHubProxy("ServiceStatusHub");
Connection.Start().Wait();
Hub.On<string>("acknowledgeMessage", (message) =>
{
Console.WriteLine("Message received: " + message);
/// TODO: Check status of the LDAP
/// and update status to Web API.
});
}
public void SayHello(string message)
{
Hub.Invoke("hello", message);
Console.WriteLine("hello method is called!");
}
public void Stop()
{
Connection.Stop();
}
}
를 추가하고 또한 콘솔 응용 프로그램을 실행합니다. 양방향 의사 소통이 설정됩니다.
참고 : 아래 코드는 콘솔을 닫았을 때 즉시 OnDisconnected 이벤트를 트리거하지 않았을 때의 문제를 해결합니다. 답장을 보내
public void Stop()
{
Connection.Stop();
}
Check the image showing result.
안녕 프레드 한, 감사합니다. Windows 서비스가 다운되면 웹 API가 어떻게 알 수 있습니까? –