如何在soap格式WCF中设置属性
如何设置soap消息的属性: 例如,我的soap消息如下所示如何在soap格式WCF中设置属性,wcf,soap,xml-attribute,Wcf,Soap,Xml Attribute,如何设置soap消息的属性: 例如,我的soap消息如下所示 <doPaymentResult xmlns:a="http://schemas.datacontract.org/2004/07/MemoService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <a:errors i:nil="true"/> <a:messages> <
<doPaymentResult xmlns:a="http://schemas.datacontract.org/2004/07/MemoService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:errors i:nil="true"/>
<a:messages>
<a:MessageEntity>
<a:codeField>Payment Request Successful</a:codeField>
<a:textField i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
</a:MessageEntity>
</a:messages>
<a:requestTrackCode>20130430T125904R14646</a:requestTrackCode>
<a:status i:nil="true"/>
</doPaymentResult>
</doPaymentResponse>
<doPaymentResult xmlns:a="http://schemas.datacontract.org/2004/07/MemoService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:errors i:nil="true"/>
<a:messages>
<a:MessageEntity codeField="Payment Request Successful">
some text here
</a:MessageEntity>
</a:messages>
<a:requestTrackCode>20130430T125904R14646</a:requestTrackCode>
<a:status i:nil="true"/>
</doPaymentResult>
</doPaymentResponse>
付款请求成功
20130430T125904R14646
但是我需要一个soap消息,它采用属性而不是元素
像下面
<doPaymentResult xmlns:a="http://schemas.datacontract.org/2004/07/MemoService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:errors i:nil="true"/>
<a:messages>
<a:MessageEntity>
<a:codeField>Payment Request Successful</a:codeField>
<a:textField i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
</a:MessageEntity>
</a:messages>
<a:requestTrackCode>20130430T125904R14646</a:requestTrackCode>
<a:status i:nil="true"/>
</doPaymentResult>
</doPaymentResponse>
<doPaymentResult xmlns:a="http://schemas.datacontract.org/2004/07/MemoService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:errors i:nil="true"/>
<a:messages>
<a:MessageEntity codeField="Payment Request Successful">
some text here
</a:MessageEntity>
</a:messages>
<a:requestTrackCode>20130430T125904R14646</a:requestTrackCode>
<a:status i:nil="true"/>
</doPaymentResult>
</doPaymentResponse>
这里有一些文字
20130430T125904R14646
我在课堂上使用datacontract。这是一个常见的问题–您希望以XML形式从WCF服务返回一个对象,但您希望或需要以XML属性而不是XML元素的形式传递部分或全部属性值;但您不能,因为DataContractSerializer不支持属性(如果您进行了web搜索,则很可能看到此StackOverflow QA)。很可能您已经将所有WCF服务代码迁移到使用XmlSerializer(以及所有XmlElement/XmlAttribute/XmlType属性等)的环境中,并且您已经大声诅咒了 好吧,我来这里是为了拯救你,因为这是可能的——而问题的答案实际上是从MSDN题为“数据契约序列化程序支持的类型”的文章中推断出来的 我将给出的示例仅用于说明目的。我没有很多时间,所以和我一起工作吧! •创建一个新的Asp.Net WCF服务应用程序,您可以使用Cassini作为您的web服务器(可能更容易–否则您可能必须启用Asp.Net兼容模式)。 •打开web.config并删除为新服务创建的元素。 •本例的接口和实现模型过于简单。将[ServiceContract]和[OperationContract]声明从为新服务创建的接口移动到也已创建的类。删除接口。 •打开.svc标记文件并在末尾添加以下内容:Factory=“System.ServiceModel.Activation.WebServiceHostFactory”-这将启用此服务的零配置WCF模型(我们将创建一个RESTful服务)。 •将以下类声明粘贴到svc codebehind中:
public interface IExampleData
{
string Description { get; set; }
string Name { get; set; }
int ID { get; set; }
}
public class ExampleData : IExampleData
{
public string Description { get; set; }
public string Name { get; set; }
public int ID { get; set; }
}
public class ExampleDataAttributed : ExampleData, IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
//implement if remote callers are going to pass your object in
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("id", ID.ToString());
writer.WriteAttributeString("name", Name);
//we'll keep the description as an element as it could be long.
writer.WriteElementString("description", Description);
}
#endregion
}
Just to demonstrate the point, the class that will be part-serialized to attributes simply derives from one that will be serialized as normal.
•Now add the following two methods to your service class:
[OperationContract]
[WebGet(UriTemplate = "/test1")]
public ExampleData Test1()
{
return new ExampleData() { ID = 1,
Name = "Element-centric",
Description =
"The contents of this item are entirely serialized to elements - as normal" };
}
[OperationContract]
[WebGet(UriTemplate = "/test2")]
public ExampleDataAttributed Test2()
{
return new ExampleData_Attributed() { ID = 2,
Name = "Mixed",
Description =
"Everything except this description will be serialized to attributes" };
}
盖上盖子,烘烤40分钟(也就是说,制作它)
如果您将服务保留为Service1.svc,请运行它并打开IE并浏览到http://localhost:[卡西尼港]/test1
结果应该如下所示:
<JSLabs.ExampleData
xmlns="http://schemas.datacontract.org/2004/07/ExampleNamespace"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Description>
The contents of this item are entirely serialized to elements - as normal
</Description>
<ID>
1
</ID>
<Name>
Element-centric
</Name>
</JSLabs.ExampleData>
此项的内容完全序列化为元素-正常情况下
1.
以元素为中心
现在浏览到http://localhost:[卡西尼港]/test2
<JSLabs.ExampleDataAttributed id="2" name="Mixed"
xmlns="http://schemas.datacontract.org/2004/07/JobServe.Labs.Web">
<description>Everything except this description will be
serialized to attributes</description>
</JSLabs.ExampleDataAttributed>
除此描述之外的所有内容都将被删除
序列化为属性
WCF数据协定序列化程序自动将讨厌的“orrible”xmlns=“属性放在类型上,这让人印象不那么深刻——但是,正如您所看到的,“ID”和“Name”属性确实被作为属性推出了
我们可以让这两个方法都返回IExampleData,然后在该接口上使用KnownType属性以使其支持其中一个(根据方法的代码返回的内容)
要支持从属性反序列化对象,只需实现IXmlSerializable.ReadXml方法
最后,正如前面提到的MSDN文章所说的受支持的类型——您还应该能够使用XmlElement/XmlNode类型作为直接表示XML的方式——DataContractSerializer(如本例中所示)走捷径,只获取XML
如果您为XML或JSON客户机双重输出对象,这也不会影响JSON格式
这是一个常见问题–您希望以XML形式从WCF服务返回一个对象,但您希望或需要以XML属性而不是XML元素的形式传递部分或全部属性值;但您不能,因为DataContractSerializer不支持属性(如果您进行了web搜索,则很可能看到此StackOverflow QA)。很可能您已经将所有WCF服务代码迁移到使用XmlSerializer(以及所有XmlElement/XmlAttribute/XmlType属性等)的环境中,并且您已经大声诅咒了 好吧,我来这里是为了拯救你,因为这是可能的——而问题的答案实际上是从MSDN题为“数据契约序列化程序支持的类型”的文章中推断出来的 我将给出的示例仅用于说明目的。我没有很多时间,所以和我一起工作吧! •创建一个新的Asp.Net WCF服务应用程序,您可以使用Cassini作为您的web服务器(可能更容易–否则您可能必须启用Asp.Net兼容模式)。 •打开web.config并删除为新服务创建的元素。 •本例的接口和实现模型过于简单。将[ServiceContract]和[OperationContract]声明从为新服务创建的接口移动到也已创建的类。删除接口。 •打开.svc标记文件并在末尾添加以下内容:Factory=“System.ServiceModel.Activation.WebServiceHostFactory”-这将启用此服务的零配置WCF模型(我们将创建一个RESTful服务)。 •将以下类声明粘贴到svc codebehind中:
public interface IExampleData
{
string Description { get; set; }
string Name { get; set; }
int ID { get; set; }
}
public class ExampleData : IExampleData
{
public string Description { get; set; }
public string Name { get; set; }
public int ID { get; set; }
}
public class ExampleDataAttributed : ExampleData, IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
//implement if remote callers are going to pass your object in
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("id", ID.ToString());
writer.WriteAttributeString("name", Name);
//we'll keep the description as an element as it could be long.
writer.WriteElementString("description", Description);
}
#endregion
}
Just to demonstrate the point, the class that will be part-serialized to attributes simply derives from one that will be serialized as normal.
•Now add the following two methods to your service class:
[OperationContract]
[WebGet(UriTemplate = "/test1")]
public ExampleData Test1()
{
return new ExampleData() { ID = 1,
Name = "Element-centric",
Description =
"The contents of this item are entirely serialized to elements - as normal" };
}
[OperationContract]
[WebGet(UriTemplate = "/test2")]
public ExampleDataAttributed Test2()
{
return new ExampleData_Attributed() { ID = 2,
Name = "Mixed",
Description =
"Everything except this description will be serialized to attributes" };
}
盖上盖子,烘烤40分钟(也就是说,制作它)
如果您将服务保留为Service1.svc,请运行它并打开IE并浏览到http://localhost:[卡西尼港]/test1
结果应该如下所示:
<JSLabs.ExampleData
xmlns="http://schemas.datacontract.org/2004/07/ExampleNamespace"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Description>
The contents of this item are entirely serialized to elements - as normal
</Description>
<ID>
1
</ID>
<Name>
Element-centric
</Name>
</JSLabs.ExampleData>
此项的内容完全序列化为元素-正常情况下
1.
以元素为中心
现在眉头