wcf - 具有字符串返回值(或参数)的 ASMX WebService 强制在 WCF 客户端中生成消息协定

标签 wcf asmx messagecontract

我有一个简单的 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/

相关文章:

asp.net-mvc - 在 .NET 中实现基于 REST 的 Web 服务的最佳方法是什么?

web-services - 无法识别意外以 '/MyWebService' 结尾的 URL 的请求格式

c# - 如何列出使用 .asmx Web 服务将项目添加到 Sharepoint 列表

c# - 为什么我收到 NaN is not a valid value of Int32 错误?

wcf - 如何从 Soap 请求中删除 <可选>

WCF:MessageContract 处理 OnDeserialized 事件

c# - WCF 服务无法通过 MailDefinition 发送邮件

.net - 在 IIS 中托管的 WCF 服务中使用非托管线程不安全库

wcf - Windows 8 Azure 模拟器正在将端口 80 重新映射到 81

c# - WCF 流文件问题