0

I am dynamically adding bindings in web.config. But I don't know how to remove it

Here is complete code

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPFarm farm = SPFarm.Local;

        webApp = properties.Feature.Parent as SPWebApplication;

        string endpointadr = "https://testserver/myservice.svc"


        string bindings = @"
                            <bindings>
                              <wsHttpBinding>        
                                <binding name=""WSHttpBinding_IAuthenticationService"">
                                  <security mode=""TransportWithMessageCredential"">
                                    <transport clientCredentialType=""None"" />
                                    <message clientCredentialType=""UserName"" establishSecurityContext=""false"" />
                                  </security>
                                </binding>
                              </wsHttpBinding>
                            </bindings>
                            <client>      
                            <endpoint address="""+endpointadr+ @""" binding=""wsHttpBinding"" bindingConfiguration=""WSHttpBinding_IAuthenticationService""
                            contract=""testService.IMyService"" name=""WSHttpBinding_IAuthenticationService"" />
                            </client>";


        webApp = properties.Feature.Parent as SPWebApplication;
        RemoveModifications();
        webApp.WebConfigModifications.Add(AddBindings(bindings));

        webApp.Update();
        webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();


    }



    private SPWebConfigModification AddBindings(string bindingString)
    {
        var modification = new SPWebConfigModification("bindings", "configuration/system.serviceModel");
        modification.Value = bindingString;
        //modification.Sequence = 0;
        modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
        modification.Owner = Owner;
        return modification;
    }

    private string Owner
    {
        get { return "MyTestPage" + webApp.Id; }
    }

    private void RemoveModifications()
    {
        var mods = from m in webApp.WebConfigModifications.AsEnumerable() where m.Owner == Owner select m;

        while (mods.Count() != 0)
        {
            webApp.WebConfigModifications.Remove(mods.First());
        }
    }

It's not removing anything when I call removeModification

Update 1 In web.config its adding like this

<client>
  <endpoint address="https://testserver/myservice.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAuthenticationService" contract="Myservice.IMyService" name="WSHttpBinding_IAuthenticationService" />
</client>
                            <bindings>
                              <wsHttpBinding>        
                                <binding name="WSHttpBinding_IAuthenticationService">
                                  <security mode="TransportWithMessageCredential">
                                    <transport clientCredentialType="None" />
                                    <message clientCredentialType="UserName" establishSecurityContext="false" />
                                  </security>
                                </binding>
                              </wsHttpBinding>
                            </bindings>
                            <client>      
                            <endpoint address="https://testserver/myservice.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAuthenticationService" contract="MyService.IMyService" name="WSHttpBinding_IAuthenticationService" />
                            </client>

Not sure why its adding client tag twice

1 Answer 1

0

You should change your approach as below

SPWebConfigModification entry = new SPWebConfigModification();
entry.Path = "configuration";
entry.Name = "bindings";
entry.Sequence = 0;
entry.Owner = "YourFeatureName";
entry.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
entry.Value =  @"<bindings>
                          <wsHttpBinding>        
                            <binding name=""WSHttpBinding_IAuthenticationService"">
                              <security mode=""TransportWithMessageCredential"">
                                <transport clientCredentialType=""None"" />
                                <message clientCredentialType=""UserName"" establishSecurityContext=""false"" />
                              </security>
                            </binding>
                          </wsHttpBinding>
                        </bindings>
                        <client>      
                        <endpoint address="""+endpointadr+ @""" binding=""wsHttpBinding"" bindingConfiguration=""WSHttpBinding_IAuthenticationService""
                        contract=""testService.IMyService"" name=""WSHttpBinding_IAuthenticationService"" />
                        </client>";
webApplication.WebConfigModifications.Add (entry); 

Now for removing you can use

Collection<SPWebConfigModification> allModifications = webApplication.WebConfigModifications;
int initialCount = allModifications.Count;
for (int i = initialCount - 1; i >= 0; i--)
{
    SPWebConfigModification Testmodification = allModifications [i];
    if (Testmodification.Owner == "YourFeatureName")
        allModifications.Remove(Testmodification);
}
if (initialCount > allModifications.Count)
{
    webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();                
    webApplication.Update();
}   
7
  • i get error on webApplication.count .. cannot resolve symbol count
    – Happy
    Jun 9, 2015 at 12:51
  • Sorry it should be int initialCount = allModifications.Count; Jun 9, 2015 at 12:53
  • Doesnt remove anything
    – Happy
    Jun 9, 2015 at 13:02
  • Can you try debugging and see code is getting executed and the if condition is met. Jun 9, 2015 at 13:03
  • I have not included entry.Sequence = 0; in my code because if i do then it removed this <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> under system.serviceModel
    – Happy
    Jun 9, 2015 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.