在上一節中我們講了如何創建一個簡單WCF Service並以添加新網站的方式發佈到IIS中,這一節我們將如何創建webpart並在webpart中調用WCF Service。
接上一節,第二步,我們先來創建一個webpart。打開VS2010,選擇sharepoint-->2010-->可視web部件,並命名爲WCFClientWebpart
2.點擊確定後,在配置嚮導填入一個已有的sharepoint站點,並選擇部署爲場解決方案。然後點擊完成。
3.右鍵點擊引用,選擇添加服務引用,並將之前創建好的WCF地址填入,在命名空間一欄中填入WebPartWCFService,點擊確定。
4.修改webpart前臺和後臺代碼
前臺VisualWebPart1UserControl.ascx文件內容爲:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="WCFClientWebPart.VisualWebPart1.VisualWebPart1UserControl" %>
<asp:TextBox ID="TextBox1" runat="server" Height="20px" Width="20px"></asp:TextBox>
+
<asp:TextBox ID="TextBox2" runat="server" Height="20px" Width="20px"></asp:TextBox>
=
<asp:TextBox ID="TextBox3" runat="server" Height="20px" Width="20px"></asp:TextBox>
<br/>
<asp:Button ID="Button1" runat="server" Text="求和" onclick="Button1_Click" />
後臺VisualWebPart1UserControl.ascx.cs中的代碼爲:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WCFClientWebPart.WebPartWCFService;
namespace WCFClientWebPart.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "0";
TextBox2.Text = "0";
}
protected void Button1_Click(object sender, EventArgs e)
{
WPServiceClient wpclient = new WPServiceClient();
TextBox3.Text = wpclient.Add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text)).ToString();
wpclient.Close();
}
}
}
5.修改完代碼後,打開項目欄中的app.config文件,並把<system.serviceModel>結點下的內容
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IWPService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:300/WPService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IWPService" contract="WebPartWCFService.IWPService"
name="BasicHttpBinding_IWPService" />
</client>
複製到對應sharepoint webapp下的web.config文件的<system.serviceModel>結點下,然後保存。
web.config文件可以在C:\inetpub\wwwroot\wss\VirtualDirectories這個路徑的對應端口文件夾下找到
6.然後直接在vs中點擊運行調試webpart,打開對應的站點後,編輯當前頁,並插入webpart。
7.保存後,在前兩個textbox中隨便輸入兩個數,就可以調用WCFService並求出兩個數的和。
以上就是一個簡單的在webpart中調用WCF服務的小例子。如果有對此方面問題有興趣或者有疑問,歡迎大家共同探討!