C#asynchronous invocation with wsa replyTo - c#

I am trying to make an asynchronous call to a web service using BeginXXX and EndXXX methods from C# client. I am using a proxy class generated by using SvcUtil.exe.
The soap message that I expect to send should contain soap header elements for ws-addressing which include tags:
'wsa:Action'
'wsa:MessageID'
'wsa:ReplyTo'
'wsa:To'
However the soap header section is currently empty and only soap body contains body information.
Without the wsa tags, it looks like a synchronous call.
Is there a way to use proxy generated in C#, to invoke a service asynchronously such that the soap message contains wsa tags?
Appreciate any input with regards to this

Related

Read SOAP XML message sent as HTTP message body

I have a 3rd party that is sending SOAP XML messages, as the HTTP message body (using HTTP Post). I need to write a program to accept / process these messages.
I have a specification document which contains 2 WSDL definitions - GetTransaction and CutOff.
I am trying to use Postman to send a test message. Content-Type is set to application/xml and the body is set to raw / XML.
I am using C#, ASP.Net (4.7.2), the code is in a Class Library. I have a Controller (ApiController) with:
[HttpPost]
[ValidationAttributes.RequireHttps]
public HttpResponseMessage Service(XmlDocument reqData)
but reqData is always null. I have tried different types instead of XmlDocument.
Now if probably a good time to say that this is all new to me - I've not created a program to accept SOAP messages before.
Should I be creating a WCF Service Application (rather than a .Net Framework Class Library)?
I've tried adding the WSDL "definitions" but they don't seem to produce anything useful. E.g. there is reference to a "GetTransaction" method, but it has a parameter list of about 150 items!?
I have found quite a lot of stuff via Google but it always seems to be about sending, rather than receiving, SOAP messages.
I appreciate that I have not really included much code to look at, but I feel like I've started from the wrong place. So, any basic guidance, suggestions or links to tutorial sites would be most welcome.

Implementing a SOAP web service

I have a WSDL definition for a SOAP service and I have successfully generated *.cs file from it using SvcUtil.
Implementing client is quite straightforward - I just need to call the necessary functions from the generated *.cs and that's it.
Implementing server seems more complicated. As I understand I need to implement an interface from the generated *.cs and then use some magic to turn it into the web server.
But I don't need a new web server, I already have a web server written in C# which already has many functionality unrelated to the SOAP service that I need to implement. I don't want to create another web server but I want my SOAP service to be just a part of my existing application (server), that is my server can answer e.g. requests http://example.com/request1, http://example.com/request2 etc. and I want this SOAP service to be just http://example.com/request3.
Since HTTP is already handled by my server I don't need .NET to handle it for me, basically my server can accept client connections and call the necessary handler based on the URL. I have a handler for SOAP request which looks approximately like this:
MyResponse HandleSOAPRequest(MyRequest request)
{
// 1. parse soap message from request.body
// 2. process it
// 3. generate response, serialize it in SOAP format and return it
}
The question is - can I rely on WSDL definition and .NET libraries to do it?
Currently I'm parsing SOAP request using XDocument and manually extract fields from it and serialize using simple string concatenation. Using .NET built-in functions to serialize or parse XML doesn't work. That is if I try to serialize response from an object of the class defined in the generated *.cs file then produced XML is different from what is expected by the protocol, similarly, if I try to parse request as an object of the class defined in the generated *.cs file I get error because XML parser expects different format. This applies to both the SoapFormatter and XmlSerializer.
Since .NET can implement client this means that everything that is necessary to parse and serialize SOAP messages is already implemented, I just need to figure out a way how to use this functionality.
The documentation for ServiceModel wasn't very helpful.
The easiest way would be to start the service via the ServiceHost:
ServiceHost host = new ServiceHost(typeof(YourService));
host.Open();
(I assumed here the configuration will come from the app.config and will not be setup in code like in the linked example below.)
How to: Expose a Contract to SOAP and Web Clients
The only drawback of this is that the application has to run with admin rights or otherwise a weird cofiguration is necessary.

Adding a soap message to a Service Reference in c#

I have added a Service Reference that points to WSDL of the service. I can consume the service by creating a proxy and theres no issue with that. My problem is that I need to send a specific soap message , and theres some parameters that need to be include into the header and the body of the soap message (credentials and IDs).
How I can manually edit or add this soap message? without touching the app.config file?, there's a way to add the same parameters inside the code? such as:
soap:Header
soap:Body

Soap Header Action Not Understood

I have imported a WSDL via the Service Reference dialog box in VS2010 (asmx not WCF) which contains a SendMessage method which I need to call, passing it Xml and a Command name. I'm also passing the URL of the WS. When I call this method, it causes the following exception:
SoapHeaderException: Soap Header Action Not Understood
I think I am able to read the Soap request via Fiddler, and it contains no Soap Header information. I would have expected C# to add this automatically since all the required information to create it is present.
How do I ensure the Soap Header is added??

Modifying SOAP requests with GetWebRequest

I am trying to dynamically modify XML data in SOAP requests to ASMX services.
I overrided GetWebRequest() method in SoapHttpClientProtocol class in order to read and modify XML data that the RequestStream contains.
The problem is, the request seems to be empty, there is no data in it whatsoever. Is this because the SOAP data hasn't yet been generated and serialized or am I doing something wrong?
What you need is a SoapExtension. You could hook into the SoapMessageStage.AfterSerialize stage in ProcessMessage to modify your soap message. I've done this in the past to add WSSE headers in situations where I couldn't add a dependency on Microsoft's WSE library (since it isn't available for Mono).
Complete tutorial here: http://msdn.microsoft.com/en-us/magazine/cc164007.aspx
GetWebRequest is too early for your purpose, GetWebResponse is too late.

Categories

Resources