Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use WcfCoreMtomEncoder in a .Net Core project?

I want to use this WcfCoreMtomEncoder lib here in my .Net Core project but I'm not sure how to use it syntactically.

I have this code below but can't use MessageEncoding because I'm in a .Net Core project (no mtom support):

var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport)
{ 
    // MessageEncoding = WSMessageEncoding.Mtom, not supported in .Net Core
    TransferMode = TransferMode.Streamed
};

EndpointAddress endpoint = new EndpointAddress(url);
var channelFactory = new ChannelFactory<T>(binding, endpoint);
var webService = channelFactory.CreateChannel();
user.UserName = await webService.EncryptValueAsync(userName);
user.Password = await webService.EncryptValueAsync(password);
var documentAddResult = webService.DocumentAdd(document);
channelFactory.Close();

From what I read I can replace it with this library code below and I see from the documentation for the encoder lib that the usage looks like this:

var encoding = new MtomMessageEncoderBindingElement(new TextMessageEncodingBindingElement());
var transport = new HttpTransportBindingElement();
var customBinding = new CustomBinding(encoding, transport);

var client = new MtomEnabledServiceClient(customBinding);

but I'm not sure what's what here? How would it be used to perform the document upload I'm trying to achieve? And is the library doing this or am I misunderstanding what it does?

If anyone can provide me an example of how to use this library to perform the document upload it would be appreciated.

like image 759
user1186050 Avatar asked Jan 06 '20 21:01

user1186050


People also ask

Is .NET Core in C#?

NET Core platform such as mobile, desktop, web, cloud, IoT, machine learning, microservices, game, etc. Supports Multiple Languages: You can use C#, F#, and Visual Basic programming languages to develop .

What is .NET Core used for?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.

What is MTOM in WCF?

MTOM is a mechanism for transmitting large binary attachments with SOAP messages as raw bytes, allowing for smaller messages. By default, the WSHttpBinding sends and received messages as normal text XML.


Video Answer


1 Answers

Proceed as follows:

  1. Generate the service client using WCF, this will result in a namespace and partial class, say DocumentWebServiceClient,
  2. in the same project and namespace, create a file to extend the partial class and implement the ConfigureEndpoint method which is intended for endpoint configuration tasks:
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using WcfCoreMtomEncoder;

public partial class DocumentWebServiceClient // DocumentWebServiceClient is generated by the WCF
{
    static partial void ConfigureEndpoint(ServiceEndpoint serviceEndpoint, ClientCredentials clientCredentials)
    {
        var messageEncodingBindingElementType = typeof(MessageEncodingBindingElement);
        var elements = serviceEndpoint.Binding.CreateBindingElements();

        IEnumerable<BindingElement> elementsWithoutEncodingElement = elements.Where(item => !messageEncodingBindingElementType.IsAssignableFrom(item.GetType()));
        var existingEncodingElement = (MessageEncodingBindingElement)elements.Where(item => messageEncodingBindingElementType.IsAssignableFrom(item.GetType())).First();

        var newEncodingElement = new MtomMessageEncoderBindingElement(existingEncodingElement);

        // Encoding is before transport, so we prepend the MTOM message encoding binding element
        // https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/custom-bindings
        var cb = new CustomBinding(elementsWithoutEncodingElement.Prepend(newEncodingElement));
        serviceEndpoint.Binding = cb;
    }
}
like image 149
aleksander_si Avatar answered Sep 30 '22 14:09

aleksander_si