SharePoint BDC Model and WCF
Here is my scenario..
I have created a BDC model project in VS2010 for deployment in SharePoint2010. I have added a service reference to a WCF service that we have running o开发者_StackOverflow社区n another system. I want my ReadList method to call the WCF service on the other system to pull data to be shown in the list.
I have created a unit test for the ReadList method to verify it works before deploying. The error message that I get is "Could not find default endpoint element that references contract 'TicketsWCF.ITickets' in the ServiceModel client configuration section."
When I add the service reference an app.config is added to the project which appears to have everything that I need for the service to run.
My two questions are
Has anyone gotten a WCF service to a non-sharepoint external system working with BDC
When the model is deployed will the app.config settings get appropriately placed within the sharepoint system?
Unfortunately, I haven't found a way to use an app config file in this case. One way around that is to define your endpoint in code, and possibly save the endpoint address in the farm property bag. Note that you will have to add a Reference to System.ServiceModel.
private static string endpointUri = SPContext.Current.Site.WebApplication.Farm.Properties["Service_Uri_PropertyBag_Key"] as string;
private static EndpointAddress endpoint = new EndpointAddress(endpointUri);
private static BasicHttpBinding binding = new BasicHttpBinding();
Then, making calls to your WCF service would be something similar to:
using (ReferencedServiceClient client = new ReferencedServiceClient(binding, endpoint))
{
return client.ReadList();
}
I have used WCF working with BDC in many projects. What I usually do is described below.
1) Create a separate SharePoint solution called SPCommon
2) Add Service Reference
3) Create MyAppSettings.cs
public class MyAppSettings
{
public string MyServiceEndPoint
{
get;
set;
}
}
4) Create 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) Create a MyServiceConnection.cs file
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) In the external list solution, add reference to SPCommon project
7) Call the service method ReadMyList() as below
MyServiceProxyClient service = SPCommon.GetMyServiceProxyClient();
listData = service.ReadMyList();
8) Add appsetting to Web.config
<appSettings>
<add key="MyServiceEndPoint" value="http://localhost:8101/MyService.svc" />
</appSettings>
9) Make sure to deploy both SPCommon and External List solutions.
精彩评论