Signalr has no connetions - asp.net-web-api

I'm trying to use signalr in ApiController that handles upload from a page.
Essentially I am invoking my Hub from my uploadController and then I want to talk to my client.
shortened down the upload controller looks like this:
public class UploadController : ApiController
{
public Task<HttpResponseMessage> PostFile()
{
var hubMan = new HubManager();
hubMan.showUpload("test");
}
}
I then have my HubManager that should take care of sending to my client:
public class HubManager : Hub
{
public HubManager()
{
}
public void showUpload(string src)
{
Caller.showUpload(src);
}
}
I also tried this in my showUpload:
public void showUpload(string str)
{
var context = GlobalHost.ConnectionManager.GetHubContext<HubManager>();
context.Clients[this.Context.ConnectionId].showUpload(str);
}
My client side code looks like this:
$(document).ready(function () {
var progress = $.connection('/signalr/hubs/hubManager');
progress.showUpload = function (src) {
alert(src);
};
// Start the connection
$.connection.hub.start();
});
Now the problem is that in my Hub class everything is Null.
My Caller,Clients and Context is null. So it seems something is not initialising properly.
Any suggestions?

You cannot create an instance of a SignalR hub yourself. Hubs need to be initialized by SignalR so the properties like Clients etc. are available.
If you want to broadcast to clients from outside the hub (e.g. your controller), you need to use the context object of the hub as described in the wiki:
public ActionResult ControllerAction()
{
var context = GlobalHost.ConnectionManager.GetHubContext<HubManager>();
context.Clients[ /* connectionId or group name */ ].showUpload();
// ...
}
Please note that you can't access context.ConnectionId or context.Caller there, because you are calling your ApiController and not SignalR, so the framework knows nothing about the ConnectionId in this case. You need to pass it to the controller in some other way, e.g. cookies or as a parameter of the controller action.

Related

How to use CXCallObserver in Xamarin?

I need to subscribe an event to handle incoming phone call. Since iOS version 11.0 CTCallCenter is deprecated we have to use CXCallObserver. I successfully implemented solution for CTCallCenter, but I am not able to subscribe event for CXCallObserver. Does anyone have working solution for CXCallObserver?
Here is my code to subscribe event for CTCallCenter..
_callCenter = new CTCallCenter();
_callCenter.CallEventHandler += CallEvent;
private void CallEvent(CTCall call)
{
CoreFoundation.DispatchQueue.MainQueue.DispatchSync(() =>
{
if(call.CallState.Equals(call.StateIncoming))
//Do something
});
}
Implement the delegate for CXCallObserver:
public class MyCXCallObserverDelegate : CXCallObserverDelegate
{
public override void CallChanged(CXCallObserver callObserver, CXCall call)
{
Console.WriteLine(call);
}
}
Then in your code, create a instance of CXCallObserver (maintain a strong reference to this) and then assign the delegate:
cXCallObserver = new CXCallObserver();
cXCallObserver.SetDelegate(new MyCXCallObserverDelegate(), null);

using signalR .net core client

I have set up a signalR website .net core. My function in my hub is:
public async Task Notify(int id) {
await Clients.All.InvokeAsync("Notified", id);
}
I have also tested this with the following js:
let connection = new signalR.HubConnection(myURL);
connection.on('Notified', data => {
console.log(4, data);
});
connection.start();
The js code seems to work fine and I see the log when I try connection.Invoke('Notify').
Now I have a console app that can needs to make the invoke. I am trying this in two ways and don't mind either solution:
1. A mvc controller within the signalR website that can take the id and invoke 'Notified'.
2. Use the client library Microsoft.AspNetCore.SignalR.Client in the console app.
The way 1 I have only done in classic asp.net like this:
GlobalHost.ConnectionManager.GetHubContext(hubName)
But couldn't find a way to do this in .net core.
Way 2 I have used the library and tried this so far:
var con = new HubConnectionBuilder();
con.WithUrl(myURL);
var connection = con.Build();
connection.InvokeAsync("Notify",args[0]).Wait();
This is the closest I have come to create a connection in the same way as the js code. However this code throws a null pointer when calling connection.InvokeAsync. The connection object is not null. It seems to be an internal object that is null. According to the stack trace the exception is thrown when a MoveNext() function is internally called.
Well looks like both are not currently possible. As of now I just used a forced way which is hopefully temporary.
I have created and used the following base class for hubs:
public abstract class MyHub : Hub
{
private static Dictionary<string, IHubClients> _clients = new Dictionary<string, IHubClients>();
public override Task OnConnectedAsync()
{
var c = base.OnConnectedAsync();
_clients.Remove(Name);
_clients.Add(Name, Clients);
return c;
}
public static IHubClients GetClients(string Name) {
return _clients.GetValueOrDefault(Name);
}
}
GlobalHost is gone. You need to inject IHubContext<THub> like in this sample.
This can be a bug in SignalR alpha1. Can you file an issue on https://github.com/aspnet/signalr and include a simplified repro?

Application_End equivalent in ASP.NET Core rc2? [duplicate]

Is there a shutdown function when using Microsoft.AspNet.Server.Kestrel? ASP.NET Core (formerly ASP.NET vNext) clearly has a Startup sequence, but no mention of shutdown sequence and how to handle clean closure.
In ASP.NET Core you can register to the cancellation tokens provided by IApplicationLifetime
public class Startup
{
public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime)
{
applicationLifetime.ApplicationStopping.Register(OnShutdown);
}
private void OnShutdown()
{
// Do your cleanup here
}
}
IApplicationLifetime is also exposing cancellation tokens for ApplicationStopped and ApplicationStarted as well as a StopApplication() method to stop the application.
For .NET Core 3.0+
From comments #Horkrine
For .NET Core 3.0+ it is recommended to use IHostApplicationLifetime instead, as IApplicationLifetime will be deprecated soon. The rest will still work as written above with the new service
In addition to the original answer, I had an error while trying to wire the IApplicationLifetime within the constructor.
I solved this by doing:
public class Startup
{
public void Configure(IApplicationBuilder app)
{
var applicationLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
applicationLifetime.ApplicationStopping.Register(OnShutdown);
}
private void OnShutdown()
{
// Do your cleanup here
}
}
I solved it with the application lifetime callback events
Startup.cs
public void Configure(IHostApplicationLifetime appLifetime) {
appLifetime.ApplicationStarted.Register(() => {
Console.WriteLine("Press Ctrl+C to shut down.");
});
appLifetime.ApplicationStopped.Register(() => {
Console.WriteLine("Terminating application...");
System.Diagnostics.Process.GetCurrentProcess().Kill();
});
}
Program.cs
Also, use UseConsoleLifetime() while building the host.
Host.CreateDefaultBuilder(args).UseConsoleLifetime(opts => opts.SuppressStatusMessages = true);
This class is now obsolete, please refer to the new interface IHostApplicationLifetime. More info here.
When using .NET 6 without a Startup class, you can access the ApplicationStopping CancellationToken via the built web application in Program.cs, e.g.
var app = builder.Build();
...
app.Lifetime.ApplicationStopping.Register(() => ...);
...
app.Run();
In .Net 6 if you are not using Startup.cs. You could use ApplicationStopping method in Program.cs
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = Directory.GetCurrentDirectory(),
WebRootPath = "wwwroot"
});
var app = builder.Build();
app.Lifetime.ApplicationStopping.Register(() =>
{
// Do Something
});
app.Run();

How can I attach a header to all published messages?

I have a particular header that I'd like to attach to all messages that I publish. I can attach this header on a case-by-case basis by specifying it in the Publish call:
_bus.Publish(myMessage, context => context.SetHeader("my key", "my value"));
This works fine but it becomes a chore to maintain this SetHeader call for every publish. Is there a way, during bus configuration or anywhere else, to specify a header that will be attached to all messages? That is, is there a way to do something like the following?
ServiceBusFactory.New(sbc =>
{
sbc.UseRabbitMq();
sbc.ReceiveFrom(hdoQueue);
// This is what I'd like to be able to do:
sbc.BeforePublish(context => context.SetHeader("my key", "my value"));
});
I believe there is a solution that involves implementing IOutboundMessageInterceptor but I can't find a way to attach my interceptor. There is a ServiceBusConfigurator.AddInboundInterceptor method but not a ServiceBusConfigurator.AddOutboundInterceptor method.
My intuition was correct, I was able to do what I wanted by implementing IOutboundMessageInterceptor:
public class AttachHeadersOutboundInterceptor : IOutboundMessageInterceptor
{
public void PreDispatch(ISendContext context)
{
context.SetHeader("my key", "my value");
}
public void PostDispatch(ISendContext context)
{
}
}
Oddly there is no ServiceBusConfigurator.AddOutboundInterceptor method, so I just created one (by copying the code for AddInboundInterceptor from github):
public static class MassTransitExtensions
{
public static void AddOutboundInterceptor(this ServiceBusConfigurator configurator,
IOutboundMessageInterceptor interceptor)
{
var builderConfigurator = new PostCreateBusBuilderConfigurator(bus =>
{
var interceptorConfigurator = new OutboundMessageInterceptorConfigurator(bus.OutboundPipeline);
interceptorConfigurator.Create(interceptor);
});
configurator.AddBusConfigurator(builderConfigurator);
}
}
And then I attach it during bus configuration:
ServiceBusFactory.New(sbc =>
{
sbc.UseRabbitMq();
sbc.ReceiveFrom(hdoQueue);
sbc.AddOutboundInterceptor(new AttachHeadersOutboundInterceptor());
});
Problem solved.

HttpRoutes - how do they work?

I´m struggling with URLs for ajax-reader/JSON. Each time I think I understand it, it seems that I haven´t.
Please, can anybody explain the logic behind this???
I got this Controller:
public class ServiceController : DnnApiController
{
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage GetAllItems(int moduleId)
{
MyProjectController controller = new MyProjectController();
IEnumerable<ItemInfo> items = controller.GetAllItems(moduleId);
return Request.CreateResponse(HttpStatusCode.OK, items);
}
}
I got this Routemapper:
public class RouteMapper : IServiceRouteMapper
{
public void RegisterRoutes(IMapRoute mapRouteManager)
{
mapRouteManager.MapHttpRoute("MyProject",
"default",
"{controller}/{action}",
new[] { "MyCompany.MyProject.Services" });
}
}
At what URL can I read the data with $.ajax() and what is the URL showing me the data in a browser?
Thanx in Advance!
Asle :)
This is how I do it (Note: this will only work with DNN6.2 and above);
In the View.ascx.cs add
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ServicesFramework.Instance.RequestAjaxScriptSupport();
ServicesFramework.Instance.RequestAjaxAntiForgerySupport();
jQuery.RequestDnnPluginsRegistration();
}
This ensures that jquery and the required DNN ajax plugins are added.
Initiate the services framework jquery plugin in the View.ascx like this inside javascript script tags (S.O. wouldn't allow me to include them)
var modId = <%=ModuleId %>;
var sf = $.ServicesFramework(modId);
Now in a separate javascript file or in the view.ascx control add the ajax function
function getAllItems(){
$.ajax({
type:"GET",
url:sf.getServiceRoot("MyProject")+"Service/GetAllItems",
beforeSend:sf.setModuleHeaders,
data:{moduleId:modId},
cache:false
}).done(function(data){
alert("Success!");
}).fail(function(){
alert("Crashed!");
}).always(function(){
//something you want done whether passed or failed
//like hide progress bar, ajax spinner etc.
});
}
The DNN jquery plugin will build the url which will look similar to this (Note: 142 is just for illustration purpose and will be replace with actual module id)
/DesktopModules/MyProject/API/Service/GetAllItems?moduleId=142
The URL will be something like
/desktopmodules/SlidePresentation/API/SlidePresetnation.ashx/ListOfSlides
I have examples at
https://slidepresentation.codeplex.com/SourceControl/latest
but they were for DNN6, they might require a few updates due to the API changes for DNN 7
you can see a DNN7 module that has a service layer at https://dnnsimplearticle.codeplex.com/SourceControl/latest#cs/services/

Resources