Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api 将AspNet.WebApi与AspNet.signal集成_Asp.net Web Api_Real Time_Signalr Hub_Signalr.client - Fatal编程技术网

Asp.net web api 将AspNet.WebApi与AspNet.signal集成

Asp.net web api 将AspNet.WebApi与AspNet.signal集成,asp.net-web-api,real-time,signalr-hub,signalr.client,Asp.net Web Api,Real Time,Signalr Hub,Signalr.client,我想将Microsoft.AspNet.signar version=“2.1.2”与Microsoft.AspNet.WebApi version=“5.2.2”集成,以便API可以实时通信。我发现一个实现/工作方式与我想要的完全相同,但示例使用jquery.signalR-0.5.0.js。一些早期的实现已经改变,到目前为止,我在升级解决方案以使用最新的信号器、asp.net web api和owin时失败了,我在这里做了一些工作。 我离开了中心 using SignalR.Hubs; na

我想将Microsoft.AspNet.signar version=“2.1.2”Microsoft.AspNet.WebApi version=“5.2.2”集成,以便API可以实时通信。我发现一个实现/工作方式与我想要的完全相同,但示例使用jquery.signalR-0.5.0.js。一些早期的实现已经改变,到目前为止,我在升级解决方案以使用最新的信号器、asp.net web api和owin时失败了,我在这里做了一些工作。 我离开了中心

using SignalR.Hubs;
namespace NdcDemo.Hubs
{
    // This hub has no inbound APIs, since all inbound communication is done
    // via the HTTP API. It's here for clients which want to get continuous
    // notification of changes to the ToDo database.
    [HubName("todo")]
    public class ToDoHub : Hub { }
}
我还保留了ApiControllerWithHub类的原样

using System;
using System.Web.Http;
using SignalR;
using SignalR.Hubs;
namespace NdcDemo.Controllers
{
    public abstract class ApiControllerWithHub<THub> : ApiController
        where THub : IHub
    {
        Lazy<IHubContext> hub = new Lazy<IHubContext>(
            () => GlobalHost.ConnectionManager.GetHubContext<THub>()
        );
        protected IHubContext Hub
        {
            get { return hub.Value; }
        }
    }
}

这是完整的ToDoController类

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Web.Http;
    using NdcDemo.Hubs;
    using NdcDemo.Models;

    namespace NdcDemo.Controllers
    {
        [InvalidModelStateFilter]
        public class ToDoController : ApiControllerWithHub<ToDoHub>
        {
            private static List<ToDoItem> db = new List<ToDoItem>
            {
                new ToDoItem { ID = 0, Title = "Do a silly demo on-stage at NDC" },
                new ToDoItem { ID = 1, Title = "Wash the car" },
                new ToDoItem { ID = 2, Title = "Get a haircut", Finished = true }
            };
            private static int lastId = db.Max(tdi => tdi.ID);
            public IEnumerable<ToDoItem> GetToDoItems()
            {
                lock (db)
                    return db.ToArray();
            }

            public ToDoItem GetToDoItem(int id)
            {
                lock (db)
                {
                    var item = db.SingleOrDefault(i => i.ID == id);
                    if (item == null)
                        throw new HttpResponseException(
                            Request.CreateResponse(HttpStatusCode.NotFound)
                        );

                    return item;
                }
            }

            public HttpResponseMessage PostNewToDoItem(ToDoItem item)
            {
                lock (db)
                {
                    // Add item to the "database"
                    item.ID = Interlocked.Increment(ref lastId);
                    db.Add(item);

                    // Notify the connected clients
                    Hub.Clients.addItem(item);

                    // Return the new item, inside a 201 response
                    var response = Request.CreateResponse(HttpStatusCode.Created, item);
                    string link = Url.Link("apiRoute", new { controller = "todo", id = item.ID });
                    response.Headers.Location = new Uri(link);
                    return response;
                }
            }

            public ToDoItem PutUpdatedToDoItem(int id, ToDoItem item)
            {
                lock (db)
                {
                    // Find the existing item
                    var toUpdate = db.SingleOrDefault(i => i.ID == id);
                    if (toUpdate == null)
                        throw new HttpResponseException(
                            Request.CreateResponse(HttpStatusCode.NotFound)
                        );

                    // Update the editable fields and save back to the "database"
                    toUpdate.Title = item.Title;
                    toUpdate.Finished = item.Finished;

                    // Notify the connected clients
                    Hub.Clients.updateItem(toUpdate);

                    // Return the updated item
                    return toUpdate;
                }
            }

            public HttpResponseMessage DeleteToDoItem(int id)
            {
                lock (db)
                {
                    int removeCount = db.RemoveAll(i => i.ID == id);
                    if (removeCount <= 0)
                        return Request.CreateResponse(HttpStatusCode.NotFound);

                    // Notify the connected clients
                    Hub.Clients.deleteItem(id);

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用系统线程;
使用System.Web.Http;
使用NdcDemo.Hubs;
使用NdcDemo.模型;
命名空间NdcDemo.Controllers
{
[InvalidModelStateFilter]
公共类ToDoController:ApiControllerWithHub
{
私有静态列表db=新列表
{
新ToDoItem{ID=0,Title=“在NDC的舞台上做一个愚蠢的演示”},
新ToDoItem{ID=1,Title=“洗车”},
新ToDoItem{ID=2,Title=“理发”,Finished=true}
};
私有静态int lastId=db.Max(tdi=>tdi.ID);
公共IEnumerable GetToDoItems()
{
锁(db)
返回db.ToArray();
}
公共ToDoItem GetToDoItem(int id)
{
锁(db)
{
var item=db.SingleOrDefault(i=>i.ID==ID);
如果(项==null)
抛出新的HttpResponseException(
Request.CreateResponse(HttpStatusCode.NotFound)
);
退货项目;
}
}
公共HttpResponseMessage PostNewToDoItem(ToDoItem项目)
{
锁(db)
{
//将项目添加到“数据库”
item.ID=联锁增量(参考lastId);
db.添加(项目);
//通知已连接的客户端
枢纽.客户.附件(项目);
//在201响应中返回新项
var response=Request.CreateResponse(HttpStatusCode.Created,item);
字符串link=Url.link(“apiRoute”,new{controller=“todo”,id=item.id});
response.Headers.Location=新Uri(链接);
返回响应;
}
}
公共ToDoItem PutUpdatedToDoItem(int id,ToDoItem项)
{
锁(db)
{
//查找现有项
var toUpdate=db.SingleOrDefault(i=>i.ID==ID);
if(toUpdate==null)
抛出新的HttpResponseException(
Request.CreateResponse(HttpStatusCode.NotFound)
);
//更新可编辑字段并保存回“数据库”
toUpdate.Title=项目.Title;
toUpdate.Finished=项目.Finished;
//通知已连接的客户端
Hub.Clients.updateItem(toUpdate);
//返回更新的项目
返回更新;
}
}
公共HttpResponseMessageDeleteToDoItem(int id)
{
锁(db)
{
int removeCount=db.RemoveAll(i=>i.ID==ID);
如果(从OP中移除计数溶液)

回答: 在喝了一杯甘菊茶之后,我发现客户机必须在Todo.js中的动态方法之前包含关键字CLIENT。因此,以下是需要修改的内容,以便示例能够正常工作

hub.client.addItem = function (item) {
        alert("i just received something...");
        viewModel.add(item.ID, item.Title, item.Finished);
    };
    hub.client.deleteItem = function (id) {
        viewModel.remove(id);
    };
    hub.client.updateItem = function (item) {
        viewModel.update(item.ID, item.Title, item.Finished);
    };

如果你找到了答案,那么就把你的答案文本作为答案发布。你可以回答自己的问题并将其标记为答案。这有助于使用网站的人从回答中筛选出未回答的问题。
 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Web.Http;
    using NdcDemo.Hubs;
    using NdcDemo.Models;

    namespace NdcDemo.Controllers
    {
        [InvalidModelStateFilter]
        public class ToDoController : ApiControllerWithHub<ToDoHub>
        {
            private static List<ToDoItem> db = new List<ToDoItem>
            {
                new ToDoItem { ID = 0, Title = "Do a silly demo on-stage at NDC" },
                new ToDoItem { ID = 1, Title = "Wash the car" },
                new ToDoItem { ID = 2, Title = "Get a haircut", Finished = true }
            };
            private static int lastId = db.Max(tdi => tdi.ID);
            public IEnumerable<ToDoItem> GetToDoItems()
            {
                lock (db)
                    return db.ToArray();
            }

            public ToDoItem GetToDoItem(int id)
            {
                lock (db)
                {
                    var item = db.SingleOrDefault(i => i.ID == id);
                    if (item == null)
                        throw new HttpResponseException(
                            Request.CreateResponse(HttpStatusCode.NotFound)
                        );

                    return item;
                }
            }

            public HttpResponseMessage PostNewToDoItem(ToDoItem item)
            {
                lock (db)
                {
                    // Add item to the "database"
                    item.ID = Interlocked.Increment(ref lastId);
                    db.Add(item);

                    // Notify the connected clients
                    Hub.Clients.addItem(item);

                    // Return the new item, inside a 201 response
                    var response = Request.CreateResponse(HttpStatusCode.Created, item);
                    string link = Url.Link("apiRoute", new { controller = "todo", id = item.ID });
                    response.Headers.Location = new Uri(link);
                    return response;
                }
            }

            public ToDoItem PutUpdatedToDoItem(int id, ToDoItem item)
            {
                lock (db)
                {
                    // Find the existing item
                    var toUpdate = db.SingleOrDefault(i => i.ID == id);
                    if (toUpdate == null)
                        throw new HttpResponseException(
                            Request.CreateResponse(HttpStatusCode.NotFound)
                        );

                    // Update the editable fields and save back to the "database"
                    toUpdate.Title = item.Title;
                    toUpdate.Finished = item.Finished;

                    // Notify the connected clients
                    Hub.Clients.updateItem(toUpdate);

                    // Return the updated item
                    return toUpdate;
                }
            }

            public HttpResponseMessage DeleteToDoItem(int id)
            {
                lock (db)
                {
                    int removeCount = db.RemoveAll(i => i.ID == id);
                    if (removeCount <= 0)
                        return Request.CreateResponse(HttpStatusCode.NotFound);

                    // Notify the connected clients
                    Hub.Clients.deleteItem(id);

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
            }
        }
    }
hub.client.addItem = function (item) {
        alert("i just received something...");
        viewModel.add(item.ID, item.Title, item.Finished);
    };
    hub.client.deleteItem = function (id) {
        viewModel.remove(id);
    };
    hub.client.updateItem = function (item) {
        viewModel.update(item.ID, item.Title, item.Finished);
    };