(0) exportieren Drucken
Alle erweitern

MessageContractAttribute-Klasse

Defines a strongly-typed class that corresponds to a SOAP message.

Namespace: System.ServiceModel
Assembly: System.ServiceModel (in system.servicemodel.dll)

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple=false)] 
public sealed class MessageContractAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple=false) */ 
public final class MessageContractAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple=false) 
public final class MessageContractAttribute extends Attribute
Nicht zutreffend.

Use the MessageContractAttribute attribute to specify the structure of the SOAP envelope for a particular message. Your service can then use the message as a parameter or return type in service operations. For 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 Data Contracts Overview.

HinweisHinweis:

You cannot use custom message types in your service operation with regular serializable parameters. Either use custom message types or serializable parameters that are not Message objects. For details, see , Specifying Data Transfer in Service Contracts.

To implement a message contract for a type, annotate it with MessageContractAttribute and annotate one or more of the class's fields or properties with MessageBodyMemberAttribute, MessageHeaderAttribute, or MessageHeaderArrayAttribute.

HinweisHinweis:

System.ServiceModel.MessageParameterAttribute is not a message contract attribute and cannot be used in conjunction with MessageContractAttribute.

Use the Action and ReplyAction properties to specify the value of the <Action> element in the SOAP message.

  • Use the HasProtectionLevel and ProtectionLevel properties to indicate whether the SOAP message type has a protection level, and if so, what it is.

  • Use the IsWrapped property to indicate whether the message body has a wrapper element, and if so, use the WrapperName and WrapperNamespace properties to specify the name and namespace, respectively, of the wrapping element.

For more information, see Using Message Contracts.

The following code example shows the use of the MessageContractAttribute to control the SOAP envelope structure for both the request message and the response message, and the use of both the MessageHeaderAttribute (to create a SOAP header for the response message) and the MessageBodyAttribute (to specify the bodies of both the request and response message). The code example contains an example of each message when sent.

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;
    }
  }
}

System.Object
   System.Attribute
    System.ServiceModel.MessageContractAttribute

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

Microsoft .NET Framework 3.0 wird unter Windows Vista, Microsoft Windows XP SP2 und Windows Server 2003 SP1 unterstützt.

.NET Framework

Unterstützt in: 3.0
Microsoft führt eine Onlineumfrage durch, um Ihre Meinung zur MSDN-Website zu erfahren. Wenn Sie sich zur Teilnahme entscheiden, wird Ihnen die Onlineumfrage angezeigt, sobald Sie die MSDN-Website verlassen.

Möchten Sie an der Umfrage teilnehmen?
Anzeigen:
© 2014 Microsoft