我在许多项目中都使用了 WCF 和 BDC。我通常做的事情如下所述。
1) 创建一个名为 SPCommon 的单独 SharePoint 解决方案
2)添加服务参考
3) 创建 MyAppSettings.cs
public class MyAppSettings
{
public string MyServiceEndPoint
{
get;
set;
}
}
4) 创建 ConfigurationManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Configuration;
using System.Web.Configuration;
using Microsoft.SharePoint.Administration;
namespace MyApp
{
public class ConfigurationManager
{
SPSite site;
public ConfigurationManager(SPSite site)
{
this.site = site;
}
public MyAppSettings GetAppSettings()
{
try
{
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("/", site.WebApplication.Name);
AppSettingsSection appSettingSection = config.AppSettings;
MyAppSettings settings = new MyAppSettings();
settings.MyServiceEndPoint = appSettingSection.Settings["MyServiceEndPoint"].Value;
return settings;
}
catch (Exception ex)
{
// Log Error
}
return null;
}
}
}
5) 创建一个 MyServiceConnection.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyServiceReference;
using System.ServiceModel;
using Microsoft.SharePoint;
namespace MyApp
{
public class MyServiceConnection
{
public const int _maxBufferSize = 2147483647;
public const int _maxReceivedBufferSize = 2147483647;
public const int _maxStringContentLength = 2147483647;
public const int _maxArrayLength = 2147483647;
public const int _maxBytesPerRead = 2147483647;
public const int _maxNameTableCharCount = 2147483647;
public const int _maxDepth = 2147483647;
public static MyServiceProxyClient GetMyServiceProxyClient()
{
SPSite site = SPContext.Current.Site;
// Get the EndPointUrl from Web.config appsetting
ConfigurationManager configMgr = new ConfigurationManager(site);
var appSetting = configMgr.GetAppSettings();
EndpointAddress myServicecrmEndPoint;
if (appSetting != null)
{
myServiceEndPoint = new EndpointAddress(appSetting.MyServiceEndPoint);
var proxy = new MyServiceProxyClient(
new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly)
{
MaxBufferSize = _maxBufferSize,
MaxReceivedMessageSize = _maxReceivedBufferSize,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxStringContentLength = _maxStringContentLength,
MaxArrayLength = _maxArrayLength,
MaxBytesPerRead = _maxBytesPerRead,
MaxNameTableCharCount = _maxNameTableCharCount,
MaxDepth = _maxDepth
},
},
myServiceEndPoint
);
return proxy;
}
else
{
// Log Error
return null;
}
}
}
}
6)在外部列表解决方案中,添加对SPCommon项目的引用
7) 调用服务方法 ReadMyList() 如下
MyServiceProxyClient service = SPCommon.GetMyServiceProxyClient();
listData = service.ReadMyList();
8) 将 appsetting 添加到 Web.config
<appSettings>
<add key="MyServiceEndPoint" value="http://localhost:8101/MyService.svc" />
</appSettings>
9) 确保同时部署 SPCommon 和 External List 解决方案。