【问题标题】:Adding web service bindings into SharePoint web.config using SPWebConfigModification使用 SPWebConfigModification 将 Web 服务绑定添加到 SharePoint web.config
【发布时间】:2012-03-09 11:03:21
【问题描述】:

我有一个使用 WCF 服务的 SharePoint Web 部件。为了能够在我的 Web 部件中使用 Web 服务,我需要修改 SharePoint web.config 以包含绑定和端点。

最好的方法是什么?

【问题讨论】:

  • 嗨,嗯。这看起来像是有用的代码,但我们更喜欢将问题和答案分开。来自常见问题解答:“问和回答你自己的问题也很好,只要你假装你在危险中:以问题的形式表达它。”那么您能否以问题形式重新表述这一点并将您的代码作为答案发布?这样一来,其他人就可以发布其他解决方案并对其有用性进行投票。
  • 请对 Mike 的建议采取行动 - 就目前而言,这个问题不适合 SO。
  • 更新了问题和答案。

标签: web-services sharepoint binding web-config


【解决方案1】:

这非常有用,但它遗漏了一点。虽然代码可以部署,但由于未指定名称,因此无法撤回。

用途:

modification.Name = "bindings";

另外,虽然说这是绑定,但如果已经存在以下设置,您(可能)仍然不能应用设置:

serviceHostingEnvironment aspNetCompatibilityEnabled="true"  

...在system.serviceModel

我已使用该技术插入绑定,然后单独插入客户端端点,因为这可以根据安装进行更改,并且在我的情况下是通过共享点列表条目设置的。

【讨论】:

    【解决方案2】:

    为了能够做到这一点,我将我的 Web 服务配置作为模板放入一个文本文件中。文本文件(BindingTemplate.txt)内容如下:

    <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_AuthenticationInterface" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://{0}/MyWebService/AuthenticationService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_AuthenticationInterface" contract="AuthenticationService.AuthenticationInterface" name="BasicHttpBinding_AuthenticationInterface" />
    </client>
    

    我用C#下面的代码来修改web.config:

    string content;
    string WebServiceServer = "example.com"; // <=== your host-name here
    using (TextReader tr = File.OpenText(bindingFilePath))
    {
        content = String.Format(tr.ReadToEnd(), WebServiceServer);
    }
    
    SPWebConfigModification modification = new SPWebConfigModification("system.serviceModel", "configuration");
    modification.Value = content;
    modification.Sequence = 0;
    modification.Type =SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    modification.Owner = OWNER_CONSTANT;
    
    webApp.WebConfigModifications.Add(modification);
    

    我花了一些时间弄清楚。希望这会对某人有所帮助。

    【讨论】:

    • 嗨@Umit,我将在我的EventReceiver 项目中使用Web 服务。当我的功能被激活或其他地方时,是否可以以编程方式将 Web 服务的端点添加到 web.config?
    猜你喜欢
    • 2011-04-02
    • 2013-03-16
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多