.NET Core 中的 WSDL 端点配置

Zen*_*san 6 .net c# wsdl .net-core

我正在尝试使用 .NET 核心处理 WSDL。目前,我可以导入 WSDL 并生成相关文件:

.NET 核心连接服务生成的文件

结构以正确的方式完成,似乎一切都应该按预期进行。现在在 ASP.NET 中,我会有一个包含以下信息的 XML:

 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="SI_SearchItem_OutBinding" />
        <binding name="SI_SearchItem_OutBinding1">
            <security mode="Transport">
                <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="XISOAPApps" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
            </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="HTTP_ENDPOINT_LINK"
        binding="basicHttpBinding" bindingConfiguration="SI_SearchItem_OutBinding"
        contract="SI_Out.SI_SearchItem_Out" name="HTTP_Port" />
      <endpoint address="HTTPS_ENDPOINT_LINK"
        binding="basicHttpBinding" bindingConfiguration="SI_SearchItem_OutBinding1"
        contract="SI_Out.SI_SearchItem_Out" name="HTTPS_Port" />
    </client>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

由于我们不再有web.config核心,我正在尝试以编程方式添加这些配置。

在看的时候,Reference.cs我注意到两件事:

  1. ConfigureEndpoint 没有在生成的文件中实现,这让我觉得我需要在那里做我的配置。
  2. ConfigureEndpoint 定义如下: static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);

但是这个函数被调用

public SI_SearchItem_OutClient(EndpointConfiguration endpointConfiguration) : 
            base(SI_SearchItem_OutClient.GetBindingForEndpoint(endpointConfiguration), SI_SearchItem_OutClient.GetEndpointAddress(endpointConfiguration))
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
Run Code Online (Sandbox Code Playgroud)

这在构造函数参数中不采用客户端凭据,因此我无法在 configureEndpoint 函数中以编程方式实际复制上述 xml 代码。

我希望我能很好地解释自己,实际上我唯一想做的就是能够通过在 ConfigureEndpoint 部分函数中以编程方式复制上述 XML 代码来使用凭据对我的 SOAP 调用进行身份验证。

感谢您抽出时间回复,芝诺

小智 2

我想明白你的问题了。web.config 文件不再存在。因此,有必要在代码端解决端点操作。

端点必须定义如下。

            EndpointAddress endpoint = new EndpointAddress(new Uri("***Your WSDL addres like http://....../test_ws***"));
Run Code Online (Sandbox Code Playgroud)

绑定操作和配置应如下进行。将 MessageSize 和 BufferSize 值定义为最大值非常重要。

            BasicHttpBinding httpBinding = new BasicHttpBinding();
            httpBinding.MaxReceivedMessageSize = Int32.MaxValue;
            httpBinding.MaxBufferSize = Int32.MaxValue;
            httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
Run Code Online (Sandbox Code Playgroud)

绑定和端点操作完成后,客户端创建就完成了。您将使用 SI_SearchItem_OutClient

            var client = new Zbw_Gelir.zbw_gelir1_wsClient(httpBinding, endpoint);
            client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            client.ClientCredentials.Windows.ClientCredential.Domain = "";
            client.ClientCredentials.Windows.ClientCredential.UserName = "***USERNAME***";
            client.ClientCredentials.Windows.ClientCredential.Password = "***PASSWORD***";
            client.ClientCredentials.UserName.UserName = "***USERNAME***";
            client.ClientCredentials.UserName.Password = "***PASSWORD***";

            client.ChannelFactory.CreateChannel();

          
Run Code Online (Sandbox Code Playgroud)