wcf.net-corebindingmtom

The value mtom is not supported in this context for the binding property message


this is an issue I was facing once converting Asp .Net function application which consumed WCF service, to .Net core. WCF service was expecting the HTTP content with encoding type mtom , but it wasn't supported by .Net core, so it issued the exception, "The value mtom is not supported in this context for the binding property message" once the WCF service is called,

I will be attaching the workaround as well below


Solution

  • Install WcfCoreMtomEncoder nuget package then in the reference file when you create bindings for the endpoint, create basicHttpBinding

    var transportSecurityBindingElement = new BasicHttpBinding();
    

    set desired security and transport mode

    transportSecurityBindingElement.Security.Mode =BasicHttpSecurityMode.Transport; 
    transportSecurityBindingElement.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;
    

    create object with type of custom binding

    var customTransportSecurityBinding = new CustomBinding(transportSecurityBindingElement);
    

    create encoding element using mtomMessage encoder

     var encodingElement = new MtomMessageEncoderBindingElement(new TextMessageEncodingBindingElement());
    

    find and replace the encoding property by iterating the element list of binding

    for (int i = 0; i < customTransportSecurityBinding.Elements.Count; i++)
                    {
                        if (customTransportSecurityBinding.Elements[i] is TextMessageEncodingBindingElement) {
                            customTransportSecurityBinding.Elements[i] = encodingElement;
                            break;
                        }
                    }
    

    return the custom binding , so it can be used from anywhere

      return customTransportSecurityBinding