Take the tripservice wsdl from this link In this wsdl, I replaced the from element with the below(added nillable as true and added min length and max length restriction).
<xs:element minOccurs="0" name="from" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="12"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Now in my vb.net client i invoked the service by adding service reference, wsdl saved to a local folder.
Dim objproxy As New Tripservice.TripPriceServiceFacadeClient
Dim gh As New Tripservice.trip
gh.adults = 9
gh.duration = 8
gh.rooms = 8
gh.to = "p"
objproxy.getTripPrice(gh)
It will throw end point not found exception, however i am interested in the request xml that is going. I enabled the trace and found that the below request is generated.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<getTripPrice xmlns="http://trip.price.service">
<trip xmlns="">
<adults>9</adults>
<duration>8</duration>
<from xsi:nil="true"/>
<rooms>8</rooms>
<to>p</to>
</trip>
</getTripPrice>
</s:Body>
</s:Envelope>
The element from xsi:nil="true" is generated, even though i am not touching the element in my vb.net code to generate the request. The element is optional as per the wsdl(min occurs = 0). How can i send a request without the from element name, even passed in the request?
You can't; it is interesting to find out why you changed it to nillable; in doing that, the way .NET code generation works, you leave it no way to know whether it should marshall the tag or not; typically, an optional string that is null is not marshalled. An optional (minOccurs=0) and nillable wouldn't work since there is no "set" indicator (JAXB has it or use to have it) to keep trace whether the user code set the value, null or not null.
Related
I am receiving a soap message from an external server that is sending xsi:nil tag for a boolean element.
JIBX complains about an invalid boolean value and its not able to parse this xml.
How can I overcome this problem?
incoming xml:
<materialChange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
the xsd:
<s:element minOccurs="0" maxOccurs="1" name="materialChange" type="s:boolean" nillable="true"/>
I’m having problems with specifying the right input parameters to the Betdaq’s sportsbetting API in Visual Basic, Visual Studio 2015. Here follows a part of the associated WSDL file available, which is loaded as a web reference:
<xs:complexType name="GetEventSubTreeNoSelectionsRequest">
<xs:sequence maxOccurs="unbounded">
<xs:element name="EventClassifierIds" type="xs:long"/>
</xs:sequence>
<xs:attribute name="WantDirectDescendentsOnly" type="xs:boolean"
use="optional"/>
<xs:attribute name="WantPlayMarkets" type="xs:boolean"
use="optional"/>
</xs:complexType>
This is my current code to implement the API request:
Private Sub ReadMarkets()
Dim GetEventSubTreeNoSelectionsRequest As New
BETDAQAPI.GetEventSubTreeNoSelectionsRequest
GetEventSubTreeNoSelectionsRequest.WantDirectDescendentsOnly = True
GetEventSubTreeNoSelectionsRequest.WantPlayMarkets = True
End Sub
I’m struggling with adding the "EventClassifierIds" specification correctly. I only intend to add one EventClassifierId = 10004, but
GetEventSubTreeNoSelectionsRequest.EventClassifierIds = 100004
wont work, since input is required to be in form of “Long()”. What's the workaround here? This is clearly some syntax I’m not getting, and every kind of help is much appreciated!
In my WCF I defined an object "Product" as below:
<xs:complexType name="Product">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Postcode" type="xs:string" nillable="true" minOccurs="0"/>
<xs:element name="ProductID" type="xs:string" />
</xs:sequence>
</xs:complexType>
Please note the element "Postcode" is nillable="true" minOccurs="0", this means when client application call the web service, they can either provide empty value for this element as below:
<Product>
<Name>Product 1</Name>
<Postcode></Postcode>
<ProductID>10</ProductID>
</Product>
Or they can simple skip "Postcode" element so the incoming message becomes this (Postcode isn't there)
<Product>
<Name>Product 1</Name>
<ProductID>10</ProductID>
</Product>
My question is, in my host program how do I check if the element "Postcode" exists in the incoming XML message? I am asking because if it doesn't there and I call the method to get Postcode value, then the program will throw an error:
string postcode = product.Postcode - this call will throw an error?
Thank you all very much for any ideas/suggestions.
Charles
You are not correct in your assertion that using nillable in your definition permits the two cases you have listed.
In fact, the nillable xsd attribute permits the construction:
<Postcode xsi:nil='true'/>
which is semantically different from Postcode being either empty, or just not present at all.
In answer to your question,
how do I check if the element "Postcode" exists in the incoming XML
message
I don't quite understand why you want to define Postcode with minOccurs=0. When wcf receives this type across the wire it will attempt to deserialise to a predefined C# type. In C#, types either have properties or they don't have.
eg.
public class Product
{
public string Postcode { get; set; }
}
is not the same things as
public class Product
{
}
So by allowing callers to construct two different versions of your Product type you are actually forced to define two separate C# types to deserialise to, which means you'll need to expose your service operation twice, once for callers who want to use Product with postcode, and one for those who want to use Product without.
I would suggest that you redefine your Product type to include the Postcode element - after all because it's a string your callers can just leave it null if they do not wish to use it.
I have a WCF service hosted on 2 servers. There was a function called GetData(param1).
I changed this function to accept 2 parameters i.e. GetData(param1,param2).
I updated the service on server1 and I updated the client code.
A weird thing is happening. the updated client code still works with the outdated service although the functions don't match. The function is being called and the results are returned. The added parameter is an enumeration value type if that helps. But why have such a non-deterministic behavior? and how does it work?
This thing is that when you design your method in a procedural way all you input parameters are optional by default and are populated with default values if you don't specify them explicitly. Assume you have a method with the following signature:
[OperationContract]
void TestMethod(string param1, int param2);
You will get the following WSDL for it:
<xs:element name="TestMethod">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="param1" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="param2" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
As you can see minOccurs attribute has 0 value which means the element is optional. So this is not surprising that your method works even after you added a new parameter.
If you want to avoid this behavior try to design your contracts in a message way by using MessageContract or at least wrapping all you parameters in a container class. And specify explicitly which parameter is required and if it allows default value via DataMember attribute.
Hope it helps!
If your service has installed more than once place. Recheck your endpoint address to ensure its pointing right hosted server.
I have a VB class in an .asmx file in Visual Studio 2008:
public class foo
public bla as String
end class
It generates the wsdl value:
<s:complexType name="foo">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="bla" type="s:string" />
</s:sequence>
</s:complexType>
But what I want the wsdl to generate is:
<xs:element name="bla" type="xs:string"
sql:datatype="varchar(25)" minOccurs="1" maxOccurs="1">
</xs:element>
Is there a way to do this?
Or can I edit the generated WSDL?
You can use The System.Xml.Serialization.XmlElementAttribute to mark the property IE:
<XmlElement(DataType := "varchar(25)")>
(my vb is a little rusty if this isn't correct
syntax)
You can save off the WSDL and edit it, however, if you change the WSDL, a proxy generated from it may not be able to communicate with your service.
Edit: If you have the target schema, I would suggest that you use xsd.exe or wsdl.exe to generate the classes you need to serialize to valid documents according to that schema.