'WSDL Endpoint configuration in .NET Core

I'm trying to work with WSDL using .NET core. Currently, I'm able to import the WSDL and the related files are generated:

.NET core Connected Services generated files

The structure is done in the right way and it seems that everything should work as expected. Now in ASP.NET I would have an XML having this information:

 <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>

Since we don't have web.config anymore in core, I'm trying to programmatically add these configurations.

While looking at the Reference.cs I noticed two things:

  1. ConfigureEndpoint is not implemented in the generated files, which made me thought that I needed to do my configurations there.
  2. ConfigureEndpoint is defined as follows: static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);

But this function is called by

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

Which does not takes the client credentials in the constructor parameters, so I'm not able to actually replicate the above xml code programmatically inside the configureEndpoint function.

I hope I explained myself well, practically the only thing I'm trying to do, is to be able to authenticate my SOAP calls with the credentials, by replicating the above XML code programmatically inside the ConfigureEndpoint partial function.

Thanks for taking your time to reply, Zeno



Solution 1:[1]

I think understood your problem. The web.config file no longer exists. Therefore, it is necessary to solve the endpoint operations code-side.

The endpoint must be defined as follows.

            EndpointAddress endpoint = new EndpointAddress(new Uri("***Your WSDL addres like http://....../test_ws***"));

Binding operations and configurations should be done as follows. It is important to define the MessageSize and BufferSize values as max.

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

After binding and endpoint operations, client creation is done. You will be use 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();

          

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Serdar Köse