我有一个简单的 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 客户端。未选中“始终生成消息契约(Contract)”复选框。 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 中将字符串参数和返回类型标记为可空?或者有没有办法让我的 WCF 客户端停止生成消息契约,即使存在不可为空的参数类型(知道这将删除参数可选性)?
最佳答案
我找到了解决这个问题的方法。看起来这是使用默认选择的 DataContractSerializer 的结果。 Visual Studio 提供的 GUI 中没有对此进行修复。要手动配置此功能,请打开服务客户端上的 Reference.svcmap 文件并更改 <Serializer>Auto</Serializer>
至<Serializer>XmlSerializer</Serializer>
。
这导致VS停止生成消息合约。
关于wcf - 具有字符串返回值(或参数)的 ASMX WebService 强制在 WCF 客户端中生成消息协定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788271/