2

I have a simple ASMX WebService and it just looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;

namespace ContractGenerationTest
{
    [WebService(Namespace = Constants.WebServiceNamespace)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Standard : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hellow World";
        }
    }
}

I also have a WCF Client that is associated with the service. The "Always generate message contracts" checkbox is unchecked. The WCF Client is generating message contract classes even though I do not want it to.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://sampleuri.org/ContractGenerationTest/", ConfigurationName="StandardService.StandardSoap")]
public interface StandardSoap {

    // CODEGEN: Generating message contract since element name HelloWorldResult from namespace http://sampleuri.org/ContractGenerationTest/ is not marked nillable
    [System.ServiceModel.OperationContractAttribute(Action="http://sampleuri.org/ContractGenerationTest/HelloWorld", ReplyAction="*")]
    ContractGenerationTestClient.StandardService.HelloWorldResponse HelloWorld(ContractGenerationTestClient.StandardService.HelloWorldRequest request);

    [System.ServiceModel.OperationContractAttribute(Action="http://sampleuri.org/ContractGenerationTest/HelloWorld", ReplyAction="*")]
    System.Threading.Tasks.Task<ContractGenerationTestClient.StandardService.HelloWorldResponse> HelloWorldAsync(ContractGenerationTestClient.StandardService.HelloWorldRequest request);
}

Note the CODEGEN comment. This seems to be constructed this way because the string type is not marked as nillable in the WSDL from the ASMX service. It is not marked as nillable (nor has maxOccurs=1) because string.Empty provides a default value.

If the strings were members of a complex type, I could just mark them as [XmlElement(IsNullable=true)] and that would solve the problem... but I can't do that here because they are part of the function definition.

Is there a known solution to this? Is there a way I can get my ASMX to mark the string parameters and return types as nillable in the WSDL? Or is there a way for my WCF Client to stop generating message contracts even if there are non-nillable parameter types (knowing this will remove parameter optionality)?

1 Answers1

2

I found a solution for this. Looks like it is a result of using the DataContractSerializer which is selected by default. There's no fix for this in the GUI provided in Visual Studio. To configure this manually, open the Reference.svcmap file on the client side of the service and change <Serializer>Auto</Serializer> to <Serializer>XmlSerializer</Serializer>. This caused the VS to stop generating message contracts.