Calling Web-Api from a SignalR Hub











up vote
0
down vote

favorite
1












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.










share|improve this question






















  • 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 at 17:16















up vote
0
down vote

favorite
1












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.










share|improve this question






















  • 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 at 17:16













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 15:26









indexoutofbounds

315221




315221












  • 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 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 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 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 at 17:16












1 Answer
1






active

oldest

votes

















up vote
1
down vote













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>();





share|improve this answer























  • Thanks for the answe, I would be very glad if you could provide an example.
    – indexoutofbounds
    Nov 22 at 14:56










  • Ive added how your hub would look.
    – cl0ud
    Nov 22 at 14:58










  • @indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
    – cl0ud
    Nov 22 at 15:07











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',
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
});


}
});














 

draft saved


draft discarded


















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








up vote
1
down vote













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>();





share|improve this answer























  • Thanks for the answe, I would be very glad if you could provide an example.
    – indexoutofbounds
    Nov 22 at 14:56










  • Ive added how your hub would look.
    – cl0ud
    Nov 22 at 14:58










  • @indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
    – cl0ud
    Nov 22 at 15:07















up vote
1
down vote













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>();





share|improve this answer























  • Thanks for the answe, I would be very glad if you could provide an example.
    – indexoutofbounds
    Nov 22 at 14:56










  • Ive added how your hub would look.
    – cl0ud
    Nov 22 at 14:58










  • @indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
    – cl0ud
    Nov 22 at 15:07













up vote
1
down vote










up vote
1
down vote









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>();





share|improve this answer














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>();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 15:05

























answered Nov 22 at 14:52









cl0ud

30912




30912












  • Thanks for the answe, I would be very glad if you could provide an example.
    – indexoutofbounds
    Nov 22 at 14:56










  • Ive added how your hub would look.
    – cl0ud
    Nov 22 at 14:58










  • @indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
    – cl0ud
    Nov 22 at 15:07


















  • Thanks for the answe, I would be very glad if you could provide an example.
    – indexoutofbounds
    Nov 22 at 14:56










  • Ive added how your hub would look.
    – cl0ud
    Nov 22 at 14:58










  • @indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
    – cl0ud
    Nov 22 at 15:07
















Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 at 14:56




Thanks for the answe, I would be very glad if you could provide an example.
– indexoutofbounds
Nov 22 at 14:56












Ive added how your hub would look.
– cl0ud
Nov 22 at 14:58




Ive added how your hub would look.
– cl0ud
Nov 22 at 14:58












@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 at 15:07




@indexoutofbounds please also see this . docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/…
– cl0ud
Nov 22 at 15:07


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks