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

时间:2016-11-22 10:51:29

标签: c# asp.net-mvc wcf service signalr

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

我有想法在WCF和MVC4服务中添加功能ok 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;
   }

   <!DOCTYPE html>

   <html>
   <head>
   <meta name="viewport" content="width=device-width" />
   <title>Index</title>
   <script src="~/Scripts/jquery-1.8.2.js"></script>
   <script src="~/Scripts/jquery-1.8.2.min.js"></script>
   <script type="text/javascript">
   $(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);

                },
            });
        });
    });

   </script>
   <style>
   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;
    }
</style>
</head>
<body>
<div class="show">
       <p>UserID</p>
       <input type="text"  class="user"/>
</div>
<ul class="data">
    <li></li>
</ul></body> 
</html>

这是路由器配置......这是最后的更改,我的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文件: -

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <appSettings>
      <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
   </appSettings>
   <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
   </system.web>
   <system.serviceModel>
      <services>
         <service name="WCF_2.Service1">
            <endpoint address="" contract="WCF_2.IService1" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="web" />
         </service>
      </services>
      <behaviors>
         <serviceBehaviors>
            <behavior>
               <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
               <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
         </serviceBehaviors>
         <endpointBehaviors>
            <behavior name="web">
               <webHttp helpEnabled="false" defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" />
            </behavior>
         </endpointBehaviors>
      </behaviors>
      <protocolMapping>
         <add binding="basicHttpsBinding" scheme="https" />
      </protocolMapping>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
   </system.serviceModel>
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <directoryBrowse enabled="true" />
   </system.webServer>
</configuration>

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

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

0 个答案:

没有答案