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.