Calling a SOAP service in .net Core
Ok this answer is for those who are trying to connect to a WCF service from a .net Core project.
Here is the solution to my problem, using the new .net Core WCF syntax/library.
BasicHttpBinding basicHttpBinding = null;
EndpointAddress endpointAddress = null;
ChannelFactory<IAService> factory = null;
IAService serviceProxy = null;
try
{
basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
endpointAddress = new EndpointAddress(new Uri("https://someurl.com/ws/TheEndpoint.pub.ws:AService"));
factory = new ChannelFactory<IAService>(basicHttpBinding, endpointAddress);
factory.Credentials.UserName.UserName = "usrn";
factory.Credentials.UserName.Password = "passw";
serviceProxy = factory.CreateChannel();
using (var scope = new OperationContextScope((IContextChannel)serviceProxy))
{
var result = await serviceProxy.getSomethingAsync("id").ConfigureAwait(false);
}
factory.Close();
((ICommunicationObject)serviceProxy).Close();
}
catch (MessageSecurityException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
finally
{
// *** ENSURE CLEANUP (this code is at the WCF GitHub page *** \\
CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
UPDATE
I got the following exception using the code above
This OperationContextScope is being disposed out of order.
Which seems to be something that is broken (or needs addressing) by the WCF team.
So I had to do the following to make it work (based on this GitHub issue)
basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
factory = new ChannelFactory<IAService_PortType>(basicHttpBinding, new EndpointAddress(new Uri("https://someurl.com/ws/TheEndpoint.pub.ws:AService")));
factory.Credentials.UserName.UserName = "usern";
factory.Credentials.UserName.Password = "passw";
serviceProxy = factory.CreateChannel();
((ICommunicationObject)serviceProxy).Open();
var opContext = new OperationContext((IClientChannel)serviceProxy);
var prevOpContext = OperationContext.Current; // Optional if there's no way this might already be set
OperationContext.Current = opContext;
try
{
var result = await serviceProxy.getSomethingAsync("id").ConfigureAwait(false);
// cleanup
factory.Close();
((ICommunicationObject)serviceProxy).Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
OperationContext.Current = prevOpContext; // Or set to null if you didn't capture the previous context
}
But your requirements will probably be different. So here are the resources you might need to help you connecting to your WCF service are here:
- WCF .net core at GitHub
- BasicHttpBinding Tests
- ClientCredentialType Tests
The tests helped me a lot but they where somewhat hard to find (I had help, thank you Zhenlan for answering my wcf github issue)
Is it possible to create SOAP service with .net core 5.0?
Is it possible to create SOAP service with .net core 5.0?
You can create an ASP.NET Web Application(.NET Framework) to create a SOAP Webservice, then create an asp.net core 5.0 application and call the SOAP service.
Besides, you can also try to use SoapCore package in the asp.net core 5.0 application, then create the SOAP service. You can refer the SoapCore Getting Started or search "create soap web service in dotnet core" using Google, there have multiple tutorials about using SoapCore, you can check them.
Call Soap 1.2 service from .net core
The server and the client must use the same binding to communicate. Your server uses wsHttpBinding but the client uses BasicHttpsBinding, so they cannot communicate normally.
You are right. Core does not currently support wsHttpBinding, so there are two solutions:
1:Change the wsHttpBinding of the server to BasicHttpBinding, and the core does not support the Message security mode. For the WCF features in the Core, you can refer to the link below:
https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md
2:The client uses the .net framework instead of core.
I suggest you use the second solution. After all, core's support for wcf is not good.
Feel free to let me know if the problem persists.
Related Topics
Windows 7 and Vista Uac - Programmatically Requesting Elevation in C#
Sqlbulkcopy Insert with Identity Column
Attach Debugger in C# to Another Process
I Didn't Find "Zipfile" Class in the "System.Io.Compression" Namespace
Inconsistency in Divide-By-Zero Behavior Between Different Value Types
No Templates in Visual Studio 2017
SQL Where Clause Matching Values with Trailing Spaces
Open Default Mail Client Along with a Attachment
What Is the "Right" Way to Bring a Windows Forms Application to the Foreground
Use Reflection to Invoke an Overridden Base Method
What's the Best Strategy for Equals and Gethashcode
How to Save Image in Database Using C#