Calling Web-Api from a SignalR Hub
I am creating a WebApi server with integrated SignalR Hubs. For simplicity's sake I am using a Controller which is operating on a List
.
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
public static List<string> Source { get; set; } = new List<string>();
public static int counter = 0;
private IHubContext<ValuesHub, IValuesClient> hubContext;
public ValuesController(IHubContext<ValuesHub, IValuesClient> hub)
{
Source.Add("bla" + counter);
counter++;
Source.Add("bla" + counter);
counter++;
this.hubContext = hub;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return Source;
}
// GET api/values/x
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return Source[id];
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
Source.Add(value);
}
// PUT api/values/x
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
Source[id] = value;
}
// DELETE api/values/x
[HttpDelete("{id}")]
public void Delete(int id)
{
var item = Source[id];
Source.Remove(item);
Console.WriteLine("Outgoing message!");
hubContext.Clients.All.ReceiveMessage("Message incoming", "Blaaaaa");
}
}
}
My Hub doesn't do anything special yet:
public interface IValuesClient
{
Task ReceiveMessage(string value, string message);
Task ReceiveMessage(string message);
}
public class ValuesHub : Hub<IValuesClient>
{
// private static ValuesController ctrl = Glo
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Also for simplicity's sake I will not go into detail why I want to achieve this, but I want the server to wait for a certain amount of time and then delete the according value, after a disconnection is detected. Let's say I want to simply delete the first element in my Source
list.
How would I access the according Controller-functions from inside my OnDisconnectedAsync
function?
One idea I came up with is to create a HttpClient
inside my Hub and let the Hub act as a client here by calling e. g. DELETE: http://localhost:5000/api/values/0
. I have to admit this sounds like a rather horrible approach, though.
.net-core asp.net-core-webapi signalr-hub asp.net-core-signalr
add a comment |
I am creating a WebApi server with integrated SignalR Hubs. For simplicity's sake I am using a Controller which is operating on a List
.
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
public static List<string> Source { get; set; } = new List<string>();
public static int counter = 0;
private IHubContext<ValuesHub, IValuesClient> hubContext;
public ValuesController(IHubContext<ValuesHub, IValuesClient> hub)
{
Source.Add("bla" + counter);
counter++;
Source.Add("bla" + counter);
counter++;
this.hubContext = hub;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return Source;
}
// GET api/values/x
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return Source[id];
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
Source.Add(value);
}
// PUT api/values/x
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
Source[id] = value;
}
// DELETE api/values/x
[HttpDelete("{id}")]
public void Delete(int id)
{
var item = Source[id];
Source.Remove(item);
Console.WriteLine("Outgoing message!");
hubContext.Clients.All.ReceiveMessage("Message incoming", "Blaaaaa");
}
}
}
My Hub doesn't do anything special yet:
public interface IValuesClient
{
Task ReceiveMessage(string value, string message);
Task ReceiveMessage(string message);
}
public class ValuesHub : Hub<IValuesClient>
{
// private static ValuesController ctrl = Glo
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Also for simplicity's sake I will not go into detail why I want to achieve this, but I want the server to wait for a certain amount of time and then delete the according value, after a disconnection is detected. Let's say I want to simply delete the first element in my Source
list.
How would I access the according Controller-functions from inside my OnDisconnectedAsync
function?
One idea I came up with is to create a HttpClient
inside my Hub and let the Hub act as a client here by calling e. g. DELETE: http://localhost:5000/api/values/0
. I have to admit this sounds like a rather horrible approach, though.
.net-core asp.net-core-webapi signalr-hub asp.net-core-signalr
Depending on how much time you need to wait, and how you handle await async you could be holding up a thread not good IMHO, better to call it when your ready, why is using HTTP client horrible ?
– saj
Nov 21 '18 at 17:16
add a comment |
I am creating a WebApi server with integrated SignalR Hubs. For simplicity's sake I am using a Controller which is operating on a List
.
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
public static List<string> Source { get; set; } = new List<string>();
public static int counter = 0;
private IHubContext<ValuesHub, IValuesClient> hubContext;
public ValuesController(IHubContext<ValuesHub, IValuesClient> hub)
{
Source.Add("bla" + counter);
counter++;
Source.Add("bla" + counter);
counter++;
this.hubContext = hub;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return Source;
}
// GET api/values/x
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return Source[id];
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
Source.Add(value);
}
// PUT api/values/x
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
Source[id] = value;
}
// DELETE api/values/x
[HttpDelete("{id}")]
public void Delete(int id)
{
var item = Source[id];
Source.Remove(item);
Console.WriteLine("Outgoing message!");
hubContext.Clients.All.ReceiveMessage("Message incoming", "Blaaaaa");
}
}
}
My Hub doesn't do anything special yet:
public interface IValuesClient
{
Task ReceiveMessage(string value, string message);
Task ReceiveMessage(string message);
}
public class ValuesHub : Hub<IValuesClient>
{
// private static ValuesController ctrl = Glo
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Also for simplicity's sake I will not go into detail why I want to achieve this, but I want the server to wait for a certain amount of time and then delete the according value, after a disconnection is detected. Let's say I want to simply delete the first element in my Source
list.
How would I access the according Controller-functions from inside my OnDisconnectedAsync
function?
One idea I came up with is to create a HttpClient
inside my Hub and let the Hub act as a client here by calling e. g. DELETE: http://localhost:5000/api/values/0
. I have to admit this sounds like a rather horrible approach, though.
.net-core asp.net-core-webapi signalr-hub asp.net-core-signalr
I am creating a WebApi server with integrated SignalR Hubs. For simplicity's sake I am using a Controller which is operating on a List
.
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
public static List<string> Source { get; set; } = new List<string>();
public static int counter = 0;
private IHubContext<ValuesHub, IValuesClient> hubContext;
public ValuesController(IHubContext<ValuesHub, IValuesClient> hub)
{
Source.Add("bla" + counter);
counter++;
Source.Add("bla" + counter);
counter++;
this.hubContext = hub;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return Source;
}
// GET api/values/x
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return Source[id];
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
Source.Add(value);
}
// PUT api/values/x
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
Source[id] = value;
}
// DELETE api/values/x
[HttpDelete("{id}")]
public void Delete(int id)
{
var item = Source[id];
Source.Remove(item);
Console.WriteLine("Outgoing message!");
hubContext.Clients.All.ReceiveMessage("Message incoming", "Blaaaaa");
}
}
}
My Hub doesn't do anything special yet:
public interface IValuesClient
{
Task ReceiveMessage(string value, string message);
Task ReceiveMessage(string message);
}
public class ValuesHub : Hub<IValuesClient>
{
// private static ValuesController ctrl = Glo
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Also for simplicity's sake I will not go into detail why I want to achieve this, but I want the server to wait for a certain amount of time and then delete the according value, after a disconnection is detected. Let's say I want to simply delete the first element in my Source
list.
How would I access the according Controller-functions from inside my OnDisconnectedAsync
function?
One idea I came up with is to create a HttpClient
inside my Hub and let the Hub act as a client here by calling e. g. DELETE: http://localhost:5000/api/values/0
. I have to admit this sounds like a rather horrible approach, though.
.net-core asp.net-core-webapi signalr-hub asp.net-core-signalr
.net-core asp.net-core-webapi signalr-hub asp.net-core-signalr
asked Nov 21 '18 at 15:26
indexoutofboundsindexoutofbounds
325222
325222
Depending on how much time you need to wait, and how you handle await async you could be holding up a thread not good IMHO, better to call it when your ready, why is using HTTP client horrible ?
– saj
Nov 21 '18 at 17:16
add a comment |
Depending on how much time you need to wait, and how you handle await async you could be holding up a thread not good IMHO, better to call it when your ready, why is using HTTP client horrible ?
– saj
Nov 21 '18 at 17:16
Depending on how much time you need to wait, and how you handle await async you could be holding up a thread not good IMHO, better to call it when your ready, why is using HTTP client horrible ?
– saj
Nov 21 '18 at 17:16
Depending on how much time you need to wait, and how you handle await async you could be holding up a thread not good IMHO, better to call it when your ready, why is using HTTP client horrible ?
– saj
Nov 21 '18 at 17:16
add a comment |
1 Answer
1
active
oldest
votes
So If I understand your problem is that you are having is that you want to access the methods on the controller from your hubs?
If this is the case - It seems to me that you have a fundamental design flaw. I would create a service that handles all the things your controller is doing, and then inject this service directly into the hub. Then you can use that service directly in the hub on the overrides and operate on your list . If this is unclear I can Provide an example.
public class ValuesHub : Hub<IValuesClient>
{
IListService _listService;
public ValuesHub (IListService listService)
{
_listService = listService;
}
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
//Call your methods here.
_listService.RemoveFirstElement();
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Thats your hub - See service example below
public class ListService : IListService
{
public void RemoveFirstElement()
{
//Delete Your Element here
}
}
public interface IListService
{
void RemoveFirstElement();
}
And then your startup.cs
services.AddSingleton<IListService,ListService>();
Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 '18 at 14:56
Ive added how your hub would look.
– cl0ud
Nov 22 '18 at 14:58
@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 '18 at 15:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415327%2fcalling-web-api-from-a-signalr-hub%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So If I understand your problem is that you are having is that you want to access the methods on the controller from your hubs?
If this is the case - It seems to me that you have a fundamental design flaw. I would create a service that handles all the things your controller is doing, and then inject this service directly into the hub. Then you can use that service directly in the hub on the overrides and operate on your list . If this is unclear I can Provide an example.
public class ValuesHub : Hub<IValuesClient>
{
IListService _listService;
public ValuesHub (IListService listService)
{
_listService = listService;
}
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
//Call your methods here.
_listService.RemoveFirstElement();
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Thats your hub - See service example below
public class ListService : IListService
{
public void RemoveFirstElement()
{
//Delete Your Element here
}
}
public interface IListService
{
void RemoveFirstElement();
}
And then your startup.cs
services.AddSingleton<IListService,ListService>();
Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 '18 at 14:56
Ive added how your hub would look.
– cl0ud
Nov 22 '18 at 14:58
@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 '18 at 15:07
add a comment |
So If I understand your problem is that you are having is that you want to access the methods on the controller from your hubs?
If this is the case - It seems to me that you have a fundamental design flaw. I would create a service that handles all the things your controller is doing, and then inject this service directly into the hub. Then you can use that service directly in the hub on the overrides and operate on your list . If this is unclear I can Provide an example.
public class ValuesHub : Hub<IValuesClient>
{
IListService _listService;
public ValuesHub (IListService listService)
{
_listService = listService;
}
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
//Call your methods here.
_listService.RemoveFirstElement();
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Thats your hub - See service example below
public class ListService : IListService
{
public void RemoveFirstElement()
{
//Delete Your Element here
}
}
public interface IListService
{
void RemoveFirstElement();
}
And then your startup.cs
services.AddSingleton<IListService,ListService>();
Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 '18 at 14:56
Ive added how your hub would look.
– cl0ud
Nov 22 '18 at 14:58
@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 '18 at 15:07
add a comment |
So If I understand your problem is that you are having is that you want to access the methods on the controller from your hubs?
If this is the case - It seems to me that you have a fundamental design flaw. I would create a service that handles all the things your controller is doing, and then inject this service directly into the hub. Then you can use that service directly in the hub on the overrides and operate on your list . If this is unclear I can Provide an example.
public class ValuesHub : Hub<IValuesClient>
{
IListService _listService;
public ValuesHub (IListService listService)
{
_listService = listService;
}
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
//Call your methods here.
_listService.RemoveFirstElement();
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Thats your hub - See service example below
public class ListService : IListService
{
public void RemoveFirstElement()
{
//Delete Your Element here
}
}
public interface IListService
{
void RemoveFirstElement();
}
And then your startup.cs
services.AddSingleton<IListService,ListService>();
So If I understand your problem is that you are having is that you want to access the methods on the controller from your hubs?
If this is the case - It seems to me that you have a fundamental design flaw. I would create a service that handles all the things your controller is doing, and then inject this service directly into the hub. Then you can use that service directly in the hub on the overrides and operate on your list . If this is unclear I can Provide an example.
public class ValuesHub : Hub<IValuesClient>
{
IListService _listService;
public ValuesHub (IListService listService)
{
_listService = listService;
}
public override async Task OnConnectedAsync()
{
await Groups.AddToGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client connected - Client-Id: {0}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Users");
Console.WriteLine("Client disconnected - Client-Id: {0}", Context.ConnectionId);
Console.WriteLine("Disconnection due to: {0}", exception);
//Call your methods here.
_listService.RemoveFirstElement();
await base.OnDisconnectedAsync(exception);
}
public async Task MessageToAll(string user, string message)
{
Console.WriteLine("SendMessage - User: {0} - Message: {1}", user, message);
await Clients.All.ReceiveMessage(user, message);
}
public async Task MessageToCaller(string message)
{
Console.WriteLine("SendMessageToCaller: {0}", message);
await Clients.Caller.ReceiveMessage(message);
}
}
}
Thats your hub - See service example below
public class ListService : IListService
{
public void RemoveFirstElement()
{
//Delete Your Element here
}
}
public interface IListService
{
void RemoveFirstElement();
}
And then your startup.cs
services.AddSingleton<IListService,ListService>();
edited Nov 22 '18 at 15:05
answered Nov 22 '18 at 14:52
cl0udcl0ud
379112
379112
Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 '18 at 14:56
Ive added how your hub would look.
– cl0ud
Nov 22 '18 at 14:58
@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 '18 at 15:07
add a comment |
Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 '18 at 14:56
Ive added how your hub would look.
– cl0ud
Nov 22 '18 at 14:58
@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 '18 at 15:07
Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 '18 at 14:56
Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 '18 at 14:56
Ive added how your hub would look.
– cl0ud
Nov 22 '18 at 14:58
Ive added how your hub would look.
– cl0ud
Nov 22 '18 at 14:58
@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 '18 at 15:07
@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 '18 at 15:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415327%2fcalling-web-api-from-a-signalr-hub%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Depending on how much time you need to wait, and how you handle await async you could be holding up a thread not good IMHO, better to call it when your ready, why is using HTTP client horrible ?
– saj
Nov 21 '18 at 17:16