【问题标题】:WCF RESTful service - How do I make my service not need the xmlns attribute in a request?WCF RESTful 服务 - 如何使我的服务在请求中不需要 xmlns 属性?
【发布时间】:2016-10-04 19:22:55
【问题描述】:

我正在构建一个 RESTful 服务,该服务在请求中包含 xmlns 属性时工作。但是,我需要让服务能够在没有 xmlns 属性的情况下接受请求。

这就是我现在的工作:

<ITEM_SEND xmlns="http://schemas.datacontract.org/2004/07/WCFInventoryService">
  <TRAN_ID>9483564</TRAN_ID>
  <VENDOR_PART>D336</VENDOR_PART>
</ITEM_SEND>

这是我需要接受的请求:

<ITEM_SEND>
  <TRAN_ID>9483564</TRAN_ID>
  <VENDOR_PART>D336</VENDOR_PART>
</ITEM_SEND>

这是我的界面:

namespace WCFInventoryService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract(Namespace = "")]
    public interface IInvService
    {
        [OperationContract]
        //[WebGet(UriTemplate="/Employees",ResponseFormat=WebMessageFormat.Xml )]
        //Employee[] GetEmployees();
        [WebInvoke(Method = "POST",
            RequestFormat = WebMessageFormat.Xml,
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
             UriTemplate = "")]
        ITEM_REPLY GetInventory(ITEM_SEND query);
    }

 public class ITEM_SEND
    {
        public string TRAN_ID { get; set; }
        public string VENDOR_PART { get; set; }
    }
}

我已尝试通过将数据协定设置为“”来更改我的请求的数据协定的命名空间。

[DataContract(Namespace = "")]
    public class ITEM_SEND
    {
        public string TRAN_ID { get; set; }
        public string VENDOR_PART { get; set; }
    } 

但这不起作用,因为当我在浏览器中查看我的 svc 时,我的请求从

【问题讨论】:

  • 如果您仍然可以选择我会切换到 JSON。没有命名空间。

标签: c# xml rest wcf


【解决方案1】:

你可以明确定义你的数据合约不属于任何命名空间-

[DataContract(Namespace = "")]
public class ITEM_SEND
{
    [DataMember]
    public string TRAN_ID { get; set; }
    [DataMember]
    public string VENDOR_PART { get; set; }
}

希望对你有帮助

【讨论】:

  • 哎呀!我实际上将此视为一种解决方案,但它对我不起作用,因为我省略了 DataMember 注释。我一开始以为这行不通,但在盯着你写的东西时,我突然看到你添加了 [DataMember] 注释。长话短说...它的工作原理!谢谢
  • 是的...如果您使用 [DataContract] 装饰类,那么您必须使用 [DataMember] 显式标记您希望公开的成员。只有这些带注释的成员被序列化为请求/响应 xml
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 2013-01-15
  • 2012-02-13
  • 1970-01-01
相关资源
最近更新 更多