Web services 使用SPWebConfigModification将web服务绑定添加到SharePoint web.config

Web services 使用SPWebConfigModification将web服务绑定添加到SharePoint web.config,web-services,sharepoint,binding,web-config,Web Services,Sharepoint,Binding,Web Config,我有一个使用WCF服务的SharePoint web部件。为了能够使用web部件中的web服务,我需要修改SharePoint web.config以包含绑定和端点 最好的方法是什么?为了能够做到这一点,我将web服务配置作为模板放入文本文件中。文本文件(BindingTemplate.txt)的内容如下所示: <system.serviceModel> <bindings> <basicHttpBinding> <binding name=

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


最好的方法是什么?

为了能够做到这一点,我将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>

我使用以下代码修改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);
字符串内容;

字符串WebServiceServer=“example.com”;// 为了能够做到这一点,我将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>

我使用以下代码修改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);
字符串内容;

字符串WebServiceServer=“example.com”;// 这是非常有用的,但它错过了一点。虽然代码可以部署,但无法收回,因为没有指定名称

使用:

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

serviceHostingEnvironment aspNetCompatibilityEnabled="true"  
。。。在
系统内。serviceModel


我使用该技术插入绑定,然后单独插入客户端端点,因为这可能会根据安装情况而改变,在我的情况下,通过sharepoint列表条目进行设置。

这非常有用,但有点遗漏。虽然代码可以部署,但无法收回,因为没有指定名称

使用:

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

serviceHostingEnvironment aspNetCompatibilityEnabled="true"  
。。。在
系统内。serviceModel


我已经使用这种技术插入了绑定,然后单独插入了客户端端点,因为这可能会根据安装而改变,在我的情况下,是通过sharepoint列表条目设置的。

Hi,umit。这看起来像是有用的代码,但我们更喜欢将问题和答案分开。在常见问题中:“只要你假装身处危险之中,问和回答你自己的问题也很好:用问题的形式表达出来。”那么,你能用问题的形式重新表述这一点,并将你的代码作为答案发布吗?这样,其他人可能会发布其他解决方案并对其有用性进行投票。请对Mike的建议采取行动-目前来看,这个问题不适合SO。更新了问题和答案。嗨,umit。这看起来像是有用的代码,但我们更喜欢将问题和答案分开。在常见问题中:“只要你假装身处危险之中,问和回答你自己的问题也很好:用问题的形式表达出来。”那么,你能用问题的形式重新表述这一点,并将你的代码作为答案发布吗?这样,其他人可能会发布其他解决方案并对其有用性进行投票。请对Mike的建议采取行动-目前来看,这个问题不适合这样做。更新了问题和答案。您好@Umit,我将在我的EventReceiver项目中使用web服务。当我的功能被激活或在其他地方时,是否可以通过编程将web服务的端点添加到web.config?您好@Umit,我将在我的EventReceiver项目中使用web服务。当我的功能被激活或在其他地方时,是否可以通过编程将web服务的端点添加到web.config?