3

Is there a way to change the way asp.net generates elements in the WSDL generated from a .asmx file? Specifically, it seems to mark all elements minoccurs="0" and there are some elements that I want to be minoccurs="1" (aka required fields).

One of these is an argument to the web service (e.g. foo(arg1, arg2) where I want arg2 to be generated in the WSDL as minoccurs="1") the other is a particular field in the class that corresponds to arg1. Do I have to forego auto WSDL generation and take a "contract first" approach?

3 Answers 3

7

I think that the XmlElement(IsNullable = true) attribute will do the job:

using System.Xml.Serialization;

[WebMethod]
public string MyService([XmlElement(IsNullable = true)] string arg)
{
  return "1";
}

EDIT [VB version]

Imports System.Xml.Serialization

Public Function MyService(<XmlElement(IsNullable:=True)> ByVal arg As String) As String
  Return ("1")
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

do you know how you'd do that in VB.NET? I can't seem to find the equivalent expression...
@user7243, you should really mark this as the answer... It helps the community, not just Panos...
2

Using XMLElement(IsNullable=true) generates minOccurs=1, but it also generates in WSDL nillable="true", which is undesirable.

Comments

1

The only way I know of (short of upgrading to WCF) is to use the [XmlSchemaProvider] attribute. This permits you tio indicate a method that will return the schema that will be emitted as part of the WSDL.

By the time you get to this point, you may find it better to simply write your own WSDL, by hand, so you don't have to worry about how to coerce .NET into writing it for you. You would then simply place the WSDL in a known location on a web site (possible the same site as the service), then tell your customers to use http://url/service.wsdl instead of service.asmx?wsdl.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.