如何在 .Net Core 项目中使用 WcfCoreMtomEncoder? [英] How do I use WcfCoreMtomEncoder in a .Net Core project?

查看:63
本文介绍了如何在 .Net Core 项目中使用 WcfCoreMtomEncoder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 .Net Core 项目中使用这个 WcfCoreMtomEncoder lib here,但我不确定如何在语法上使用它.

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

我在下面有这个代码但不能使用 MessageEncoding 因为我在一个 .Net Core 项目中(不支持 mtom):

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();

从我读到的,我可以用下面的 library 代码替换它,我从文档中看到用法如下所示的编码器库:

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.

推荐答案

经过一番挣扎,我设法创建了一个可以被类库使用的 WCF 服务.但它只支持自定义绑定.请参考下面的例子.
服务端(基于Dotnet Framework 4.7.2的控制台应用)

After some struggles, I managed to create a WCF service that could be consumed by the class library. But it only supports the Custombinding. Please refer to the below example.
Server-side (a console application based on Dotnet Framework 4.7.2)

class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("http://localhost:21011");
            MtomMessageEncodingBindingElement encoding = new MtomMessageEncodingBindingElement();
            var transport = new HttpTransportBindingElement();
            transport.TransferMode = TransferMode.Streamed;
            var binding = new CustomBinding(encoding, transport);
            using (ServiceHost sh = new ServiceHost(typeof(MyService), uri))
            {
                sh.AddServiceEndpoint(typeof(IService), binding, "");
                ServiceMetadataBehavior smb;
                smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                {
                    smb = new ServiceMetadataBehavior()
                    {
                        HttpGetEnabled = true
                    };
                    sh.Description.Behaviors.Add(smb);
                }
                Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
                sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");


                sh.Opened += delegate
                {
                    Console.WriteLine("Service is ready");
                };
                sh.Closed += delegate
                {
                    Console.WriteLine("Service is clsoed");
                };
                sh.Open();
                Console.ReadLine();
                //pause
                sh.Close();
                Console.ReadLine();
            }
        }
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Test();

    }
    public class MyService : IService
    {
        public string Test()
        {
            return DateTime.Now.ToLongTimeString();
        }
}

客户端(基于核心的控制台应用程序,带有 WcfCoreMtomEncoder nuget 包,使用 ChannelFactory 调用服务).

Client-side (Core-based Console application with WcfCoreMtomEncoder nuget package, calling the service by using ChannelFactory).

    class Program
    {
        static void Main(string[] args)
        {
            var encoding = new MtomMessageEncoderBindingElement(new TextMessageEncodingBindingElement());
            var transport = new HttpTransportBindingElement();
            transport.TransferMode = TransferMode.Streamed;
            var binding = new CustomBinding(encoding, transport);
            EndpointAddress endpoint = new EndpointAddress("http://vabqia969vm:21011");
            ChannelFactory<IService> channelFactory = new ChannelFactory<IService>(binding, endpoint);
            var webService = channelFactory.CreateChannel();
            Console.WriteLine(webService.Test());
        }
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Test();

}

还有一点需要注意的是,如果服务器使用传输安全模式来保护通信,我们应该手动将证书绑定到服务器端的特定端口.

One more thing we pay attention to is that we should manually bind a certificate to the particular port on the server-side if the server using Transport security mode to secure the communication.

netsh http 添加 sslcert ipport=0.0.0.0certhash=0102030405060708090A0B0C0D0E0F1011121314appid={00112233-4455-6677-8899-AABBCCDDEEFF}

Netsh http add sslcert ipport=0.0.0.0 certhash=0102030405060708090A0B0C0D0E0F1011121314 appid={00112233-4455-6677-8899-AABBCCDDEEFF}

在上面的例子中,我将一个名为 vabqia969vm 主题(DNS)的证书绑定到机器(主机名是 vabqia969vm).这是一些官方链接.
https://docs.microsoft.com/en-us/windows/win32/http/add-sslcert
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate
在客户端,在调用服务之前,我们应该建立信任关系,以便客户端和服务器端之间可以进行通信.因此,我在客户端 LocalCA(证书存储中的受信任的根证书颁发机构)上安装了服务器证书.或者,我们可以手动添加证书验证过程.

In the above example, I bind a certificate that has a named vabqia969vm subject(DNS) to the machine(hostname is vabqia969vm). Here are some official links.
https://docs.microsoft.com/en-us/windows/win32/http/add-sslcert
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate
On the client-side, before making a call to the service, we should establish a trust relationship so that communication is available between the client-side and the server-side. Therefore, I install the server certificate on the client-side LocalCA(Trusted Root Certification Authorities in the certification store). Alternatively, we could manually add a certificate validation process.

ChannelFactory<IService> channelFactory = new ChannelFactory<IService>(binding, endpoint);
            channelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication()
            {
                CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None,
                RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck
            };

如果有什么我可以帮忙的,请随时告诉我.
已更新.
我已经更新了上面的例子,它可以在 HTTP 协议上正常工作.

Feel free to let me know if there is anything I can help with.
Updated.
I have updated the above example which works properly over HTTP protocol.

这篇关于如何在 .Net Core 项目中使用 WcfCoreMtomEncoder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆