天天看點

SharePoint 2010之部署WCF(deploy WCF service to SharePoint 2010 via VS 2010 step by step)

現将在sharepoint 2010上部署WCF方法和步驟整理如下:

·         在sharepoint project中建立WCF檔案,保證運作正常

·         增加svc檔案到layouts檔案夾

   a) Add SharePoint “Layouts” Mapped Folder --> add a xml file named "WCFService.svc"

   b)替換上述建立的svc檔案内容:

  <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"%> 

   <% @ServiceHost Service="MyProject.MyService" %>

·          進到C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v10.0/SharePointTools 目錄,打開

Microsoft.VisualStudio.SharePoint.targets 檔案,找到TokenReplacementFileExtensions 節點,在後面增加svc到檔案擴充清單中。示例如下:

<TokenReplacementFileExtensions>$(TokenReplacementFileExtensions);xml;aspx;ascx;webpart;dwp;svc </TokenReplacementFileExtensions>

·         在sharepoint站點中增加WCF service

      1)打開要部署站點的web.config檔案.路徑類似如下

     path:C:/inetpub/wwwroot/wss/VirtualDirectories/80/web.config

2)打開project中的app.config(WCF配置檔案),拷貝<system.serviceModel>節點中所有内容,内容類似如下:

<system.serviceModel>

<!--the following content need to be copied and pasted into the corresponding element in web.config-->

<behaviors>

<serviceBehaviors>

<behavior name="WCFDeployment.WCFDeploy2SPBehavior">

<serviceMetadata httpGetEnabled="true" />

<serviceDebug includeExceptionDetailInFaults="false" />

</behavior>

</serviceBehaviors>

</behaviors>

<services>

<service behaviorConfiguration="WCFDeployment.WCFDeploy2SPBehavior"

name="WCFDeployment.WCFDeploy2SP">

<endpoint address="" binding="wsHttpBinding" contract="WCFDeployment.IWCFDeploy2SP">

<identity>

<dns value="localhost" />

</identity>

</endpoint>

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

<host>

<baseAddresses>

<add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFDeployment/WCFDeploy2SP/" />

</baseAddresses>

</host>

</service>

</services>

<!--copy end-->

</system.serviceModel>

            3) 在第一步中的web.config中找到 system.serviceModel 節點粘貼第2步中拷貝内容

·         在檔案中修改baseAddress内容,修改後在web.config檔案中内容如下

<system.serviceModel>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

<!--add the configured information for WCF service -->

<behaviors>

<serviceBehaviors>

<behavior name="WCFDeployment.WCFDeploy2SPBehavior">

<serviceMetadata httpGetEnabled="true" />

<serviceDebug includeExceptionDetailInFaults="false" />

</behavior>

</serviceBehaviors>

</behaviors>

<services>

<service behaviorConfiguration="WCFDeployment.WCFDeploy2SPBehavior"

name="WCFDeployment.WCFDeploy2SP">

<endpoint address="" binding="wsHttpBinding" contract="WCFDeployment.IWCFDeploy2SP">

<identity>

<dns value="localhost" />

</identity>

</endpoint>

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

<host>

<baseAddresses>

<add baseAddress="http://localhost/_layouts/WCFDeployment/WCFDeploy2SP/" />

</baseAddresses>

</host>

</service>

</services>

<!--end-->

</system.serviceModel>

·         儲存所有内容後,編譯部署

·         測試。 打開浏覽器,輸入:http://localhost/_layouts/WCFDeployment/DeploymentWCF.svc

問題處理:

如果不能檢視,打開event view,檢視錯誤原因,我在這裡羅列自己遇到的2個問題,供參考

error 1. 有關asp.net compatibility的問題

解決辦法:

修改WCF的service類加上asp.net compatibility。

如下:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

//[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] -->this is the old. it is replaced by the sectence in above

public class WCFDeploy2SP : IWCFDeploy2SP

{...}

error 2: 有關登入權限的問題,需要enable匿名登入

打開IIS-->到站點,點選authentication, enable the anonymous authentication.

就這麼多了。上述内容已經經過驗證。

繼續閱讀