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

use*_*050 5 c# wcf mtom .net-core

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();
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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.

ale*_*_si 6

请按如下方式进行:

  1. 使用 WCF 生成服务客户端,这将生成命名空间和部分类,例如DocumentWebServiceClient
  2. 在同一项目和命名空间中,创建一个文件来扩展分部类并实现ConfigureEndpoint用于端点配置任务的方法:
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://learn.microsoft.com/en-us/dotnet/framework/wcf/extending/custom-bindings
        var cb = new CustomBinding(elementsWithoutEncodingElement.Prepend(newEncodingElement));
        serviceEndpoint.Binding = cb;
    }
}
Run Code Online (Sandbox Code Playgroud)


div*_*481 0

你的.net core客户端代码应该是这样的

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


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