lundi 28 mai 2018

C# How to Update Visual Studio 2107 xxx.exe.config binding at runtime?

A xxx.exe.config file is created when I compile my C# web service code. However, I want to modify one of the bindings at run time. An example of the xxx.exe.config file generated by Visual Studio is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ContentService"
                         messageEncoding="Mtom" />
            </basicHttpBinding>
        </bindings>
    </system.serviceModel>
</configuration>

I want to add some parameters at run time to the binding named "BasicHttpBinding_ContentService" that are equivalent to manually coding:

<binding name="BasicHttpBinding_ContentService"                             
              messageEncoding="Mtom"
              maxBufferSize="5000000"
              maxBufferPoolSize="524288"
              maxReceivedMessageSize="2147483648"
              transferMode="Streamed" />

I think the C# code to define the parameter settings looks like:

using System.ServiceModel;

BasicHttpBinding myBinding = new BasicHttpBinding("BasicHttpBinding_ContentService");
myBinding.MaxBufferSize = 5000000;
myBinding.MaxBufferPoolSize = 524288;
myBinding.MaxReceivedMessageSize = 2147483648;
myBinding.TransferMode = TransferMode.Streamed;

But I can't find a C# example (that works) that applies 'myBinding' to my Visual Studio .config file. The samples I do find reference classes like ServiceHost and ServiceClient that are undefined in my environment.

Note - I am using .NET 4.6.




Aucun commentaire:

Enregistrer un commentaire