user7243
user7243

Reputation: 41

Change ASP.NET generated WSDL for an ASP.NET web-service

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?

Upvotes: 3

Views: 5943

Answers (3)

John Saunders
John Saunders

Reputation: 161773

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.

Upvotes: 1

Ramzay
Ramzay

Reputation:

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

Upvotes: 2

Panos
Panos

Reputation: 19142

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

Upvotes: 7

Related Questions