signalr client 400 bad request - asp.net-mvc

I used signalr self hosting with MVC and need to call it from client on another machine so I wrote code like that:
$(function () {
jQuery.support.cors = true;
$.connection.hub.url = "http://[server external Ip]:3031/signalr";
var chat = $.connection.CustomHub;
chat.client.addMessage = function (data, IMEI) {
//SomeCode
}
}
Everything going well but I have this error in Firefox Firebug:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:// [server external IP]/signalr/negotiate?connectionData=%5B%7B%22name%22%3A%22customhub%22%7D%5D&clientProtocol=1.3&_=1400692033406. This can be fixed by moving the resource to the same domain or enabling CORS.

You have to enable Cross-Domain on the server application by installing Microsoft.Owin.Cors package and the calling UseCors() when starting up SignalR (assuming you are using SignalR 2.x). You do NOT need to specify jQuery.support.cors = true; in SignalR 2.0, actually you should remove it AFAIK.

It seems that the error refer to the network connection, we have already used signalR and the identification of the Url to hub is not mandatory.
Below an implementation of SignalR for Sales object:
1- Enable the service broker on the Database SQL Server:
ALTER DATABASE BlogDemos SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE ;
2- Install signalR from nuget
Install-Package Microsoft.AspNet.SignalR
3- Add reference to signalr javascript library if not added via nuget
<script src="/Scripts/jquery.signalR-2.2.1.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
4- Add Javascript to call the Hub
$(function () {
// Declare a proxy to reference the hub.
var salesNotificationsHub = $.connection.salesHub;
// Create a function that the hub can call to broadcast messages.
salesNotificationsHub.client.updateClientsales = function () {
Search();
//alert('des nouveaux sales');
};
// Start the connection.
$.connection.hub.start().done(function () {
alert('successful connection');
}).fail(function (e) {
alert(e);
});
});
5- Add the Search function created in step 4
function Search() {
grid.reload({ searchString: searchfields });
}
6- Creation of the code to load the Grid from GetList function in the
Controller
grid = $("#grid").grid({
dataKey: "Codsales",
uiLibrary: "bootstrap",
dataSource:"/sales/GetList",
autoLoad: true,
columns: [
{ field: "Codsales", title: "Code Demande", width: 200, sortable: true },
{ field: "Libelsales", title: "Libellé Demande", width: 200, sortable: true },
],
pager: { enable: true, limit: 10, sizes: [10, 20, 30, 40] }
});
7- Creation GetList function in the controller
public JsonResult GetList()
{
List<salesData> objsalesList = GetList().ToList();
return Json(objGridData, JsonRequestBehavior.AllowGet);
}
8- Create Function GetList Where will be attached the SqlDependency Object
public static List<salesData> GetList()
{
SqlDependency dependency = new SqlDependency();
using (SqlConnection cn = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(#"SELECT Codsales, Libelsales, Datsales,DatDetailledsales FROM [dbo].sales ", cn))
{
command.Notification = null;
dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (cn.State == ConnectionState.Closed)
cn.Open();
command.ExecuteNonQuery();
}
}
List<salesData> objList = new List<salesData>();
objList=Fetchsales(filterExpression, sortExpression, pageIndex, pageSize, out total);
rowsCount = total;
return objList;
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
salesHub.Updatesales();
}
}
9- Create Hub class
public class salesHub : Hub
{
[HubMethodName("updatesales")]
public static void Updatesales()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<salesHub>();
context.Clients.All.updateClientsales();
}
}
10- Configuration of the Sqldependency in Global.asax file
protected void Application_Start()
{ //Start SqlDependency with application initialization
string connString= ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlDependency.Start(connString);
}
protected void Application_End()
{
//Stop SQL dependency
string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlDependency.Stop(connString);
}
Cordially

Related

How to websocket in grails5

I want a grails application in which server sends some message at a fixed interval.
I have tried using spring-websocket plugin in grails, server and client are able to connect but that doesn't fullfill my requirement. i.e., I want, server sends some message at a fixed interval.
This is the server-side code :
package test
import org.springframework.messaging.handler.annotation.MessageMapping
import org.springframework.messaging.handler.annotation.SendTo
class ExampleController {
def index() { }
// server
#MessageMapping("/hello")
#SendTo("/topic/hello")
protected String hello(String world) {
List<String> list = new ArrayList<>();
BufferedReader file = new BufferedReader(new FileReader("src/main/resources/dummyLog.txt"));
file.eachLine {line ->
list.add(line)
}
int idx = (int)(Math.random() * list.size());
println idx;
return list.get(idx);
}
}
And this is the client-side code :
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', () => {
var socket = new SockJS("${createLink(uri: '/stomp')}");
var client = webstomp.over(socket);
client.connect({}, function() {
client.subscribe("/topic/hello", (message) => {
document.getElementById('helloDiv').append(message.body);
});
});
document.getElementById('helloButton').addEventListener('click', () => {
client.send("/app/hello", JSON.stringify("world"));
});
});
</script>
Thanks.

How to Call Application service from Web Api in ASP.Net Boilerplate MVC project?

ASP.NET Boilerplate (.Net Framework v4.7.2 & MVC)
I am creating a web api controller(AgentInfoController) for an application service class(AgentInfoAppService), which depends on a repository IAgentInfoRepository. From AgentInfoController, I am able to call a method from AgentInfoAppService by passing IAgentInfoRepository in the constructor like this new AgentInfoAppService(_agentInfoRepository).GetAgentCampaignswithCode but I am not sure if this is the correct way?
We have a working code from mvc layer where we call application service class(dynamic web api layer) without adding any references to Repository like below. Is there a way we could add a dependency injection so I don't have to add the repository in each application service calls from web api layer?Please advise.
jQuery.ajax({
url: "/api/services/app/AgentInfoAppService/GetAgentCampaignswithCode?agentCode=100",
type: "GET",
contentType: "application/json; charset=utf-8"
})
public class AgentInfoAppService : ApplicationService, IAgentInfoAppService
{
private readonly IAgentInfoRepository _agentInfoRepository;
public AgentInfoAppService(IAgentInfoRepository agentInfoRepository)
{
_agentInfoRepository = agentInfoRepository;
}
public GetAgentCodeCampaignOutput GetAgentCampaignswithCode(string agentCode, bool? defaultCampaign)
{
var agentCampaigns = _agentInfoRepository.GetAgentCampaignswithCode(agentCode, defaultCampaign);
return new GetAgentCodeCampaignOutput()
{
AgentCodeCampaign = Mapper.Map<List<AgentCodeCampaignDto>>(agentCampaigns)
};
}
}
public class AgentInfoController : AbpApiController
{
private readonly IAgentInfoRepository _agentInfoRepository;
[HttpGet]
public GetAgentCodeCampaignOutput GetAgentCampaignswithCode(string agentCode, bool? defaultCampaign)
{
return new AgentInfoAppService(_agentInfoRepository).GetAgentCampaignswithCode(agentCode, defaultCampaign);
}
}
When you run the project Abp creates all Methods from ApplicationService layer and you can use it. You don't need to create the method again in the controller.
Here is an example of what I'm saying
I have an ApplicationServie method like this
public class CiudadAppService : AsyncCrudAppService<AdozonaCiudad, CiudadDto, int, PagedAndSortedRequest, CiudadDto, CiudadDto>, ICiudadAppService
{
private readonly ICiudadManager _ciudadManager;
public CiudadAppService(IRepository<AdozonaCiudad> repository, ICiudadManager ciudadManager) : base(repository)
{
_ciudadManager = ciudadManager;
}
public override async Task<CiudadDto> CreateAsync(CiudadDto input)
{
var result = await _ciudadManager.RegistrarOActualizar(ObjectMapper.Map<AdozonaCiudad>(input));
return MapToEntityDto(result);
}
}
In this AppService I have one Method called CreateAsync this method can be used from javascript like this.
(function ($) {
var _ciudadService = abp.services.app.ciudad;
var _$form = $('form[name=Index]');
function save() {
_$form.validate({
invalidHandler: function (form, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var firstInvalidElement = $(validator.errorList[0].element);
$('html,body').scrollTop(firstInvalidElement.offset().top - 100);
firstInvalidElement.focus();
}
},
rules: {
Ciudad: "required"
},
messages: {
Ciudad: "El valor del campo es requerido"
},
highlight: function (element) {
$(element).parent().addClass('error-validation')
},
unhighlight: function (element) {
$(element).parent().removeClass('error-validation')
}
});
if (!_$form.valid()) {
return;
}
var ciudad = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js
abp.ui.setBusy(_$form);
_ciudadService.create(ciudad).done(function () {
location.reload(true); //reload page to see edited role!
abp.notify.success('Registro correcto');
$(".save-button").html('Guardar');
}).always(function () {
abp.ui.clearBusy(_$form);
});
}
}
As you can see there the important part is: var _ciudadService = abp.services.app.ciudad; with this you're creating a service and you can access to all the methods that you have in ApplicationService
I hope it could help you, if you need further help please leave a comment!

ASP.NET MVC SignalR Client methods not invoked in separate project

I have to two separate asp.net projects on the same server. All correct SignalR nuget packages are installed as far as I know on both projects. One is the ChatServer and another is a ChatClient. Both are DotNetNuke projects.
The second project DOES know about signalR because OnConnected in Server project gets triggered when running client project and the client user is inserted as online (through the server OnConnected method).
When the sever comes online (by launching server project)the method in the client "getonlineusers" does not get triggered though.
But if I refresh the client, the server does show as offline. But I shouldnt have to refresh.
I am probably missing something not sure if I am creating a proxy correctly for the client project, any help would be greatly appreciated.
How do you connect a client separate project to the project that has the hub with SignalR?
In the ChatServer project:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
public class ChatSupportHub : Hub
{
private ChatServerManager CSM { get; set; }
private int UserID { get; set; }
//private UserInfo CurrentUser { get; set; }
public ChatSupportHub()
{
CSM = new ChatServerManager();
//CurrentUser = DotNetNuke.Entities.Users.UserController.Instance.GetCurrentUserInfo();
}
public override Task OnConnected()
{
int outNumber = -1;
Online user = new Online();
User dbUser = new User();
var userid = Context.QueryString["userid"];
bool success = Int32.TryParse(userid, out outNumber);
string connID = Context.ConnectionId;
if (success)
{
user.ID = outNumber;
dbUser = CSM.GetUser(outNumber);
UserID = outNumber;
}
user.ConnectionID = connID;
user.Type = dbUser.TypeID.HasValue ? CSM.GetUserType((int) dbUser.TypeID) : null;
user.ShowOnline = true;
CSM.AddOnlineUser(user);
var onlineUsers = CSM.GetOnlineUsers();
var clients = Clients.Caller;
string onlineUserJSON = JsonConvert.SerializeObject(onlineUsers);
clients.getonlineusers(dbUser.Name, onlineUserJSON);
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
var username = Context.User.Identity.Name;
int userID = CSM.GetUserId(username);
CSM.RemoveOnlineUser(userID);
return base.OnDisconnected(stopCalled);
}
}
And the View index page of the ChatServer project:
<script src="~/myServertProjectPath/Scripts/jquery.signalR-2.4.1.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
$(function () {
// set up the hub connection
var hub = $.connection.chatSupportHub;
$.connection.hub.qs = "userid=" + #Model.CurrentUserInfo.UserID.ToString();
hub.client.getonlineusers = function (currentUsername, onlineUsers) {
if (onlineUsers) {
console.log('There are users online and one is: ' + currentUsername);
}
}
$.connection.hub
.start()
.done(function () {
})
});
And same thing in the separate client project for the view:
<script src="~/myClientProjectPath/Scripts/jquery.signalR-2.4.1.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
$(function () {
connect();
});
function connect() {
hub = $.connection.chatSupportHub;
$.connection.hub.qs = "userid=" + #Model.CurrentUserInfo.UserID.ToString();
hub.client.getonlineusers = function (currentUsername, onlineUsers) {
if (onlineUsers) {
console.log ("Hello from the separate project.");
}
}
$.connection.hub
.start()
.done(function () {
})
}
</script>

How to live notification in MVC with SignalR?

I'm trying to make live notification using signalR. My project is running on localhost. But I don't see my notification when I set webconfig server-side. (although I did it with signalR)
When I run the 'internet' part of Chrome 's check item, I see that the request does not fall. how do I make this problem?
ajax code;
function updateNotification() {
$('#notiContent').empty();
$('#notiContent').append($('<li>Yükleniyor...</li>'));
$.ajax({
type: 'GET',
datatype : JSON,
contentType: 'application/json; charset=utf-8',
url: '/notification/GetNotificationFlows',
success: function (response) {
$('#notiContent').empty();
if (response.length == 0) {
$('#notiContent').append($('<li>Data yok..</li>'));
}
$.each(response, function (index, value) {
$('#notiContent').append($('<li>Yeni kişi : ' + value.flowName + ' (' + value.flowPhone + ') eklendi.</li>'));
});
},
error: function (error) {
console.log(error);
}
})
}
Global.asax;
string con = ConfigurationManager.ConnectionStrings["sqlConString"].ConnectionString;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
SqlDependency.Start(con);
}
protected void Session_Start(object sender, EventArgs e)
{
NotificationComponent NC = new NotificationComponent();
var currentTime = DateTime.Now;
HttpContext.Current.Session["LastUpdated"] = currentTime;
NC.RegisterNotification(currentTime);
}
protected void Application_End()
{
//here we will stop Sql Dependency
SqlDependency.Stop(con);
}
}
Notification component
public void RegisterNotification(DateTime currentTime)
{
string conStr = ConfigurationManager.ConnectionStrings["sqlConString"].ConnectionString;
string sqlCommand = #"SELECT [flowId],[flowName],[flowEMail],[flowPhone],[kaynakId] from [dbo].[flow] where [createDate] > #createDate";
//you can notice here I have added table name like this [dbo].[Contacts] with [dbo], its mendatory when you use Sql Dependency
using (SqlConnection con = new SqlConnection(conStr))
{
SqlCommand cmd = new SqlCommand(sqlCommand, con);
cmd.Parameters.AddWithValue("#createDate", currentTime);
if (con.State != System.Data.ConnectionState.Open)
{
con.Open();
}
cmd.Notification = null;
SqlDependency sqlDep = new SqlDependency(cmd);
sqlDep.OnChange += sqlDep_OnChange;
//we must have to execute the command here
using (SqlDataReader reader = cmd.ExecuteReader())
{
// nothing need to add here now
}
}
}
void sqlDep_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
SqlDependency sqlDep = sender as SqlDependency;
sqlDep.OnChange -= sqlDep_OnChange;
//from here we will send notification message to client
var notificationHub = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
notificationHub.Clients.All.notify("eklendi.");
//re-register notification
RegisterNotification(DateTime.Now);
}
}
public List<flow> GetFlows(DateTime afterDate)
{
using (smartCMSEntities dc = new smartCMSEntities())
{
return dc.flow.Where(a => a.createDate > afterDate).OrderByDescending(a => a.createDate).ToList();
}
}
Notification Controller
public JsonResult GetNotificationFlows()
{
var notificationRegisterTime = Session["LastUpdated"] != null ? Convert.ToDateTime(Session["LastUpdated"]) : DateTime.Now;
NotificationComponent NC = new NotificationComponent();
var list = NC.GetFlows(notificationRegisterTime);
Session["LastUpdate"] = DateTime.Now;
return new JsonResult { Data = list, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
Notification Hub
public class NotificationHub : Hub
{
//public void Hello()
//{
// Clients.All.hello();
//}
}
SQL (for sql dependency)
ALTER DATABASE [db_name] SET ENABLE_BROKER with rollback immediate;
I had the same problem you need to create your function inside your Hub.
Let say
public class NotificationHub : Hub
{
public static void Send()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.All.displayStatus();
}
}
And call it in your html
function updateNotificationCount() {
$('span.count').show();
var count = 0;
count = parseInt($('span.count').html()) || 0;
count++;
$('span.noti').css("color", "white");
// $('span.count').css({ "background-color": "red", "color": "white" });
$('span.count').html(count);
}
// signalr js code for start hub and send receive notification
var hub = $.connection.notificationHub;
// Declare a function on the hub hub so the server can invoke it
hub.client.displayStatus = function () {
updateNotificationCount();
};
// Start the connection
$.connection.hub.start();

Calling a Client Method on a Windows Service

I have a SignalR client in a Windows Service that successfully calls a Server method in an MVC app. First the Server Code:
public class AlphaHub : Hub
{
public void Hello(string message)
{
// We got the string from the Windows Service
// using SignalR. Now need to send to the clients
Clients.All.addNewMessageToPage(message);
// Send message to Windows Service
}
and
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.MapSignalR("/signalr", new HubConfiguration());
}
}
The Windows Service client is:
protected override async void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
try
{
var hubConnection = new HubConnection("http://localhost.com/signalr", useDefaultUrl: false);
IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");
await hubConnection.Start();
await alphaProxy.Invoke("Hello", "Message from Service");
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message);
}
}
It sends a message to the MVC Server. Now I want to call the other way from server to client. The Client Programming Guide has the following code examples which will NOT work as this is not a desktop.
WinRT Client code for method called from server without parameters (see WPF and Silverlight examples later in this topic)
var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHub.On("notify", () =>
// Context is a reference to SynchronizationContext.Current
Context.Post(delegate
{
textBox.Text += "Notified!\n";
}, null)
);
await hubConnection.Start();
How can I call a method on the client?
The .NET client side code seems fine. You can simply get rid of Context.Post since your client is running inside of a Windows Service and doesn't need a SyncContext:
protected override async void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
try
{
var hubConnection = new HubConnection("http://localhost.com/signalr", useDefaultUrl: false);
IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");
stockTickerHub.On("Notify", () => eventLog1.WriteEntry("Notified!"));
await hubConnection.Start();
await alphaProxy.Invoke("Hello", "Message from Service");
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message);
}
}
You can invoke the "Notify" callback from inside your AlphaHub on the server like so:
public class AlphaHub : Hub
{
public void Hello(string message)
{
// We got the string from the Windows Service
// using SignalR. Now need to send to the clients
Clients.All.addNewMessageToPage(message);
// Send message to the Windows Service
Clients.All.Notify();
}
Any client will be able to listen to these notifications since we are using Clients.All. If you want to avoid this, you need some way to authenticate your Windows Service and get its ConnectionId. Once you have that, you can send to the Windows Service specifically like so:
Clients.Client(serviceConnectionId).Notify();
Hope this helps.
Windows Service with self hosted SignalR
public partial class MyWindowsService : ServiceBase
{
IDisposable SignalR { get; set; }
public class SignalRStartup
{
public static IAppBuilder App = null;
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration()
{
// EnableDetailedErrors = true
};
map.RunSignalR(hubConfiguration);
});
}
}
public MyWindowsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args) { Start(); }
protected override void OnStop() { Stop(); }
public void Start()
{
SignalR = WebApp.Start<SignalRStartup>("http://localhost:8085/signalr");
CallToMvcJavascript();
}
public new void Stop()
{
SignalR.Dispose();
}
private void CallToMvcJavascript(){
GlobalHost.ConnectionManager.GetHubContext<MyHub>().Clients.All.addNotice(// object/data to send//);
}
}
The Hub in the Windows Service
public class MyHub : Hub
{
public void Send()
{
Clients.All.confirmSend("The service received the client message");
}
}
The Javascript
$.connection.hub.logging = true;
$.connection.hub.url = "http://localhost:8085/signalr";
var notices = $.connection.myHub;
notices.client.addNotice = function(notice) {
console.log(notice);
};
notices.client.confirmSend = function(msg) {
alert(msg);
};
$.connection.hub.start().done(function() {
$('#myTestBtn').on('click', function() {
notices.server.send();
});
});

Resources