【发布时间】:2014-11-06 19:56:03
【问题描述】:
我有一个简单的 ASMX WebService,它看起来像这样
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";
}
}
}
我还有一个与该服务关联的 WCF 客户端。 “始终生成消息合同”复选框未选中。尽管我不希望 WCF 客户端生成消息协定类。
[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);
}
注意CODEGEN 评论。这似乎是这样构造的,因为string 类型在来自 ASMX 服务的 WSDL 中未标记为可空。它没有被标记为 nillable(maxOccurs=1),因为string.Empty 提供了一个默认值。
如果字符串是复杂类型的成员,我可以将它们标记为[XmlElement(IsNullable=true)],这样就可以解决问题……但我不能在这里这样做,因为它们是函数定义的一部分。
对此有已知的解决方案吗?有没有办法让我的 ASMX 在 WSDL 中将字符串参数和返回类型标记为 nillable?或者有没有办法让我的 WCF 客户端停止生成消息协定,即使存在不可为空的参数类型(知道这将删除参数可选性)?
【问题讨论】:
标签: wcf asmx messagecontract