C# 如何将SignalR添加到WCF服务和MVC4客户端应用程序?
我正在开发一个带有WCF服务的ASp.NETMVC4客户端应用程序项目。 我在MVC中创建了一个控制器,并在其中添加了一个视图。此外,我还使用ajax和jquery从视图传递了数据。在服务响应之后,结果数据值存储在html元素中。就像我实现的那样。一部分完成了,第二部分完成了 我想在WCF和MVC4服务中添加ok Signaler功能 我以为信号员是一个自我主持的人 如何在WCF和MVC4中为signalR创建api。下面是我的代码 MVC控制器:-C# 如何将SignalR添加到WCF服务和MVC4客户端应用程序?,c#,asp.net-mvc,wcf,service,signalr,C#,Asp.net Mvc,Wcf,Service,Signalr,我正在开发一个带有WCF服务的ASp.NETMVC4客户端应用程序项目。 我在MVC中创建了一个控制器,并在其中添加了一个视图。此外,我还使用ajax和jquery从视图传递了数据。在服务响应之后,结果数据值存储在html元素中。就像我实现的那样。一部分完成了,第二部分完成了 我想在WCF和MVC4服务中添加ok Signaler功能 我以为信号员是一个自我主持的人 如何在WCF和MVC4中为signalR创建api。下面是我的代码 MVC控制器:- using System;
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>
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>
谁能为我提供一个信号机接口的解决方案
是否可以将信号器添加到WCF服务和MVC4。如何成功配置它们?看一看;谢谢你的链接teresko..我对服务信号员有了一些想法。看看;谢谢你的链接teresko.我对信号员的服务有了一些想法。
<?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>