在 .Net Core 3.1 中使用 WCF 服务

Posted

技术标签:

【中文标题】在 .Net Core 3.1 中使用 WCF 服务【英文标题】:Using WCF Service in .Net Core 3.1 【发布时间】:2021-09-28 01:47:19 【问题描述】:

我正在将 .Net framework 4 中的 Web API 转换为 .NET Core 3.1,并且在 API 中我正在使用供应商 WCF 服务。 WCF 服务使用相互证书身份验证以及 basicHttpBinding 和 customBinding。我可以在项目的“连接服务”中通过 wsdl 添加代理类。但我正在努力转换 appsettings.json 中旧 web.config 中的配置部分以及如何在 startup.cs 中使用它。在旧 Web.config 的 配置部分下方(来自 .Net Framework 4 中的 Web API)

<system.serviceModel>
    <services />
    <behaviors>
        <endpointBehaviors>
            <behavior name="hz.behavior.rdy">
                <clientCredentials>
                    <clientCertificate findValue="TestWCF" storeLocation="LocalMachine" storeName="Root" x509FindType="FindBySubjectName" />
                    <serviceCertificate>
                        <defaultCertificate findValue="de607fe9qwrqwrtqtqetqetq74bf292ebe87c0dbbe3716" storeLocation="LocalMachine" storeName="Root" x509FindType="FindByThumbprint" />
                    </serviceCertificate>
                </clientCredentials>
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors />
    </behaviors>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IDebitCard" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
            <binding name="BasicHttpBinding_IAuthentication" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
        <wsHttpBinding>
            <binding name="wsHttpEndpoint" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" >
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                <security mode="TransportWithMessageCredential">
                    <transport clientCredentialType="None" />
                    <message clientCredentialType="UserName" establishSecurityContext="false" />
                </security>
            </binding>
        </wsHttpBinding>
        <customBinding>
            <binding name="BalanceInquiryServiceSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
                <textMessageEncoding messageVersion="Soap12" />
                <security defaultAlgorithmSuite="Basic128Rsa15" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" securityHeaderLayout="Lax" allowInsecureTransport="false" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" requireSecurityContextCancellation="false" />
                <httpsTransport />
            </binding>
            <binding name="CardStatusChangeServiceSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
                <textMessageEncoding messageVersion="Soap12" />
                <security defaultAlgorithmSuite="Basic128Rsa15" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" securityHeaderLayout="Lax" allowInsecureTransport="false" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" requireSecurityContextCancellation="false" />
                <httpsTransport />
            </binding>
            <binding name="CardNumberInquiryServiceSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
                <textMessageEncoding messageVersion="Soap12" />
                <security defaultAlgorithmSuite="Basic128Rsa15" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" securityHeaderLayout="Lax" allowInsecureTransport="false" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" requireSecurityContextCancellation="false" />
                <httpsTransport />
            </binding>
            <binding name="PinInquiryServiceSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
                <textMessageEncoding messageVersion="Soap12" />
                <security defaultAlgorithmSuite="Basic128Rsa15" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" securityHeaderLayout="Lax" allowInsecureTransport="false" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" requireSecurityContextCancellation="false" />
                <httpsTransport />
            </binding>
            <binding name="LoadUnloadPurseServiceSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
                <textMessageEncoding messageVersion="Soap12" />
                <security defaultAlgorithmSuite="Basic128Rsa15" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" securityHeaderLayout="Lax" allowInsecureTransport="false" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" requireSecurityContextCancellation="false" />
                <httpsTransport />
            </binding>
            <binding name="TransactionHistoryServiceSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
                <textMessageEncoding messageVersion="Soap12" />
                <security defaultAlgorithmSuite="Basic128Rsa15" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" securityHeaderLayout="Lax" allowInsecureTransport="false" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" requireSecurityContextCancellation="false" />
                <httpsTransport maxReceivedMessageSize="2147483647" />
            </binding>
            <binding name="CustomerInfoUpdateServiceSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
                <textMessageEncoding messageVersion="Soap12" />
                <security defaultAlgorithmSuite="Basic128Rsa15" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" securityHeaderLayout="Lax" allowInsecureTransport="false" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" requireSecurityContextCancellation="false" />
                <httpsTransport maxReceivedMessageSize="2147483647" />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAuthentication" contract="AuthWS.IAuthentication" name="AuthWs.IAuthentication" />
        <endpoint address="https://services.debitcardprovider.com/hzwbsvc/services/BalanceInquiryServicePort" binding="customBinding" bindingConfiguration="BalanceInquiryServiceSoapBinding" contract="BalanceInquiryService.BalanceInquiryService" name="BalanceInquiryServicePort" behaviorConfiguration="hz.behavior.rdy" />
        <endpoint address="https://services.debitcardprovider.com/hzwbsvc/services/CardStatusChangeServicePort" binding="customBinding" bindingConfiguration="CardStatusChangeServiceSoapBinding" contract="CardStatusChangeService.CardStatusChangeService" name="CardStatusChangeServicePort" behaviorConfiguration="hz.behavior.rdy" />
        <endpoint address="https://services.debitcardprovider.com/hzwbsvc/services/CardNumberInquiryServicePort" binding="customBinding" bindingConfiguration="CardNumberInquiryServiceSoapBinding" contract="CardNumberInquiryService.CardNumberInquiryService" name="CardNumberInquiryServicePort" behaviorConfiguration="hz.behavior.rdy" />
        <endpoint address="https://services.debitcardprovider.com/hzwbsvc/services/PinInquiryServicePort" binding="customBinding" bindingConfiguration="PinInquiryServiceSoapBinding" contract="PinInquiryService.PinInquiryService" name="PinInquiryServicePort" behaviorConfiguration="hz.behavior.rdy" />
        <endpoint address="https://services.debitcardprovider.com/hzwbsvc/services/LoadUnloadPurseServicePort" binding="customBinding" bindingConfiguration="LoadUnloadPurseServiceSoapBinding" contract="LoadUnloadPurseService.LoadUnloadPurseService" name="LoadUnloadPurseServicePort" behaviorConfiguration="hz.behavior.rdy" />
        <endpoint address="https://services.debitcardprovider.com/hzwbsvc/services/TransactionHistoryServicePort" binding="customBinding" bindingConfiguration="TransactionHistoryServiceSoapBinding" contract="TransactionHistoryService.TransactionHistoryService" name="TransactionHistoryServicePort" behaviorConfiguration="hz.behavior.rdy" />
        <endpoint address="https://services.debitcardprovider.com/hzwbsvc/services/CustomerInfoUpdateServicePort" binding="customBinding" bindingConfiguration="CustomerInfoUpdateServiceSoapBinding" contract="CustomerInfoUpdateService.CustomerInfoUpdateService" name="CustomerInfoUpdateServicePort" behaviorConfiguration="hz.behavior.rdy" />
    </client>
</system.serviceModel>

【问题讨论】:

【参考方案1】:

如果我对您的理解正确,那么您的问题就是。 NET Framework 4 转换为。 NET Core 3.1 要更改哪个配置。

下面的链接可能会回答你的问题。

https://www.outsystems.com/blog/posts/migrating-from-net-framework-to-net-core/

谢谢。

【讨论】:

以上是关于在 .Net Core 3.1 中使用 WCF 服务的主要内容,如果未能解决你的问题,请参考以下文章

如何在 .NET Core 3.1 应用程序中添加 WCF 服务引用?

如何从 .NET Core 3.1 中的 F# 访问 WCF 服务?

忽略 ASP.NET Core 3.1 中的 WCF 证书错误

尝试使用 .Net Core 3.1 连接 mySAP WCF 时出现问题

连接的服务 WCF 不起作用 NET core 3.1

使用 .NET Core 3.1 生成 WCF wsdl 客户端的问题