我已經使用WCF在很多項目中與BDC工作。我通常做的事情在下面描述。
1)創建名爲SPCommon
2的單獨的SharePoint溶液)添加服務引用
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)在外部列表的解決方案,如下面
MyServiceProxyClient service = SPCommon.GetMyServiceProxyClient();
listData = service.ReadMyList();
8)添加到appsetting web.config中SPCommon項目
7)調用服務方法ReadMyList()添加參考
<appSettings>
<add key="MyServiceEndPoint" value="http://localhost:8101/MyService.svc" />
</appSettings>
9)確保同時部署SPCommon和External List解決方案。
如果您在VS2010中創建BDC模型項目,則可能使用的是BDC .NET Assembly Connector。你能確認這是你的設置BDC - > .NET Assembly Connector - > WCF - > External System嗎?我有它的工作,但我的設置是BDC - > WCF - >外部系統。 – MLF 2011-06-07 15:25:09
如果要創建使用WCF的BDC,請使用SharePoint Designer讓您開始。以下是一個示例[使用WCF和SharePoint Designer 2010的Business Connectivity Services(BCS)](http://malikhan.wordpress.com/2010/01/11/business-connectivity-services-bcs-using-wcf-sharepoint-designer -2010 /) – MLF 2011-06-07 17:22:10