12,166
社区成员




//IEmployees.cs
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
namespace Artech.WcfServices.Service.Interface
{
[ServiceContract]
public interface IEmployees
{
[WebGet(UriTemplate = "all",ResponseFormat =WebMessageFormat.Json)]
IEnumerable<Employee> GetAll();
[OperationContract, WebInvoke(UriTemplate = "InsRow",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string InsertRow(string id, string title, string content);
}
[DataContract]
public class Employee
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public string Grade { get; set; }
}
}
//EmployeesService.cs
using System.Collections.Generic;
using Artech.WcfServices.Service.Interface;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
namespace Artech.WcfServices.Service
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeesService : IEmployees
{
public IEnumerable<Employee> GetAll()
{
return new List<Employee>
{
new Employee{ Id = "001", Name="张三", Department="开发部", Grade = "G6"},
new Employee{ Id = "002", Name="李四", Department="人事部", Grade = "G7"},
new Employee{ Id = "003", Name="王五", Department="销售部", Grade = "G8"}
};
}
public string InsertRow(string id, string title, string content)
{
return string.Format("您输入的标题是:{0}\n\n您输入的内容是:{1}\n\n此文章的id是:{2}", title, content, id);
}
}
}
//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Web;
using System.ServiceModel;
using Artech.WcfServices.Service.Interface;
namespace Artech.WcfServices.Service
{
public class Program
{
static void Main()
{
using (WebServiceHost host = new WebServiceHost(typeof(EmployeesService)))
{
host.Open();
Console.Read();
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="Artech.WcfServices.Service.EmployeesService">
<endpoint kind="webHttpEndpoint"
address="http://127.0.0.1:3721/employees" behaviorConfiguration="ProductsBehavior"
contract="Artech.WcfServices.Service.Interface.IEmployees"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="ProductsBehavior">
<webHttp />
<!--<enableWebScript />如果需要支持脚本调用请启用此项-->
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ProductsBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>
<!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>员工列表</title>
<style type="text/css">
body
{
font-size: 12px;
text-align: center;
}
#employees
{
border: 1px solid #000000;
margin: 10px auto;
background-color: #eee;
}
#employees tr
{
line-height: 23px;
}
#employees th
{
background-color: #ccc;
color: #fff;
}
.oddRow
{
background-color: #fff;
}
</style>
<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "get",
url: "http://127.0.0.1:3721/employees/all",
dataType: "jsonp",
success: function (employees) {
$.each(employees, function (index, value) {
var detailUrl = "detail.html?id=" + value.Id;
var html = "<tr><td>";
html += value.Id + "</td><td>";
html += "<a href='" + detailUrl + "'>" + value.Name + "</a></td><td>";
html += value.Grade + "</td><td>";
html += value.Department + "</td></tr>";
$("#employees").append(html);
});
$("#employees tr:odd").addClass("oddRow");
}
});
});
function callServer() {
var id = Number($("#id").val());
var title = String($("#title").val());
var content = String($("#content").val());
$.ajax({
cache: false,
async: true,
type: "POST",
dataType: "json",
url: "http://127.0.0.1:3721/employees/InsRow",
data: '{"id":' + id + ',"title":"' + title + '","content":"' + content + '"}',
contentType: "application/json;charset=utf-8",
success: function (msg) {
var a = eval('(' + msg + ')');
if (String(a.d).length > 0) { alert(a.d); }
else { alert("服务器超时"); }
}
});
}
</script>
</head>
<body>
<table id="employees" width="600px">
<tr>
<th>ID</th>
<th>姓名</th>
<th>级别</th>
<th>部门</th>
</tr>
</table>
</p>
<div>
文章标题:<input type="text" id="title" />
<br />
文章内容:<textarea id="content"></textarea>
<input type="hidden" id="id" value="1" /><br />
<input type="button" value="提交" onclick="callServer();" />
</div>
</body>
</html>
function callServer() {
var id = Number($("#id").val());
var title = String($("#title").val());
var content = String($("#content").val());
$.ajax({
cache: false,
async: true,
type: "POST",
dataType: "json",
url: "http://127.0.0.1:3721/employees/InsRow",
data: '{"title":"' + title + '"}',
contentType: "application/json;charset=utf-8",
success: function (msg) {
var a = eval('(' + msg + ')');
if (String(a.d).length > 0) { alert(a.d); }
else { alert("服务器超时"); }
}
});
}
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
namespace Artech.WcfServices.Service.Interface
{
[ServiceContract]
public interface IEmployees
{
[WebGet(UriTemplate = "all",ResponseFormat =WebMessageFormat.Json)]
IEnumerable<Employee> GetAll();
[OperationContract]
string InsertRow(string title);
}
[DataContract]
public class Employee
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public string Grade { get; set; }
}
}
using System.Collections.Generic;
using Artech.WcfServices.Service.Interface;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
namespace Artech.WcfServices.Service
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeesService : IEmployees
{
public IEnumerable<Employee> GetAll()
{
return new List<Employee>
{
new Employee{ Id = "001", Name="张三", Department="开发部", Grade = "G6"},
new Employee{ Id = "002", Name="李四", Department="人事部", Grade = "G7"},
new Employee{ Id = "003", Name="王五", Department="销售部", Grade = "G8"}
};
}
[WebInvoke(UriTemplate = "InsRow",
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string InsertRow(string title)//, string title, string content)
{
return string.Format("您输入的标题是:{0}\n\n", title);
}
}
}