MessageContractAttribute Klasa

Definicja

Definiuje silnie typizowana klasę odpowiadającą komunikatowi SOAP.

public ref class MessageContractAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, AllowMultiple=false)]
public sealed class MessageContractAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, AllowMultiple=false)>]
type MessageContractAttribute = class
    inherit Attribute
Public NotInheritable Class MessageContractAttribute
Inherits Attribute
Dziedziczenie
MessageContractAttribute
Atrybuty

Przykłady

W poniższym przykładzie kodu pokazano użycie elementu MessageContractAttribute do kontrolowania struktury koperty protokołu SOAP zarówno dla komunikatu żądania, jak i komunikatu odpowiedzi, oraz użycia zarówno MessageHeaderAttribute nagłówka protokołu SOAP dla komunikatu odpowiedzi, jak i MessageBodyMemberAttribute (w celu określenia treści zarówno żądania, jak i komunikatu odpowiedzi). Przykładowy kod zawiera przykład każdego komunikatu po wysłaniu.

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace = "Microsoft.WCF.Documentation")]
  interface IMessagingHello
  {
    [OperationContract(
     Action = "http://GreetingMessage/Action",
     ReplyAction = "http://HelloResponseMessage/Action"
    )]
    HelloResponseMessage Hello(HelloGreetingMessage msg);
  }

  [MessageContract]
  public class HelloResponseMessage
  {
    private string localResponse = String.Empty;
    private string extra = String.Empty;

    [MessageBodyMember(
      Name = "ResponseToGreeting",
      Namespace = "http://www.examples.com")]
    public string Response
    {
      get { return localResponse; }
      set { localResponse = value; }
    }

    [MessageHeader(
      Name = "OutOfBandData",
      Namespace = "http://www.examples.com",
      MustUnderstand=true
    )]
    public string ExtraValues
    {
      get { return extra; }
      set { this.extra = value; }
   }

   /*
    The following is the response message, edited for clarity.

    <s:Envelope>
      <s:Header>
        <a:Action s:mustUnderstand="1">http://HelloResponseMessage/Action</a:Action>
        <h:OutOfBandData s:mustUnderstand="1" xmlns:h="http://www.examples.com">Served by object 13804354.</h:OutOfBandData>
      </s:Header>
      <s:Body>
        <HelloResponseMessage xmlns="Microsoft.WCF.Documentation">
          <ResponseToGreeting xmlns="http://www.examples.com">Service received: Hello.</ResponseToGreeting>
        </HelloResponseMessage>
      </s:Body>
    </s:Envelope>
    */
 }
  [MessageContract]
  public class HelloGreetingMessage
  {
    private string localGreeting;

    [MessageBodyMember(
      Name = "Salutations",
      Namespace = "http://www.examples.com"
    )]
    public string Greeting
    {
      get { return localGreeting; }
      set { localGreeting = value; }
    }
  }

  /*
   The following is the request message, edited for clarity.

    <s:Envelope>
      <s:Header>
        <!-- Note: Some header content has been removed for clarity.
        <a:Action>http://GreetingMessage/Action</a:Action>
        <a:To s:mustUnderstand="1"></a:To>
      </s:Header>
      <s:Body u:Id="_0" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <HelloGreetingMessage xmlns="Microsoft.WCF.Documentation">
          <Salutations xmlns="http://www.examples.com">Hello.</Salutations>
        </HelloGreetingMessage>
      </s:Body>
   </s:Envelope>
   */

  class MessagingHello : IMessagingHello
  {
    public HelloResponseMessage Hello(HelloGreetingMessage msg)
    {
      Console.WriteLine("Caller sent: " + msg.Greeting);
      HelloResponseMessage responseMsg = new HelloResponseMessage();
      responseMsg.Response = "Service received: " + msg.Greeting;
      responseMsg.ExtraValues = String.Format("Served by object {0}.", this.GetHashCode().ToString());
      Console.WriteLine("Returned response message.");
      return responseMsg;
    }
  }
}
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.ServiceModel.Channels

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace := "Microsoft.WCF.Documentation")> _
  Friend Interface IMessagingHello
    <OperationContract(Action := "http://GreetingMessage/Action", ReplyAction := "http://HelloResponseMessage/Action")> _
    Function Hello(ByVal msg As HelloGreetingMessage) As HelloResponseMessage
  End Interface

  <MessageContract> _
  Public Class HelloResponseMessage
    Private localResponse As String = String.Empty
    Private extra As String = String.Empty

    <MessageBodyMember(Name := "ResponseToGreeting", Namespace := "http://www.examples.com")> _
    Public Property Response() As String
      Get
          Return localResponse
      End Get
      Set(ByVal value As String)
          localResponse = value
      End Set
    End Property

    <MessageHeader(Name := "OutOfBandData", Namespace := "http://www.examples.com", MustUnderstand:=True)> _
    Public Property ExtraValues() As String
      Get
          Return extra
      End Get
      Set(ByVal value As String)
          Me.extra = value
      End Set
    End Property

'   
'    The following is the response message, edited for clarity.
'    
'    <s:Envelope>
'      <s:Header>
'        <a:Action s:mustUnderstand="1">http://HelloResponseMessage/Action</a:Action>
'        <h:OutOfBandData s:mustUnderstand="1" xmlns:h="http://www.examples.com">Served by object 13804354.</h:OutOfBandData>
'      </s:Header>
'      <s:Body>
'        <HelloResponseMessage xmlns="Microsoft.WCF.Documentation">
'          <ResponseToGreeting xmlns="http://www.examples.com">Service received: Hello.</ResponseToGreeting>
'      </s:Body>    
'    </s:Envelope>
'    
  End Class
  <MessageContract> _
  Public Class HelloGreetingMessage
    Private localGreeting As String

    <MessageBodyMember(Name := "Salutations", Namespace := "http://www.examples.com")> _
    Public Property Greeting() As String
      Get
          Return localGreeting
      End Get
      Set(ByVal value As String)
          localGreeting = value
      End Set
    End Property
  End Class

'  
'   The following is the request message, edited for clarity.
'    
'    <s:Envelope>
'      <s:Header>
'        <!-- Note: Some header content has been removed for clarity.
'        <a:Action>http://GreetingMessage/Action</a:Action> 
'        <a:To s:mustUnderstand="1"></a:To>
'      </s:Header>
'      <s:Body u:Id="_0" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
'        <HelloGreetingMessage xmlns="Microsoft.WCF.Documentation">
'          <Salutations xmlns="http://www.examples.com">Hello.</Salutations>
'      </s:Body>
'   </s:Envelope>
'   

  Friend Class MessagingHello
      Implements IMessagingHello
    Public Function Hello(ByVal msg As HelloGreetingMessage) As HelloResponseMessage Implements IMessagingHello.Hello
      Console.WriteLine("Caller sent: " & msg.Greeting)
      Dim responseMsg As New HelloResponseMessage()
      responseMsg.Response = "Service received: " & msg.Greeting
      responseMsg.ExtraValues = String.Format("Served by object {0}.", Me.GetHashCode().ToString())
      Console.WriteLine("Returned response message.")
      Return responseMsg
    End Function
  End Class
End Namespace

Uwagi

Użyj atrybutu MessageContractAttribute , aby określić strukturę koperty protokołu SOAP dla określonego komunikatu. Usługa może następnie użyć komunikatu jako parametru lub zwracać typ operacji usługi. Aby uzyskać informacje na temat kontrolowania serializacji zawartości treści protokołu SOAP bez modyfikowania domyślnej koperty protokołu SOAP, zobacz System.Runtime.Serialization.DataContractAttribute, Określanie transferu danych w kontraktach usług i Używanie kontraktów danych.

Uwaga

W operacji usługi nie można używać niestandardowych typów komunikatów ze zwykłymi parametrami możliwych do serializacji. Użyj niestandardowych typów komunikatów lub parametrów możliwych do serializacji, które nie Message są obiektami. Aby uzyskać szczegółowe informacje, zobacz , Określanie transferu danych w kontraktach usług.

Aby zaimplementować kontrakt komunikatu dla typu, dodaj do niego MessageContractAttribute adnotację i dodaj adnotację do co najmniej jednego pola lub właściwości klasy za pomocą MessageBodyMemberAttribute, MessageHeaderAttributelub MessageHeaderArrayAttribute.

Uwaga

System.ServiceModel.MessageParameterAttribute nie jest atrybutem kontraktu komunikatu i nie można go używać w połączeniu z MessageContractAttribute.

Action Użyj właściwości iReplyAction, aby określić wartość <Action> elementu w komunikacie SOAP.

  • HasProtectionLevel Użyj właściwości i ProtectionLevel , aby wskazać, czy typ komunikatu PROTOKOŁU SOAP ma poziom ochrony, a jeśli tak, to co to jest.

  • IsWrapped Użyj właściwości , aby wskazać, czy treść komunikatu ma element otoki, a jeśli tak, użyj WrapperName właściwości i WrapperNamespace , aby określić odpowiednio nazwę i przestrzeń nazw elementu opakowującego.

Aby uzyskać więcej informacji, zobacz Korzystanie z kontraktów komunikatów.

Konstruktory

MessageContractAttribute()

Inicjuje nowe wystąpienie klasy MessageContractAttribute.

Właściwości

HasProtectionLevel

Pobiera wartość wskazującą, czy komunikat ma poziom ochrony.

IsWrapped

Pobiera lub ustawia wartość określającą, czy treść komunikatu ma element otoki.

ProtectionLevel

Pobiera lub ustawia wartość, która określa, czy komunikat musi być zaszyfrowany, podpisany, czy oba.

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego Attributeelementu .

(Odziedziczone po Attribute)
WrapperName

Pobiera lub ustawia nazwę elementu otoki treści wiadomości.

WrapperNamespace

Pobiera lub ustawia przestrzeń nazw elementu otoki treści komunikatu.

Metody

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

(Odziedziczone po Attribute)
GetHashCode()

Zwraca wartość skrótu dla tego wystąpienia.

(Odziedziczone po Attribute)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
IsDefaultAttribute()

Podczas zastępowania w klasie pochodnej wskazuje, czy wartość tego wystąpienia jest wartością domyślną dla klasy pochodnej.

(Odziedziczone po Attribute)
Match(Object)

Po przesłonięciu w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

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

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie dla obiektu, który może służyć do pobierania informacji o typie dla interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy