title: How to set encoding in SOAP-Request?
body: <p>I’m trying to consume a webservice in .NET Core 2.0 by using SOAP. The client for the webservice was generated with the “Connected Service”-feature of Visual Studio.</p>
  <p>Now I have the problem, that the webservice needs in the request the (first) line with the encoding-Attribute:</p>
  <p><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</code></p>
  <p>I’ve tried many different ways to write this line in the output-XML, but nothing works.</p>
  <h3>First approach:</h3>
  <p>I added a new endpoint behavior (by implementing IEndpointBehavior) to the client with using a customized message inspector (by implementing IClientMessageInspector). Via debugging I found out, the message object contains the said line. But by writing the outgoing XML to the webservice, the line is missing.</p>
  <h3>Second approach:</h3>
  <p>I wrote a customized message encoding binding element with a wsdl export extension (by using MessageEncodingBindingElement &amp; IWsdlExportExtension) . This binding element uses a customized MessageEncoder where the encoding is set explicitly. I added the binding to the generated client.</p>
  <p>Here are some .net 4.6 code snippets:</p>
  <pre><code>// Binding for the client
  TextMessageBindingElement textBindingElement = new TextMessageBindingElement();
  textBindingElement.Encoding = &quot;utf-8&quot;;
  textBindingElement.MessageVersion = MessageVersion.Soap11;
  textBindingElement.MediaType = &quot;text/xml&quot;;
  
  bindingElements.Add(textBindingElement);
  
  CustomBinding binding = new CustomBinding(bindingElements);
  </code></pre>
  <hr />
  <pre><code>// Customized Message Binding Element for encoding
  public class TextMessageBindingElement : MessageEncodingBindingElement, 
  IWsdlExportExtension {
  
      public override MessageVersion MessageVersion { get; set; }
      public string Encoding { get; set; }
  
      ...
  
      public override MessageEncoderFactory CreateMessageEncoderFactory() {
          return new TextMessageEncoderFactory(MediaType, Encoding, MessageVersion);
      }
  
      void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) {
      }
  
      void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context) {
           TextMessageEncodingBindingElement mebe = new TextMessageEncodingBindingElement();
           mebe.MessageVersion = this.msgVersion;
           ((IWsdlExportExtension) mebe).ExportEndpoint(exporter, context);
      }
  }
  </code></pre>
  <p>In .NET 4.6 this works, but .NET Core 2.0 doesn’t support IWsdlExportExtension. I’ve searched for alternatives, but haven’t found anything yet.</p>
  <p>Please, can anybody help me?</p>