如何将SignalR添加到我的WCF服务和MVC 4客户端应用程序?

我正在使用WCF服务为ASp.net MVC 4客户端应用程序开发项目。 我在MVC中创建了一个控制器并在其中添加了一个视图。 而且,我通过ajax和jquery从视图传递数据。来自服务的响应,结果数据值存储在一个html元素中。就像我实现的那样。一部分已经完成。第二部分是

我有想法在WCF和MVC4服务中添加functionok SignalR?

我认为SignalR是一个自托管?

我如何在WCF和MVC4中为signalR创建一个api。我的代码是。

MVC控制器: –

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVC4.Controllers { public class SampleController : Controller { public ActionResult Index() { return View(); } } } 

视图:-

  @{ Layout = null; }     Index    $(document).ready(function () { var u = $(".user"); var l = $("li"); $(u).focusout(function () { var User = u.val(); $.ajax({ type: "GET", contentType: "application/json;charset=utf-8", url: "http://localhost:64712/Service1.svc/GetValue", data: { "username": User }, success: function (data) { var c = data['GetValueResult']; console.log(c); console.log(c); l.append(c); }, }); }); });   div.show { height:250px; width:500px; background-color:indianred; box-sizing:border-box; } ul.data { height:250px; width:500px; background-color:beige; box-sizing:border-box; }    

UserID

这是路由器配置……这是最后的更改,我的MVC部分已经完成。

RouteConfig.cs: –

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MVC4 { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Sample", action = "Index", id = UrlParameter.Optional } ); } } } 

WCF服务: –
这是Service1.svc.cs: –

  public class Service1 : IService1 { public string GetValue(string username) { string u = "username="+username; Console.WriteLine("u=" + u); return u; } } 

和IService.cs文件是: –

  public interface IService1 { [OperationContract] [WebInvoke(Method="GET")] string GetValue(string username); } 

这是我的全球应用类: –

  public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { } protected void Session_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { if (HttpContext.Current != null) { HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Methods", " POST,GET"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.End(); } } } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { } } 

这是Web.Config文件: –

                                       

任何人都可以为我提供SignalR接口的解决方案。

是否可以将SignalR添加到WCF服务和MVC4 ..如何成功配置它们?