Enable CORS in Asp .Net 5(vNext) and MVC 6
5 stars based on
This post is part of a two-part series on integrating Angular 2 and SignalR. If you haven't read the first post, please check it out:.
Over the years I've always been looking for an excuse to use SignalR in applications. I just think the experience real-time communication provides is great, and the library really makes it all pretty easy to do. However, it does take some work to get a proper SignalR system set up, and for my personal site I actually am just using Pusher for real time messaging, but only because they make signalr cors options incredibly easy to use and have a very generous free tier. At work however we can't normally use 3rd party services, so this was a good signalr cors options to try to implement similar functionality using SignalR and Angular 2.
NET console app, and then wire up an Angular 2 client that signalr cors options interact and receive push notifications from the server. I am also taking some inspiration from how Pusher is designed here to abstract the SignalR components into channels and events. This first of two articles will tackle getting the server up and running, and then in the second article we'll tackle writing the Angular 2 client so that the SignalR stuff is hidden and easy to consume.
All of signalr cors options code shown is available in my github repo. As I worked on the client side of this experiment I made some changes to how the server worked. They aren't major, but include the following: I updated the ChannelEvent class to include some additional information that helps the client side handle messages.
I added an "admin" channel by default that publishes events related to activity, but this isn't too critical for the demos 3. The OnEvent call available on the clients now accepts the channel name as a parameter.
Outside of those changes the overall approach hasn't changed. I've updated all code snippets here to reflect the current state of signalr cors options. In this example we have a SignalR Hub called EventHub that is used to send out status events generated by clients. However, these events are specific to a channel called tasksso only clients that have joined the channel will get the messages.
We're hard coding this information in this example, but in a real app these values could be generated at run-time depending on the situation. One of the great things about the later versions of ASP. In a production scenario you would probably host this in a windows service, IIS or Azure, but for this demo we don't need any of that. Once you've got all these packages in there, update the Main method so it looks like the following.
Basically we're just doing the following:. Now this won't compile yet because we also need to add a file called Startup. Create one that contains the following class. This is basically doing:. We also need to create our SignalR hub itself, so we need two classes like these. The hub exposes three methods for the clients to call:.
You'll notice that we're not saving what clients are in what groups, but that's because this is a simple demo. I might try to tackle an approach for saving signalr cors options data in a future signalr cors options, but for now we're not gonna worry about that.
Finally, SignalR is a dynamic thing, signalr cors options that the methods shared between the server and client aren't strictly signalr cors options. So you need to pay special care to the name and casing used. In this hub we're using a single client method called OnEvent that the server will invoke with any new events raised by other clients.
I also define a class called ChannelEvent that is a container for any event that will be published. It contains the channel name, the event name, a timestamp and a generic Data property that can contain anything. We also expose a Json property that will set the serialized form of the Data object. The final piece we need in this console app is the API endpoints that clients can call to do work.
We will simulate that using a "task" controller that exposes a long signalr cors options operation, and a shorter running one. Some key things to see here include:. With that code written we'll all set to fire up the server. Compile and run it and you should see the following:. If you use a tool like Postman to make an REST call to the long task you should see the status events being sent out over the SignalR connection:. So with this we've got a simple WebAPI and SignalR hub all hosted in a console app with some simple logging signalr cors options let us see what messages are being sent out.
In the second part of this series we'll walk signalr cors options the Angular 2 client and demonstrate how easy it is to consume these SignalR notifications.
If you haven't read the first post, please check it out: So with that let's tackle the server! Basically we're just doing the following: Configuring the logger so that we get pretty signalr cors options in the console Creating signalr cors options OWIN host to run the app on signalr cors options This is basically doing: Enabling CORS so that our Angular 2 client can connect from a different origin This is very important, but may not apply depending on your specific need!
The hub exposes three methods for the clients to call: ChannelName, channelEvent ; if channelEvent. Some key things to see here include: We're just grabbing a reference to signalr cors options SignalR signalr cors options from the static GlobalHost property. Normally we'd inject that somehow.
Each API method defines the event name that it will fire while running. This is something the clients would need to know in advance to properly subscribe to them. The PublishEvent method makes a call invoke a call on the signalr cors options using the context.
It cannot call the public methods of the EventHub class. To me this is the part I don't like about SignalR Compile and run it and you should see the following: If you use a tool like Postman to make an REST call signalr cors options the long task you should see the status events being sent out over the SignalR connection: