我是 WCF 新手,正在编写我的第一个 WCF 应用程序。我无法从 json 请求调用服务
以下是项目/解决方案的详细信息
我的解决方案中共有 3 个项目 1. 服务库项目(MyFirstRESTfulService) 2. 宿主应用程序(WCFService2) 3. 服务客户端(ServiceClient)
1.MyFirstRESTfulService
类IEmployeeService.cs
namespace MyFirstRESTfulService
{
[ServiceContract()]
public interface IEmployeeService
{
[WebGet(UriTemplate = "Employee", ResponseFormat=WebMessageFormat.Json )]
[OperationContract]
List<Employee> GetAllEmployeeDetails();
}
}
班级EmployeeService.cs
namespace MyFirstRESTfulService
{
[AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed )]
public class EmployeeService: IEmployeeService
{
public List <Employee> GetAllEmployeeDetails()
{
return EmployeeData.Instance.EmployeeList;
}
}
}
2.WCFService2
服务.svc
网页配置
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MyFirstRESTfulService.EmployeeService">
<endpoint address="http://localhost:8046/WCFService2/Service.svc" behaviorConfiguration="Web" binding="webHttpBinding" contract="MyFirstRESTfulService.IEmployeeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8046/WCFService2/Service.svc/MyFirstRESTfulService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
3.服务客户端
默认.aspx
<body>
<script src="Script/jquery1.9.0.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery.support.cors = true;
function RefreshPage() {
var serviceUrl = "http://localhost:8046/WCFService2/Service.svc/MyFirstRESTfulService/GetAllEmployeeDetails";
debugger;
$.ajax({
type: "GET",
url: serviceUrl,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
var itemRow = "<table>";
$.each(data, function (index, item) {
itemRow += "<tr><td>" + item.EmpId + "</td><td>" + item.Fname + "</td></tr>";
});
itemRow += "</table>";
$("#divItems").html(itemRow);
},
error: ServiceFailed
});
}
</script>
</script>
<form id="form1" runat="server">
<input type="button" onclick="RefreshPage()" name="btnRefesh" value="Refresh" />
<div id="divItems">
</div>
</form>
</body>
但是这些东西根本不起作用。点击按钮时什么也没有发生。你能告诉我哪里出错了。
我收到以下错误
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Service</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Endpoint not found.</p>
</div>
</body>
</html>
提前致谢