解决方案整体结构如下:
<% @ ServiceHost Language="C#"
Debug="true"
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
Service="spweb.SPWCF, $SharePoint.Project.AssemblyFullName$"
%>
<TokenReplacementFileExtensions>svc</TokenReplacementFileExtensions>
Web.config中也可加入svc(也可以不写)
注意webservice.cs中的空间名与解决方案同名。
Demo.js
function callWCFService(WCFServicesURL) {
$.ajax({
type: "GET",
url: WCFServicesURL,
contentType: "application/json;charset=utf-8",
dataType: 'json',
processdata: true,
success: function (msg) {
WCFServiceSucceeded(msg);
},
error: WCFServiceError
});
}
function WCFServiceSucceeded(result) {
alert(result);
}
function WCFServiceError() {
alert("error");
}
webpart.cs
namespace spweb.wpspweb
{
[ToolboxItemAttribute(false)]
public class wpspweb : WebPart
{
protected override void CreateChildControls()
{
ScriptLink.Register(this.Page, "spweb/jquery-1.8.0.min.js", false);
ScriptLink.Register(this.Page, "spweb/Demo.js", false);
string url = SPContext.Current.Web.Url + this.ResolveUrl("~/_vti_bin/mywcf/WCFDemo.svc/DoWork");
ScriptManager.RegisterStartupScript(this.Page, typeof(wpspweb), "", string.Format("callWCFService('{0}');",url), true);
}
}
}