【问题标题】:SharePoint BDC Model and WCFSharePoint BDC 模型和 WCF
【发布时间】:2011-09-09 16:28:26
【问题描述】:

这是我的场景..

我在 VS2010 中创建了一个 BDC 模型项目,用于在 SharePoint2010 中进行部署。我添加了对我们在另一个系统上运行的 WCF 服务的服务引用。我希望我的 ReadList 方法调用其他系统上的 WCF 服务来提取要显示在列表中的数据。

我为 ReadList 方法创建了一个单元测试,以在部署之前验证它是否有效。我收到的错误消息是“在 ServiceModel 客户端配置部分中找不到引用合同 'TicketsWCF.ITickets' 的默认端点元素。”

当我添加服务引用时,一个 app.config 被添加到项目中,该项目似乎拥有运行服务所需的一切。

我的两个问题是

  1. 有没有人为使用 BDC 的非共享点外部系统提供 WCF 服务

  2. 部署模型后,app.config 设置是否会正确放置在共享点系统中?

【问题讨论】:

  • 如果您在 VS2010 中创建 BDC 模型项目,您可能正在使用 BDC .NET 程序集连接器。您能否确认这是您的设置 BDC -> .NET 程序集连接器 -> WCF -> 外部系统?我有它的工作,但我的设置是 BDC -> WCF -> 外部系统。
  • 如果要创建使用 WCF 的 BDC,请使用 SharePoint Designer 开始。这是一个例子Business Connectivity Services (BCS) Using WCF & SharePoint Designer 2010

标签: wcf visual-studio-2010 sharepoint-2010 bdc


【解决方案1】:

不幸的是,在这种情况下,我还没有找到使用应用配置文件的方法。解决此问题的一种方法是在代码中定义您的端点,并可能将端点地址保存在农场属性包中。请注意,您必须添加对 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();

然后,调用您的 WCF 服务将类似于:

        using (ReferencedServiceClient client = new ReferencedServiceClient(binding, endpoint))
        {
            return client.ReadList();
        }

【讨论】:

    【解决方案2】:

    我在许多项目中都使用了 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 和外部列表解决方案。

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      相关资源
      最近更新 更多