【问题标题】:ASMX WebService with string return value (or parameter) forcing message contract generation in WCF Client带有字符串返回值(或参数)的 ASMX WebService 强制在 WCF 客户端中生成消息合同
【发布时间】: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


    【解决方案1】:

    我找到了解决方案。看起来这是使用默认选择的 DataContractSerializer 的结果。在 Visual Studio 中提供的 GUI 中没有解决此问题。要手动配置,请在服务客户端打开 Reference.svcmap 文件并将&lt;Serializer&gt;Auto&lt;/Serializer&gt; 更改为&lt;Serializer&gt;XmlSerializer&lt;/Serializer&gt;。 这导致 VS 停止生成消息合约。

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2012-03-19
      • 1970-01-01
      相关资源
      最近更新 更多