7

I am trying to consume a web service specified using WSDL by creating a WCF proxy using svcutil.exe, but the WSDL specifies that some of the operations have parameters that are optional (minOccurs="0"), for example:

<xs:element minOccurs="0" maxOccurs="1" name="meetingId" type="xs:int" /> 

Unfortunately, the generated proxy does not allow me to not specify the values (the parameters are not nullable), and there are no "specified" fields as part of the call to instruct the proxy that no value should be sent.

Is there any way to use svcutil to generate a proxy that would allow me to do this?

(On a side note, I noticed through my research that others were able to generate these extra "specified" fields correctly using the "Add Service Reference" feature, but for whatever reason Visual Studio doesn't appear to want to generate the proxy after I add the reference (nothing happen afterwards))

WSDL File Generated Proxy

Command Used: svcutil http://sas-int.elluminate.com/site/external/adapter/default/v1/webservice.wsdl /internal /n:*,Elluminate.WebService.WebServiceProxy /o:WebServiceProxy.cs /config:App.config /nologo

David
  • 660
  • 7
  • 18
  • SvcUtil generally is a PITA and I don't use it, but my services and WCF so I have far easier task. – Krzysztof Kozmic Jun 18 '09 at 14:16
  • It is quite strange that the Specified properties are not generated... This is WCF ways of handling optionnal value types. No errors provided by svcutil? If not, can you provide us with the WSDL file and what is generated? – Philippe Jun 18 '09 at 15:17
  • Sure, the WSDL is available at: http://sas-int.elluminate.com/site/external/adapter/default/v1/webservice.wsdl I'm using the following command to generate the proxy: svcutil http://sas-int.elluminate.com/site/external/adapter/default/v1/webservice.wsdl /internal /n:*,Elluminate.WebService.WebServiceProxy /o:WebServiceProxy.cs /config:..\App.config /nologo And here is what is generated: http://pastebin.com/m20688d39 Thanks! – David Jun 18 '09 at 17:09

3 Answers3

8

I would guess the client proxy class generated by svcutil has a field/property called meetingId of type int - right? Yes, this is non-nullable - but I bet you also have a boolean field/property called meetingIdSpecified - only if this is set to true will the service actually look at it; if you don't set it, then the service will not look at the value, so it's almost as if it where NULL.

Also - you didn't specify the field to be nullable in your XSD, either - you specified it to be optional. To make it nullable, use this syntax here:

<xs:element minOccurs="0" maxOccurs="1" name="meetingId" type="xs:int" 
            nillable="true" />

See the "nullable" attribute? That's the one used for making a field really nullable - you can now have an entry like this in your XML:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <meetingId xsi:nil="true" />
</root>

Marc

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Those specified fields aren't showing up, but you are right about the nillable... if I add that to the WSDL then svcutil does properly generate nullable parameters for the calls. Thanks – David Jun 19 '09 at 17:33
  • I have a case in which the element is optional (minOccurs="0") and intentionally not nillable. I also don't get the "specified" fields... unless I include the "/serializer:XmlSerializer" command-line option. I guess the default DataContractSerializer doesn't handle this situation correctly...? The only way to get the DataContractSerializer code to work is if you manually modify the generated code to specify EmitDefaultValue="false" in the DataMemberAttribute, but then you'd never be able to send zero, because that'd be interpreted as an absent value by the server. – ALEXintlsos Jun 18 '12 at 14:37
1

Do not use SVCUtil if it does not work for you. If the service is not very complicated, you can try writing the service interface manually, and use ChannelFactory<> to create your proxies.

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
1

It is a problem because minOccurs="0" should allow you not to emit the 'meetingId' value, and the generated proxy does not allow it.

You only consume the service, so you don't have control over the wsdl and you cannot add nillable="true" in the wsdl in order to have 'meetingId' optional in your proxy.

If you generate your proxy with wsdl.exe, and not svcutil.exe, you will have the additional field 'meetingIdSpecified' that allows you to choose whether or not to emit the field meetingId.

wsdl.exe http:///myservice?wsdl

But with wsdl.exe you will consume your service with asp.net web service, and not WCF.

I think the missing field is a bug in svcutil.exe (for me 4.0.30319.17929), because if you generate with the option /wrapped:

svcutil.exe /wrapped http:///myservice?wsdl

..then you will also have the field 'meetingIdSpecified' generated !

Anthony Brenelière
  • 60,646
  • 14
  • 46
  • 58