Consume xml data via C# Web Service - c#

I will need to be able to receive this xml data from a Java WebService and I am not really sure what to expose in my WebMethod so I can consume it? It is just a basic order and items. In .Net I would just have passed an order object List.
I should expand a bit further. It is an Oracle BPEL process that will need to map to this exposed C# WebService. I would need to expose the OrderNumber, ItemNumber ,etc (as shown in XML). The issue I am having is that I would have 1 to Many items ,etc so I can't just expose the basic items (string, int).
Probably pretty trivial for most the community here...just not sure how to do it? Any suggestions greatly appreciated.
I could do something like (build an order object and it appears to show the xml as I would expect?)
[WebMethod]
public static List<Orders> GetOrders(List<Orders> ordersList)
{
List<Orders oList = ordersList;
return oList;
}
XML:
<Order>
<OrderNumber>12345</OrderNumber>
<OrderDate>01/25/2010</OrderDate>
<OrderSource>Affiliate123</OrderSource>
<Items>
<ItemNumber>123478</ItemNumber>
<Qty>5</Qty>
<UOM>EA</UOM>
<Description>Test Item</Description>
</Items>
</Order>

You have answered your own question. The .NET web services framework will map a return type of List<T> to a sequence of T at the SOAP level, just as if you had used a T[] (array of T).
When I write up a quick sample service just like yours, this is the XML it returns:
<?xml version="1.0" encoding="utf-8"?>
<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>
<GetOrdersResponse xmlns="http://tempuri.org/">
<GetOrdersResult>
<Order>
<OrderNumber>int</OrderNumber>
<OrderDate>dateTime</OrderDate>
<OrderSource>string</OrderSource>
<Items>
<Item xsi:nil="true" />
<Item xsi:nil="true" />
</Items>
</Order>
<Order>
<OrderNumber>int</OrderNumber>
<OrderDate>dateTime</OrderDate>
<OrderSource>string</OrderSource>
<Items>
<Item xsi:nil="true" />
<Item xsi:nil="true" />
</Items>
</Order>
</GetOrdersResult>
</GetOrdersResponse>
</soap:Body>
</soap:Envelope>
Your BPEL layer should be able to consume that pretty easily.

You don't need to expose a WebMethod, as I am assuming you are not publishing a WebService, instead you are consuming someone else's WebService.
I am not sure if I understand your question correctly, but if I do then in Visual Studio you just have to "Add Webreference" to the WebService URL and it should automatically create the .NET proxy objects, you can then use these objects to consume the methods.
Cheers,
Mithun
http://blog.mithunbose.com

Related

WCF request without namespace alias

I have a WCF service that works with a test client generated with SvcUtil.exe.
But my real client is another company that used different technology.
With the generated test client the XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<PostModel xmlns="http://tempuri.org/">
<model xmlns:a="http://schemas.datacontract.org/2004/07/SomeName.Models" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Items>
<a:Item>
<a:SomeElement1>SomeValue</a:SomeElement1>
<a:SomeElement2>SomeValue</a:SomeElement2>
</a:Item>
<a:Item>
<a:SomeElement1>SomeValue</a:SomeElement1>
<a:SomeElement2>SomeValue</a:SomeElement2>
</a:Item>
</a:Items>
<a:MoreItems>
<a:MoreItem>
<a:SomeElement3>binary</a:SomeElement3>
</a:MoreItem>
</a:MoreItems>
</model>
</PostModel>
</s:Body>
</s:Envelope>
But the real client sends the xml without alias:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<PostModel xmlns="http://tempuri.org/">
<model xmlns="http://schemas.datacontract.org/2004/07/SomeName.Models">
<Items>
<Item>
<SomeElement1>SomeValue</SomeElement1>
<SomeElement2>SomeValue</SomeElement2>
</Item>
<Item>
<SomeElement1>SomeValue</SomeElement1>
<SomeElement2>SomeValue</SomeElement2>
</Item>
</Items>
<MoreItems>
<MoreItem>
<SomeElement3>binary</SomeElement3>
</MoreItem>
</MoreItems>
</model>
</PostModel>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The result is that the model that I receive in the endpoint is NULL.
If I catch the request in fiddler and edit it, add nothing but the alias for the model namespace and add it to the child-elements it works.
Is there a solution for this problem, as far as I know the XML that the client sends is valid XML, aliases are not mandatory.
The difference between the test client XML and the real client XML lies in the namespace of the element model. From the test client:
<PostModel xmlns="http://tempuri.org/">
<model xmlns:a="http://schemas.datacontract.org/2004/07/SomeName.Models" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
This defines an element with local name "model". It has no namespace prefix so exists in the default namespace. However, it does not define a default namespace for itself -- instead it defines namespaces with prefixes "a" and "i". Thus it belongs in whatever default namespace its parent hierarchy defines, in this case "http://tempuri.org/" from its immediate parent.
In the real client's XML, the model element again has no prefix but does have its own default namespace declaration:
<PostModel xmlns="http://tempuri.org/">
<model xmlns="http://schemas.datacontract.org/2004/07/SomeName.Models">
Thus model falls into the namespace "http://schemas.datacontract.org/2004/07/SomeName.Models" rather than the parent's default namespace.
All the child elements of model are in the same namespace in both XML versions. It's only the model element itself which is inconsistent.
This inconsistency would explain the null model in the endpoint.

Custom fault type for an ASP.NET webservice

I am developing a webservice in C#, ASP.NET. By default, when my webservice throws exception, using
throw new SoapException("ERROR_MESSAGE", SoapException.ClientFaultCode);
This outputs the following xml:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>ERROR_MESSAGE</faultstring>
<detail/>
</soap:Fault>
</soap:Body>
</soap:Envelope>
However, I have to implement a custom fault type that uses different xml:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body xmlns:cst="http://cust.om/Namespace">
<cst:Fault>
<cst:errorMessage>ERROR_MESSAGE</cst:errorMessage>
</cst:Fault>
</soap:Body>
</soap:Envelope>
They expect that this fault type appears also in the wsdl file that is generated by http-GETting ?wsdl, surfacing there as element.
Is this even possible? Well, everything is possible by rewriting the exiting stream with replaced string, but I'd prefer less brutal approach. Besides, that kind of fix would not cause the elements to appear in the auto-generated wsdl spec.

Removing additional namespace definition in WCF SOAP message

I've got this WSDL(the service is not mine): http://soaptest.webapi-beta.gratka.pl/dom.html?wsdl
When I use WCF generated proxy the method tag in request soap message gets additional namespace definition like:
<q1:zaloguj xmlns:q1="http://soaptest.webapi-beta.gratka.pl/dom.html">
When I use PHP or proxy generated by wsdl.exe this doesn't happen.
I would like to ask, why does WCF do so, and is there possibility to change this behaviour (without using hand-made message modification in BeforeSendRequest)
Below I paste messages generated by PHP and WCF:
PHP one:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://soaptest.webapi-beta.gratka.pl/dom.html" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:zaloguj>
<login xsi:type="xsd:string">login</login>
<haslo xsi:type="xsd:string">password</haslo>
<klucz_webapi xsi:type="xsd:string">key</klucz_webapi>
<id_kategoria xsi:type="xsd:int">382a</id_kategoria>
<wersja_webapi xsi:type="xsd:int">2</wersja_webapi>
</ns1:zaloguj>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
WCF one:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<q1:zaloguj xmlns:q1="http://soaptest.webapi-beta.gratka.pl/dom.html">
<login xsi:type="xsd:string">login</login>
<haslo xsi:type="xsd:string">password</haslo>
<klucz_webapi xsi:type="xsd:string">key</klucz_webapi>
<id_kategoria xsi:type="xsd:int">382</id_kategoria>
<wersja_webapi xsi:type="xsd:int">2</wersja_webapi>
</q1:zaloguj>
</s:Body>
</s:Envelope>
Are you facing any issues? As far as xml is concerned, both are equivalent. PHP code is declaring the namespace (xmlns:ns1="http://soaptest.webapi-beta.gratka.pl/dom.html") at the root element while WCF is declaring at the point where it is needed - I would believe that this is what WSDL's implementation would be - does not seem to be anything wrong in it.

WCF XML deserialization fail

I've got this WSDL(the service is not mine): http://soaptest.webapi-beta.gratka.pl/dom.html?wsdl
When I get response from method "pobierz_kategorie" I get exception like that
CommunicationException
Error in deserializing body of reply message for operation 'pobierz_kategorie'.
{"There is an error in XML document (2, 501)."} {"The specified type was not recognized: name='kategoria_drzewo', namespace='http://soap.webapi-beta.gratka.pl/dom.html', at ."}
I would like to ask how to modify WSDL or WCF configuration or generated proxy to make it working. When I use PHP to call that method it works so I guess that must be some XMLSerializer problem.
This the response message I get:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://soap.webapi-beta.gratka.pl/dom.html" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:pobierz_kategorieResponse>
<drzewo_kategorii SOAP-ENC:arrayType="ns1:kategoria_drzewo[48]" xsi:type="ns1:drzewo_kategorii">
<item xsi:type="ns1:kategoria_drzewo">
<id xsi:type="xsd:int">382</id>
<nazwa xsi:type="xsd:string">Dom</nazwa>
<dane xsi:type="xsd:string">0</dane>
<id_rodzic xsi:type="xsd:int">1</id_rodzic>
<poziom xsi:type="xsd:int">0</poziom>
<id_prasa xsi:type="xsd:int">2</id_prasa>
</item>
<item xsi:type="ns1:kategoria_drzewo">
<id xsi:type="xsd:int">8251</id>
<nazwa xsi:type="xsd:string">Pokoje</nazwa>
<dane xsi:type="xsd:string"/>
<id_rodzic xsi:type="xsd:int">382</id_rodzic>
<poziom xsi:type="xsd:int">1</poziom>
<id_prasa xsi:type="xsd:int">0</id_prasa>
</item>
</drzewo_kategorii>
</ns1:pobierz_kategorieResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
That is the request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q1="http://soaptest.webapi-beta.gratka.pl/dom.html">
<s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<q1:pobierz_kategorie>
<sesja xsi:type="xsd:string">ad9e730460e334b916c95533c8da320003ac7e14</sesja>
<id_kategoria xsi:type="xsd:int">382</id_kategoria>
</q1:pobierz_kategorie>
</s:Body>
</s:Envelope>
I've retrieved that WSDL and the response message name does not match what you have above.
Where you have pobierz_kategorieResponse in the wsdl, it is pobierz_kategorie_wyjscie in the WSDL definition.
Message...
<message name="pobierz_kategorie_wyjscie">
<part name="drzewo_kategorii" type="tns:drzewo_kategorii"/>
</message>
Operation...
<operation name="pobierz_kategorie">
<input message="tns:pobierz_kategorie_wejscie"/>
<output message="tns:pobierz_kategorie_wyjscie"/>
</operation>
Maybe you need to regenerate your service reference or point it at a different endpoint.
EDIT
You may have to customize your reference.cs file (your generated WCF client code) to handle the soap array. I haven't done this myself but there is an SO article about it:-
Why won't .NET deserialize my primitive array from a web service?
I'm not 100% sure if this is the same issue as what you are having. It looks like you need to tell WCF explicitly - for this element, this is how you de-serialize the array. Sorry I can't be of more help.
EDIT #2
There is a mismatch between the wsdl published to you 'soaptest' and the actual implementation. Note the namespaces. Or, you've generated your code of a test wsdl endpoint and pointed it at a prod endpoint.See the namespaces below.Your request's namespaces looked like this:-
http://soaptest.webapi-beta.gratka.pl/dom.html
But the response's namespaces looked like this:-
http://soap.webapi-beta.gratka.pl/dom.html
That ain't gonna work!
Try generating your WCF client off
http://soap.webapi-beta.gratka.pl/dom.html?wsdl
instead of off
http://soaptest.webapi-beta.gratka.pl/dom.html?wsdl

Error Consuming a RPC/Encoded SOAP web service in .NET

I am receiving the following error when calling a web service method.
Cannot assign object of type System.Xml.XmlNode[] to an object of type System.String.
The web service is a PHP service. I created my proxy class using wsdl.exe from the wsdl document defined here - http://webservice.intelecast.com.au/traffic/PublicSoap/server.php?wsdl
Below is the proxy client method I am calling.
[System.Web.Services.Protocols.SoapRpcMethodAttribute("http://webservice.intelecast.com.au/traffic/PublicSoap/server.php#getAllTraffic", RequestNamespace="http://webservice.intelecast.com.au/traffic/PublicSoap/server.php", ResponseNamespace="http://webservice.intelecast.com.au/traffic/PublicSoap/server.php")]
[return: System.Xml.Serialization.SoapElementAttribute("return")]
public TmcInfo[] getAllTmcTraffic(string[] States, int[] EventCodes) {
object[] results = this.Invoke("getAllTmcTraffic", new object[] { //Error here
States,
EventCodes});
It appear the proxy class is failing to deal with the complex types/arrays defined in the wsdl...I am unsure now how to proceed can't find a solution on the web anywhere.
Below is the SOAP response i am receiving. This all appears valid.
<?xml version="1.0" encoding="iso-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="http://webservice.intelecast.com.au/traffic/PublicSoap/server.php">
<SOAP-ENV:Body>
<ns1:getAllTrafficResponse
xmlns:ns1="http://webservice.intelecast.com.au/traffic/PublicSoap/server.php">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:TrafficInfo[2]">
<item xsi:type="tns:TrafficInfo">
<id xsi:type="xsd:string">1245632068110</id>
<entryDate xsi:type="xsd:datetime">22/06/2009 10:54:28</entryDate>
<direction xsi:type="xsd:int">0</direction>
<extent xsi:type="xsd:int">1</extent>
<locationCode xsi:type="xsd:int">31593</locationCode>
<countryCode xsi:type="xsd:int">4</countryCode>
<ltn xsi:type="xsd:int">56</ltn>
<diversionAdvice xsi:type="xsd:int">0</diversionAdvice>
<slAdvice xsi:type="xsd:int">0</slAdvice>
<quantifier xsi:type="xsd:int">0</quantifier>
<suppInfoCode xsi:type="xsd:int">0</suppInfoCode>
<addEvent xsi:type="xsd:int">0</addEvent>
<detDivAdvice xsi:type="xsd:int">0</detDivAdvice>
<destinations xsi:type="xsd:int">0</destinations>
<clSourceProblem xsi:type="xsd:int">0</clSourceProblem>
<eventCode xsi:type="xsd:int">802</eventCode>
<timeInfo xsi:type="tns:TimeInfo">
<start xsi:type="xsd:string">21/06/2009 14:00:00</start>
<finish xsi:type="xsd:string">05/11/2009 14:00:00</finish>
<timeZone xsi:type="xsd:string">Australia/Brisbane</timeZone>
</timeInfo>
<location xsi:type="tns:Point">
<lat xsi:type="xsd:float">-27.3112692120521</lat>
<lon xsi:type="xsd:float">153.029100894928</lon>
</location>
<additionalStreetInfo
xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:AdditionalStreetInfo[1]">
<item xsi:type="tns:AdditionalStreetInfo">
<street xsi:type="xsd:string">Bracken Ridge Rd</street>
<type xsi:type="xsd:string">CROSS</type>
</item>
</additionalStreetInfo>
<address xsi:type="tns:Address">
<streetNumber xsi:type="xsd:string"/>
<streetName xsi:type="xsd:string">Bracken Ridge Rd</streetName>
<suburb xsi:type="xsd:string">Bracken Ridge</suburb>
<state xsi:type="xsd:string">QLD</state>
</address>
</item>
<item xsi:type="tns:TrafficInfo">
<id xsi:type="xsd:string">1245632111995</id>
<entryDate xsi:type="xsd:datetime">22/06/2009 10:55:11</entryDate>
<direction xsi:type="xsd:int">1</direction>
<extent xsi:type="xsd:int">1</extent>
<locationCode xsi:type="xsd:int">31592</locationCode>
<countryCode xsi:type="xsd:int">4</countryCode>
<ltn xsi:type="xsd:int">56</ltn>
<diversionAdvice xsi:type="xsd:int">0</diversionAdvice>
<slAdvice xsi:type="xsd:int">0</slAdvice>
<quantifier xsi:type="xsd:int">0</quantifier>
<suppInfoCode xsi:type="xsd:int">0</suppInfoCode>
<addEvent xsi:type="xsd:int">0</addEvent>
<detDivAdvice xsi:type="xsd:int">0</detDivAdvice>
<destinations xsi:type="xsd:int">0</destinations>
<clSourceProblem xsi:type="xsd:int">0</clSourceProblem>
<eventCode xsi:type="xsd:int">802</eventCode>
<timeInfo xsi:type="tns:TimeInfo">
<start xsi:type="xsd:string">21/06/2009 14:00:00</start>
<finish xsi:type="xsd:string">05/11/2009 14:00:00</finish>
<timeZone xsi:type="xsd:string">Australia/Brisbane</timeZone>
</timeInfo>
<location xsi:type="tns:Point">
<lat xsi:type="xsd:float">-27.3125370752656</lat>
<lon xsi:type="xsd:float">153.042898178101</lon>
</location>
<additionalStreetInfo
xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:AdditionalStreetInfo[1]">
<item xsi:type="tns:AdditionalStreetInfo">
<street xsi:type="xsd:string">Bracken Ridge Rd</street>
<type xsi:type="xsd:string">CROSS</type>
</item>
</additionalStreetInfo>
<address xsi:type="tns:Address">
<streetNumber xsi:type="xsd:string"/>
<streetName xsi:type="xsd:string">Bracken Ridge Rd</streetName>
<suburb xsi:type="xsd:string">Bracken Ridge</suburb>
<state xsi:type="xsd:string">QLD</state>
</address>
</item>
</return>
</ns1:getAllTrafficResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Is it a bug in wsdl.exe? I have a few unpleasant encounters with wsdl.exe. Here are the problems outlined in my post:
The first issue it fails to handle
sequence with complex element. You
would think that in today's webservice
environment, a sequence of complex
elements is a norm. But apparently
Microsoft doesn't think so, and this
results in inability of wsdl.exe to
handle a sequence of complex elements.
What if you insist on using wsdl.exe
to generate proxy class for element
with a sequence of complex types? You
will get a cryptic exception: unable
to import binding * from namespace *
- unable to import operation *
- the datatype * is missing The second bug wsdl has is that it is not
permissible to have two web methods
that have the same return signature.
Yes, if one method returns a type of
ComplexType, the other methods must
have a different return type. Failing
to do so will result in an
System.InvalidOperationException: The
XML element * from namespace *
references a references a method and a
type exception.
Note that in both cases the messages
are cryptic. You won't actually know
what's going on behind the hood. And
this is the most frustrating of all.
You don't know whether you fail
because the tool is lousy, or because
you are not doing things right.
Maybe you can refer to this post for an alternative of wsdl.exe.
It's worth noting that rpc/encoded SOAP implementation have problems on complex types. One way around this problem is to convert the WSDL to Document/Literal format.

Categories

Resources