控制 WCF 响应格式和命名空间

编程入门 行业动态 更新时间:2024-10-19 16:35:44
本文介绍了控制 WCF 响应格式和命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我希望我的 WCF 响应具有一个包含两个命名空间的响应元素,使用 DataContracts,但我无法让它工作.这就是我希望得到的回应:

I want my WCF response to have a response element with two namespaces using DataContracts, but I can't get it to work. This is what I would like the response to be:

<s:Envelope xmlns:s="http://schemas.xmlsoap/soap/envelope/" xmlns:xs="http://www.w3/2001/XMLSchema">
  <s:Header />
  <s:Body>
    <ns2:TestReply xmlns="http://www.test/test/2007/00" xmlns:ns2="http://www.test2/test2/types">
      <ns2:Result>
        <ns2:ActionSuccessful>true</ns2:ActionSuccessful>
      </ns2:Result>
      <ns2:ResultData>
        <ns2:Name>Maikel Willemse</ns2:Name>
      </ns2:ResultData>
    </ns2:TestReply>
  </s:Body>
</s:Envelope>

这是我得到的响应(使用 WCF 测试客户端进行测试时):

This is the response I get (when testing with the WCF Test Client):

<s:Envelope xmlns:s="http://schemas.xmlsoap/soap/envelope/">
  <s:Header />
  <s:Body>
    <GetDataResponse xmlns="http://www.test/test/2007/00">
      <TestReply xmlns:a="http://www.test2/test2/types" xmlns:i="http://www.w3/2001/XMLSchema-instance">
        <a:Result>
          <a:ActionSuccessful>true</a:ActionSuccessful>
        </a:Result>
        <a:ResultData>
          <a:Name>Maikel Willemse</a:Name>
        </a:ResultData>
      </TestReply>
    </GetDataResponse>
  </s:Body>
</s:Envelope>

我的服务界面是这样的:

My service interface looks like this:

[ServiceContract(Namespace = "http://www.test/test/2007/00")]
public interface IService1
{
    [OperationContract]
    [return: MessageParameter(Name = "TestReply")]
    GetDataResponse GetData(string name);
}

服务类:

public class Service1 : IService1
{
    public GetDataResponse GetData(string name)
    {
        return new GetDataResponse
            {
                Result = new Result {ActionSuccessful = true},
                ResultData = new ResultData {Name = name}
            };
    }
}

DataContract 类是:

And the DataContract classes are:

[DataContract(Namespace = "http://www.test2/test2/types")]
public class GetDataResponse
{
    [DataMember(Name = "Result")]
    public Result Result { get; set; }

    [DataMember(Name = "ResultData")]
    public ResultData ResultData { get; set; }
}

[DataContract(Namespace = "http://www.test2/test2/types")]
public class Result
{
    [DataMember(Name = "ActionSuccessful")]
    public bool ActionSuccessful { get; set; }
}

[DataContract(Namespace = "http://www.test2/test2/types")]
public class ResultData
{
    [DataMember(Name = "Name")]
    public string Name { get; set; }
}

我的 WCF 项目的目标框架是 .NET 4.命名空间前缀不必相同.如何获得所需格式的响应?

The target framework of my WCF project is .NET 4. The namespace prefixes do not have to be the same. How can I get the response in the format I want?

推荐答案

致@MaikelTestReply 位于默认命名空间中,因此没有前缀,但里面的元素确实有.

To @Maikel The TestReply is in the default namespace so there is no prefix, but the elements inside do have it.

xmlns:a="http://www.test2/test2/types

所以这个命名空间的前缀是 a.(因为 'a=')并且它与默认命名空间不同.

so the prefix for this namespace is a. (because of 'a=') and it is different from the default namespace.

在你的方法的 ServiceContractAttribute 中

In the ServiceContractAttribute of your method

GetDataResponse GetData(string name); 

按照@Carlos 的建议,你可以写

as @Carlos suggested, you can write

[ServiceContract(Namespace="http://www.test2/test2/types")]

你不能拥有这个

<a:TestReply xmnls:a="http://www.test2/test2/types">

这篇关于控制 WCF 响应格式和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 20:12:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   空间   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!