2010-06-30 61 views
0

我有一個使用WCF服務的SharePoint Web部件。爲了能夠使用我的Web部件中的Web服務,我需要修改SharePoint web.config以包含綁定和端點。使用SPWebConfigModification將Web服務綁定添加到SharePoint web.config

這樣做的最好方法是什麼?

+2

嗨,沒有。這看起來像有用的代碼,但我們更願意將問題和答案分開。從常見問題解答:「只要你假裝你在危險之中,問問並回答你自己的問題也是完全正確的:以問題的形式說出它。」那麼你可以在問題表單中重新提出這個問題,並將你的代碼作爲答案嗎?這樣,其他人可能會發布其他解決方案並對其用途進行投票。 – 2010-12-01 16:41:27

+0

請對邁克的建議採取行動 - 因爲它的立場,這個問題是不適合的。 – 2010-12-01 18:54:45

+0

更新了問題和答案。 – Umit 2011-08-24 12:08:34

回答

0

爲了能夠做到這一點,我把我的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); 

我花了一些時間計算出來。希望這會幫助某人。

1

這是非常有用的,但它錯過了一點。鑑於代碼可以部署,因爲名稱未分配,所以無法收回。

用途:

modification.Name = "bindings"; 

而且,話說回來,這是綁定,你(可能)不能仍不能如果已經設置那裏應用設置:

serviceHostingEnvironment aspNetCompatibilityEnabled="true" 

...內system.serviceModel

我已經使用該技術來插入綁定,然後客戶端點分別插入,因爲這可以根據in在我的情況下,通過共享點列表條目設置。

相關問題