2012-01-22 111 views
0

我得到了這樣的XSD的服務響應:WCF - 定製響應消息

<xs:element name="AddEditResponse"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element minOccurs="1" maxOccurs="1" name="response" type="xs:boolean"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

注意,只有元素的響應消息中的名稱是「響應」。

[ServiceContract] 
[XmlSerializerFormat] 
public interface IService 
{ 
    [OperationContract] 
    [return:XmlElement("return")] 
    bool AddEdit(MultipleElements elements); 
} 

我申請的XmlElement屬性AddEdit操作的返回值,但我仍然得到以下XSD:

<xs:element name="AddEditResponse"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element minOccurs="1" maxOccurs="1" name="AddEditResult" type="xs:boolean"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

的AddEditResponse元素中的元素的名稱保持不變不管名稱在[return:XmlElement]屬性中。

這是怎麼發生的?有什麼方法可以自定義WCF中的數據交換格式的細節?

謝謝。

回答

1

我會通過讓svcutil爲我建立合同來解決這個問題。看看它是否有幫助...

使用您必須通過一個操作生成WSDL文件的模式(至少與您的AddEdit方法相匹配)。

一旦你有一個,運行一個類似的命令行SvcUtil工具(在我的情況,我現在用的工具生成三個WSDL文件中的一個引用XSD文件):

svcutil /mc AddEditSoapHttp.wsdl AddEditSoapBinding.wsdl AddEditInterface.wsdl WCF-WSDLFirst.xsd 

結果應該是像這樣:

Microsoft (R) Service Model Metadata Tool 
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1] 
Copyright (c) Microsoft Corporation. All rights reserved. 

Generating files... 
....\AddEditSoapHttpService.cs 
....\output.config 

看看生成的代碼來找到你的問題的答案(也許更多)。

對於此XSD(示出了請求/響應對作爲例子):

<?xml version="1.0" encoding="utf-8" ?> 
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="AddEditRequest"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element minOccurs="1" maxOccurs="1" name="request" type="xs:string"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="AddEditResponse"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element minOccurs="1" maxOccurs="1" name="response" type="xs:boolean"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

我正在(我張貼的摘錄只):

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] 
[System.ServiceModel.ServiceContractAttribute(Namespace="http://tempuri.org/ifx/addedit/interface/1/", ConfigurationName="AddEditPortType")] 
public interface AddEditPortType 
{ 

    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ifx/addedit/bindings/1/AddEdit", ReplyAction="*")] 
    [System.ServiceModel.XmlSerializerFormatAttribute()] 
    AddEditResponse1 AddEdit(AddEditRequest1 request); 
} 
+0

謝謝,這是我通緝。我是新來的WCF,我不知道svcutil。但是,它生成消息合同,而不是數據合同。 「如果用於模擬消息的命名約定與WCF標準不同,代碼生成工具將爲請求和響應生成MessageContract類」 - from [this](http://msdn.microsoft.com/en-us/雜誌/ ee335699.aspx)文章。 –