Asmx Webservice Method to receive an soap xml input string - c#

Good day.
i am hoping that someone could assist me. I am creating a web service (this is the first time i have ever created a web service). i have worked with web services/API in the past from a client side by creating soap xml messages and sent and received messages back and forth. Now i need to create my own web Service and am needing a little assistance with creating it correctly.
i initially created a simple web method as follows.
[WebMethod(Description = "Soap Request")]
public string Request(string xmlDocument)
{
//do work
}
but when i tried consuming this method i received the following error:
System.Web.HttpRequestValidationException: A potentially dangerous
Request.Form value was detected from the client
i then tried (after googling) to change the method as follows:
[WebMethod(Description = "Soap Request")]
public string Request(XmlDocument xmlDocument)
{
//do work
}
but that didnt seem to work either, when i tried to send an xml soap message to this method it came through empty. What i then did was to create the method with parameters matching the elements in my xml Soap message. the method then picked up each element and populated the parameters correctly as follows.
public void Request(string parm1, string parm2)
{
//do work
}
with the following xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:Request>
<tem:Parm1>111</tem:Parm1>
<tem:Parm2>222</tem:Parm2>
</tem:Request>
</soapenv:Body>
</soapenv:Envelope>
i would like to find out, is this the correct way to handle the incoming message or is there something else i should be doing to accept the incoming xml message? i thought that i could create the method as i had initially done that it would accept the xml string passed to it and i could work with the message as i did when receiving xml message back from web services i used in the past.
please forgive me if i have duplicated a question, i could not find anything that would give me a definite answer.
i have started creating the web service in Visual Studio 2019.
any assistance or advise would be greatly appreciated.

I was having the exact same issue. So in my project I just added this to my web.config/system.web/httpRuntime:
requestValidationMode="2.0"
And It just worked!

Related

I have a WSDL, how do I create (BUT NOT SEND) the SOAP for a request?

My parent company has a WSDL, and I can create a ServiceModel, and use it to invoke web methods, and that's fine. However, what I need is to generate the SOAP XML without sending it. So, if I do this:
var mySvc = new MyWsdl.MyWebService();
var response = mySvc.DoTheThing(myUser, myData, myOtherData);
I know that under the covers, .NET is creating XML and sending it across the wire. But I need that XML without sending it, so, ideally, some code like:
var mySvc = new MyWsdl.MyWebService();
var xml = mySvc.GetSOAP("DoTheThing", myUser, myData, myOtherData);
Is there a way to do this? I am trying to get out of manually generating the XML.
The reason I'm doing this is because I need to actually send the SOAP to a different, normal non-SOAP web service. I suspect that that service is just going to turn around and call the SOAP service anyway, possibly massage the results, and return something to me, but that's out of my hands. Very soon I will not be able to use the ServiceClient directly.
Thanks!
EDIT: I would like to do this programatically. Otherwise, yes, I can get the XML by capturing a sent request, or using an app like SoapUI, but then I'm still manually creating the XML. The idea is, that if the WSDL changes, I don't want to have to change the XML I generate, so if the service client can do it for me, that would be ideal.

SSIS Web Service Task XmlNode Input POSSIBLE?

I've scoured the internet for the past two weeks and tried so many different ways that I'm beginning to think this might not be possible via a web service task in SSIS. I've created a web service task in SSIS by using a provided WSDL - see screenshot below.
As you can see the requirement for this web service method is to pass in an XML node which contains filter items like so:
<FilterItems><FilterItem FilterItemId="12345">4/20/2015 12:00:00 AM</FilterItem></FilterItems>
This "FilterItems" node is to be nested in between another XML element named "TemplateValues" - see soap body:
<soap:Body>
<GetReportResults xmlns="https://service.service.com">
<username>string</username>
<password>string</password>
<reportId>int</reportId>
<templateValues>xml</templateValues>
</GetReportResults>
The problem I'm facing is that when I go to chose a data type in the variable list in SSIS, there is no XML data type so I have to use a string. The funny part is that I can use SOAPUI and call the same method with no issues just by passing in:
<FilterItems><FilterItem FilterItemId="12345">4/20/2015 12:00:00 AM</FilterItem></FilterItems>
Here is the SOAPUI call that I am able to use:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="https://service.servicename.com">
<soapenv:Header/>
<soapenv:Body>
<ser:GetReportResults>
<!--Optional:-->
<ser:username>username</ser:username>
<!--Optional:-->
<ser:password>12343</ser:password>
<ser:reportId>000</ser:reportId>
<!--Optional:-->
<ser:templateValues>
<FilterItems><FilterItem FilterItemId="12345">4/20/2015 12:00:00 AM</FilterItem></FilterItems>
</ser:templateValues>
</ser:GetReportResults>
</soapenv:Body>
</soapenv:Envelope>
As you can see, I've just pasted the xml in between the element and SOAPUI processes the request with no problem - so why can't SSIS web service task process this same request?
My thoughts are that since SSIS is forcing me to use a string variable to store the xnlNode that certain characters are being parsed. Here are the results of the variable when I debug and run the web service task:
Value <FilterItems><FilterItem FilterItemId=\"12345\">4/20/2015 12:00:00 AM</FilterItem></FilterItems>
The debugging results show that the quotes have been escaped and I believe this is part of the problem. Here are some other things I've tried:
Passing in entirely encoded XML(also tried using CDATA):
<FilterItems><FilterItem FilterItemId="12345">4/20/2015 12:00:00 AM</FilterItem></FilterItems>
I also tried creating a SQL task and when executed would grab the xml and add it to a variable. Once again at runtime, escape characters were added in for the quotes causing the method not to execute. I'm finding it hard to believe that this is not a common issue and there isn't an easy workaround to passing in XML into a web service task. Is this a fault in SSIS or just something that hasn't been implemented? Help would be much appreciated!
It looks like the only answer is to change the web service to accept only simple types as parameters. I have scoured the web and there seems to be no way to dynamically create complex types for consumption by the input values in the web service task.
See also: Create complex-type variables for a Web Service Task

Response from ASMX

We have an old bit of code, ASMX WebService, that we have lost the source to. I am trying to replicate the behavior of this so we can take control of it once again without affecting any of the clients.
I have created a class that mimics the behavior and properties of the response, and a soap request returns as follows
<soap:Body>
<LoginResponse>
<LoginResult>
<UserId>string</UserId>
<Password>string</Password>
</LoginResult>
</LoginResponse>
Now the service I am trying to replicate returns
<soap:Body>
<LoginRS xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<UserId>string</UserId>
<Password>Password</Password>
</LoginRS>
</soap:Body>
Now LoginRS is the name of the class I have made. My question is how do I make my response look like the second response.
I am replicating this in c# ASMX and am using the following
[WebMethod(MessageName="Login")]
public LoginRS Login(string password, string userId)
Thanks for you time.
If your objective is to reverse engineer the service, and if the service dll is with you, you can use .NET Reflector (google it) to read the dll and provide you the exact source code.

.net Client consuming Axis2 Web Service

I have an .net 2.0 C# client app that has a web service reference to an Axis2 Java Webservice.
The idea is to send some xml data to the webservice, so it can be parsed and inserted into database.
The WS method accepts a single parameter of type 'xsd:anytype'.
Java web service:
public class JWS{
public Response AddData(Object inputXML) {
return Response;
}
}
C# Client:
JWS client = new JWS();
object inputXML = "<xml>some xml data</xml>";
response = client.AddData(inputXML);
There are 2 issues i am seeing when monitored using fiddler.
1) The request has an additional element '<inputXML>' added before the actual xml data.
<inputXML><xml>some xml data</xml></inputXML>
2) The xml is encoded, so '<' is appearing as "<"
I am not sure if this is how SOAP request's are generated but i would like to remove the <inputXML> tag and also, have the xml appear as is without having to replace the special characters.
Is this possible? Is it got something to do with 'Wrapping'/'UnWrapping' Types?
Also, i have used SoapUI to test the java web service and it works well. However, in the request tab, i had to manually remove the <inputXML> tag and submit for it to work correctly. Please help.
TIA
This is expected behaviour under SOAP and the inputXml variable will be decoded back to the original string when passed to your web service method.
However this may indicate a problem with your design, have you considered constructing an object to send to your web service rather than xml data? (As this object will transparently be converted to xml for the web service call anyway).
I found out that the issue is not with encoding but it was interpreted incorrectly on java side when the message was viewed in axis2. So, it is getting decoded properly. Also, the inputxml is now being handled correctly.

Server did not recognize the value of HTTP Header SOAPAction

[SoapRpcMethod(Action = "http://cyberindigo/TempWebService/InsertXML",
RequestNamespace = "http://cyberindigo/TempWebService/Request",
RequestElementName = "InsertXMLRequest",
ResponseNamespace = "http://cyberindigo/TempWebService/Response",
ResponseElementName = "InsertXMLResponse",
Use = System.Web.Services.Description.SoapBindingUse.Literal)]
[WebMethod]
public string InsertXML(string Jobs)
{
return "Hi";
}
The Problem when I am accessing it using XMLHttpRequest it gives following error
Server did not recognize the value of HTTP Header SOAPAction: http://Cyberindigo/TempWebService/InsertXML
The source of the next part of this post is:
http://bluebones.net/2003/07/server-did-not-recognize-http-header-soapaction/
(since the OP didn't want to give attribution, and thanks to Peter)
Please note that bakert is the original author of the text, not the OP.
Seeing as nowhere on the internet can I find an explanation of this error I thought I’d share the fruits of my long search for this bug.
It means (at least in my case) that you are accessing a web service with SOAP and passing a SOAPAction parameter in the HTTP request that does not match what the service is expecting.
I got in a pickle because we moved a web service from one server to another and thus I changed the “namespace” (don’t get confused between web service namespaces and .net namespaces) in the calling C# file to match the new server. But the server doesn’t care about the actual web reality of http://yournamespace.com/blah it only cares that you send it what you have said you are expecting on the server. It doesn’t care if there’s actually anything there or not.
So basically the web service was moved from http://foo.com/servicename to http://bar.com/servicename but the “namespace” of the web service stayed as http://foo.com/servicename because no one changed it.
And that only took about 4 hours to work out!
If you’re having a similar problem but can’t work what I’m saying here, feel free to mail me on bakert+web#gmail.com – I wouldn’t wish my four hours on anyone!
While calling the .asmx / wcf web service please take care of below points:
The namespace is case sensitive,the SOAP request MUST be sent with the same namespace with which the WebService is declared.
e.g. For the WebService declared as below
[WebService(Namespace = "http://MyDomain.com/TestService")]
public class FooClass : System.Web.Services.WebService
{
[WebMethod]
public bool Foo( string name)
{
......
}
}
The SOAP request must maintain the same case for namespace while calling.Sometime we overlook the case sensitivity.
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Foo xmlns="http://MyDomain.com/TestService">
<name>string</name>
</Foo>
</soap:Body>
</soap:Envelope>
The namespace need not be same as hosted url of the service.The namespace can be any string.
e.g. Above service may be hosted at http://84.23.9.65/MyTestService , but still while invoking the Web Service from client the namespace should be the same which the serice class is having i.e.http://MyDomain.com/TestService
I agree with Sam in that the SOAP definition does not match what is expected. Here is just ONE solution it could be, I had to manually figure this error for myself:
My problem was that I changed the name of the web method but did not change the "MessageName" in the metadata tag.
[WebMethod(MessageName = "foo")]
public string bar()
{
}
It should be
[WebMethod(MessageName = "foo")]
public string foo()
{
}
hope that helps someone
I've decided to post my own answer here because I've lost a few hours on this and I think that, although the accepted answer is very good and pointed me in the right direction (yes, it got a voteup), it was not detailed enough to explain what was wrong with my application, at least in my case.
I'm running a BPEL module in OpenESB 2.2 and the Test Case of my Composite Application was failing with the following error:
Caused by: System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: .
After doing some research I've noticed that the external WSDL has all the clues we need to fix this problem, e.g., I'm using the following web service to validate a credit card number through a orchestration of Web Services:
http://www.webservicex.net/CreditCard.asmx?WSDL
If you check the <wsdl:operation elements you will see that it clearly states the soapAction for that operation:
<wsdl:binding name="CCCheckerSoap" type="tns:CCCheckerSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ValidateCardNumber">
<soap:operation soapAction="http://www.webservicex.net/ValidateCardNumber" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
...
But, once you create the Composite Application and build the project with the BPEL that invokes this external WSDL service, for some reason (bug?), the XML of the Composite Application Service Assembly (CASA) binding is generated with an empty soapAction parameter:
<binding name="casaBinding1" type="ns:CCCheckerSoap">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="ValidateCardNumber">
<soap:operation soapAction="" style="document"/>
<input>
<soap:body use="literal"/>
</input>
Once you copy the proper soapAction (http://www.webservicex.net/ValidateCardNumber) into this parameter, the application's Test Case will correctly and return the expected Soap response.
<soap:operation soapAction="http://www.webservicex.net/ValidateCardNumber" style="document"/>
So, it's a more specific solution that I decided to document based on the information found in this blog post: http://bluebones.net/2003/07/server-did-not-recognize-http-header-soapaction/.
It means (at least in my case) that you are accessing a web service
with SOAP and passing a SOAPAction parameter in the HTTP request
that does not match what the service is expecting.
I had same problem, it fixed after some checking:
<< Target WebService Exists but called method's not eXXXists. >>
my local service contain methods but target server(connecting server)
does not contain specified called method.
Check your program scenario again...
Just to help someone on this problem, after an afternoon of debug, the problem was that the web service was developed with framework 4.5 and the call from android must be done with SoapEnvelope.VER12 and not with SoapEnvelope.VER11
I had similar issue. To debug the problem, I've run Wireshark and capture request generated by my code. Then I used XML Spy trial to create a SOAP request (assuming you have WSDL) and compared those two.
This should give you a hint what goes wrong.
My error fixed by answer Mr. John Saunders : http://forums.asp.net/post/2906487.aspx
in short: difference between Namespace of ws
.asmx.cs with ws
.wsdl files.
1) [WebService(Namespace = "http://tempuri.org/")]
later web service namespace changed to :
2) [WebService(Namespace = "http://newvalue.com/")]
so we referenced (1) in application and web service is (2) now.
make them equal to fix your problem.
I had this same problem, but the solution for me was that I was pointing to the wrong web service. I had updated the web reference correctly. But we store the URl for the service in an encrypted file, and I didn't update the file with the correct service encrypted.
But all these suggestions really helped me to realize where to go for debugging.
Thanks!
I had the same problem after changing the namespace from "tempuri" in my Web Service.
You have to update your Service Reference in the project that is consuming the above service, so it can get the latest SOAP definitions.
Or at least that worked for me. :)
We had renamed some of our webservice project namespaces and forgot to update the website httphandlers config section with the namespace of the renamed projects.
I had the same error, i was able to resolve it by removing the 'Web Reference' and adding a 'Service Reference' instead
I got this error when I tried to call a method which did not exist. It only existed in a newer version of our webservice.
I had to sort out capitalisation of my service reference, delete the references and re add them to fix this. I am not sure if any of these steps are superstitious, but the problem went away.
I had a similar problem with the same error message:
System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction:
We utilize dynamic URL's in our web service calls. We have a central configuration server that manages the location of all web service calls so that our code can run in DEV, test or live without recompiling. The URL for a specific web service call for the test environment was incorrect in our configuration server. The web service call was being sent to the wrong server and the wrong web service.
So this error can simply be the result of the web service request not matching the web service being called.
It took running fiddler on the Web App server to see that the actual call was to the incorrect web service.
the problem is in the System.Web.Services.Protocols.SoapDocumentMethodAttribute
in the service. Please check it. it may changed.
I found out that my web reference was out of date and the code was calling a removed web service.

Categories