MessageParameterAttribute 類別

定義

控制要求的名稱並回應參數名稱。 不能與 Message 或訊息合約一起使用。

public ref class MessageParameterAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, Inherited=false)]
public sealed class MessageParameterAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, Inherited=false)>]
type MessageParameterAttribute = class
    inherit Attribute
Public NotInheritable Class MessageParameterAttribute
Inherits Attribute
繼承
MessageParameterAttribute
屬性

範例

下列程式碼範例會示範如何使用 MessageParameterAttribute 建立參數的名稱以及中繼資料內的傳回值。

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace="http://microsoft.wcf.documentation")]
  public interface ISampleService{
    [OperationContract]
    [return:MessageParameter(Name = "Output")]
    string SampleMethod([MessageParameter(Name = "Input")]string msg);
  }

  /*
   The preceding use of MessageParameterAttribute generates the following XSD:

<?xml version="1.0" encoding="utf-8" ?>
  <xs:schema
    elementFormDefault="qualified"
    targetNamespace="http://microsoft.wcf.documentation"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://microsoft.wcf.documentation">
    <xs:element name="SampleMethod">
      <xs:complexType>
        <xs:sequence>
          <xs:element minOccurs="0" name="Input" nillable="true" type="xs:string" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name="SampleMethodResponse">
      <xs:complexType>
        <xs:sequence>
          <xs:element minOccurs="0" name="Output" nillable="true" type="xs:string" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:schema>

   And the message sent is edited for clarity:
  <s:Body>
    <SampleMethod xmlns="http://microsoft.wcf.documentation">
      <Input>hello!</Input>
    </SampleMethod>
  </s:Body>
   */
Imports System.ServiceModel
Imports System.Text

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="http://microsoft.wcf.documentation")> _
  Public Interface ISampleService
    <OperationContract> _
    Function SampleMethod(<MessageParameter(Name := "Input")> ByVal msg As String) As <MessageParameter(Name := "Output")> String
  End Interface

'  
'   The preceding use of MessageParameterAttribute generates the following XSD:
'
'<?xml version="1.0" encoding="utf-8" ?> 
'  <xs:schema 
'    elementFormDefault="qualified" 
'    targetNamespace="http://microsoft.wcf.documentation" 
'    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
'    xmlns:tns="http://microsoft.wcf.documentation">
'    <xs:element name="SampleMethod">
'      <xs:complexType>
'        <xs:sequence>
'          <xs:element minOccurs="0" name="Input" nillable="true" type="xs:string" /> 
'        </xs:sequence>
'      </xs:complexType>
'    </xs:element>
'    <xs:element name="SampleMethodResponse">
'      <xs:complexType>
'        <xs:sequence>
'          <xs:element minOccurs="0" name="Output" nillable="true" type="xs:string" /> 
'        </xs:sequence>
'      </xs:complexType>
'    </xs:element>
'  </xs:schema>
'   
'   And the message sent is edited for clarity:
'  <s:Body>
'    <SampleMethod xmlns="http://microsoft.wcf.documentation">
'      <Input>hello!</Input>
'  </s:Body>   
'

下列程式碼範例會顯示針對先前服務產生的服務合約。 請注意,參數名稱目前為 Input

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://microsoft.wcf.documentation", ConfigurationName="ISampleService")]
public interface ISampleService
{

    [System.ServiceModel.OperationContractAttribute(
      Action="http://microsoft.wcf.documentation/ISampleService/SampleMethod",
      ReplyAction="http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
    )]
    [return: System.ServiceModel.MessageParameterAttribute(Name="Output")]
    string SampleMethod(string Input);
}
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0"), _
System.ServiceModel.ServiceContractAttribute(Namespace:="http://microsoft.wcf.documentation", _
                                             ConfigurationName:="ISampleService")> _
Public Interface ISampleService

    <System.ServiceModel.OperationContractAttribute(Action:="http://microsoft.wcf.documentation/ISampleService/SampleMethod", _
                    ReplyAction:="http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse")> _
    Function SampleMethod(ByVal Input As String) As <System.ServiceModel.MessageParameterAttribute(Name:="Output")> String
End Interface

備註

使用 MessageParameterAttribute 控制參數名稱或傳回值。 若參數由 MessageContractAttribute 屬性標記,則此屬性無作用。

注意

作業簽章中參數名稱的值是合約的一部分,而且必須區分大小寫。 當您需要區分本機參數名稱和描述用戶端應用程式的作業的中繼資料時,請使用此屬性。

For more information about controlling the serialization of the contents of a SOAP body without modifying the default SOAP envelope itself, see System.Runtime.Serialization.DataContractAttribute, Specifying Data Transfer in Service Contracts, and Using Data Contracts.

如需建立訊息合約的詳細資訊,請參閱 使用訊息合約

建構函式

MessageParameterAttribute()

初始化 MessageParameterAttribute 類別的新執行個體。

屬性

Name

取得或設定 XML 結構描述 (XSD) 中參數的名稱屬性。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於