Consuming Signal R service in Desktop application - winforms

I am new to SignalR, I want to develop a service using SingalR API’s and Host as selfhost\IIS, now my doubt is How can I consume these message in Client side my client application will be Windows(Desktop) applications.
Main intention is to send Notification from SignalR service to Clients. I would like to know is this achievable.
Please share any tutorials link or similar projects URL
Thanks in Advance

What I do is use .Net events , to wrap the SignlR client functions and connect to the desktop application call backs . like so :
public class Client
{
public delegate void OnReciveMessage(Object message);
public event OnReciveMessage MessageRecivedEvent;
public void ReciveMessage(Object message)
{
if (MessageRecivedEvent != null)
{
MessageRecivedEvent(message);
}
}
}

Yes it's possible. You can connect to a Signal R hub from webclient or winformsclient.
Useful information on this links:
SignalR Chat App in WinForm With Remote Clients
http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-signalr-20-self-host

Related

Disconnect from SignalR at the time of logout - ReactJS & Entity Framework

I'm facing the issue regarding to disconnecting from signalR. I want to know that is there some way to disconnect the connection that is established between my frontend and the backend when i logout from the app.
Following npm package of signalR we are using on frontend:
#aspnet/signalr: 1.1.4
U can send stop connection from client side like this
const logoutBtn=()=>{
connection.stop() // This line will send a disconnected call to the server
Logout()
}
// Server code
public override async Task OnDisconnectedAsync(Exception? exception)
{
Context.ConnectionId // U will get the connection id that is disconnected
}

SignalR client hangs on start() on the second session

I have a ASP.Net core web-app published to IIS, the web-app uses SignalR to display notifications. I also have a desktop windows forms application is used to connect to a fingerprint device and is also used to send notification to the web-app that a person just entered so i can display a notification on the web-app about that person.
And everything works fine with one session of the web-app, but if i open another tab for example or browse the web-app from another computer the second one hangs on the connection.start() until i close the first session. and this drives me crazy.
Here is the code.
ASP.net core Hub
public class SignalRServer : Hub
{
public async Task displayNotification()
{
await Clients.All.SendAsync("displayNotification");
}
}
Javascript client code
let connection = new signalR.HubConnectionBuilder()
.withUrl("/SignalRServer")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on("displayNotification", () => {
getLatestNotification();
});
connection.start();
The windows forms app has two projects the first one is .NET framework 4.5 which uses the fingerprint device API to connect to the device. And the second project is .NET standard which contains the SignalR HubConnection code to connect to server, and HubConnection is used in the first project to connect and send notifications.
windows forms app Hub connection code in .NET standard
public class HubNotification
{
HubConnection connection;
public HubNotification(string Ip)
{
connection = new HubConnectionBuilder()
.WithUrl(Ip + "/SignalRServer")
.Build();
connection.StartAsync();
}
public async Task ConnectAsync()
{
await connection.StartAsync();
}
public void SendNotAsync()
{
connection.InvokeAsync("displayNotification");
}
}
then in the first project i use ConnectAsync and SendNotAsync to send notification when i person enters.
I don't think there is a problem with the desktop app but i posted the code anyway.
I have read that there is a problem with signalR and multiple session but didn't find any solution.

Debugging web API back-end and angular front-end as two projects in one solution

I have one solution in Visual Studio 2013 which contains MVC Web API and Angular app.
I have set Multiple Startup Projects so both projects start in the same time when debugging.
The both start OK, but they are running on different ports so there is no way for front-end to call api from back-end.
front-end has request look like this:
$http.get("api/MenuItems").then(function(response) {
//do something here
});
and backend has controller like this:
[RoutePrefix("api/MenuItems")]
public class MenuController : ApiController
{
public IHttpActionResult Get()
{
return Ok(MenuItem.GetItems());
}
}
How do I debug this? I would like to set breakpoint in the controller of the Web API and see the debugger stop when fron-tend fire up request, but this never happens ... Am I missing something?
This was inspired by tutorial AngularJS Token Authentication using ASP.NET Web API 2, Owin, and Identity
Source code for the tutoprial is tjoudeh/AngularJSAuthentication
Because they are running on different ports, you have to specify the full url. A full url goes http(s)://host:port/address
So if your backend server was on port 80, you should have your api call
http://localhost:80/api/menuItems
When a url is relative it will go to the same host so if your frontend was on port 40, /api/menuItems refers to
http://localhost:40/api/menuItems
Then if you are debugging web api and you put a breakpoint at the start of the controller function Get() you should be able to debug the request
There is an article for similar to this.
https://weblog.west-wind.com/posts/2011/Dec/15/Debugging-ApplicationStart-and-Module-Initialization-with-IIS-and-Visual-Studio
Basically if you want to be able to debug AngularJS and MVC server side code at the same time(F5 in Visual Studio), you need to configure the website in IIS. So serverside and client side can call the same domain(not with the port).

How to abort controller action in play framework 2.1 when client cancel request or client socket is closed?

I am testing play framework 2.1 to check what happen when client cancel request or socket is closed from client side. I created this simple program:
package controllers;
import play.;
import play.mvc.;
import views.html.*;
public class Application extends Controller {
public static Result index() {
try{
for(int i=0;i<1000;i++){
Thread.sleep(10000);
System.out.println(i+"\n");
}
}catch(Exception e){
System.out.println("\nexcepción capturada");
}
return ok(index.render("Your new application is ready."));
}
}
If I cancel request from client (google chrome) the loop isn't aborted. I think this could be a problem in real world application, not making a loop, but doing a "heavy" query to database.
Since there was no answer I asked on the google group for play
https://groups.google.com/forum/?hl=en#!topic/play-framework/POvFA3moXug
and here is what James Roper from Typesafe said:
Play offers no such feature. However, there are other ways to achieve
the same thing, for example, if you have a long running request, you
could do it as a websockets request, which let's you easily detect
when the client navigates to a different page.
The latest release as of today is play-2.2.0-RC1 (in case they add support later).

Silverlight user authentication

I am currently developing a Silverlight 3 app that needs some sort of user authentication, because the data pulled from a WCF service is user specific. Target audience is the regular Internet - so there is no AD to authenticate against.
Here are some of the questions I have concerning that situation:
Is there a framework or other mechanism that would support me?
Would you recommend authentication within the Silverlight app or via outside mechanisms like forms auth? Which is more secure?
What about out-of-browser support?
I used ASP.NET's authentication. Just use a MembershipProvider (or implement your own).
Then go to http://www.silverlightshow.net/items/Accessing-the-ASP.NET-Authentication-Profile-and-Role-Service-in-Silverlight.aspx to check out how you can expose the authentication service.
Then in your WCF service, you do the following (hosted in ASP):
public class MyWCFService : IMyWCFService
{
// retrieve your UserId from the MembershipProvider
private int GetUserId()
{
MembershipUser user = Membership.GetUser();
int userId = (int)user.ProviderUserKey;
return userId;
}
// check if user is authenticated
private bool IsUserAuthenticated()
{
return HttpContext.Current.User.Identity.IsAuthenticated;
}
public void Subscribe()
{
if (!IsUserAuthenticated())
{
throw new SecurityException("You must be authenticated to be able to use this service.");
}
int userId = GetUserId();
DoStuff(userId);
}
}
Hope that helps.
I would consider using the the authentication classes that exist in ASP.NET. You can then use .NET RIA Services (or even simply, WCF) to communicate with authentication service.
Consider this article as a primer.

Resources