Bagikan melalui


ServiceContractAttribute Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Indicates that an interface or a class defines a service contract in a Silverlight client application.

Inheritance Hierarchy

System.Object
  System.Attribute
    System.ServiceModel.ServiceContractAttribute

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Interface, Inherited := False,  _
    AllowMultiple := False)> _
Public NotInheritable Class ServiceContractAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Interface, Inherited = false, 
    AllowMultiple = false)]
public sealed class ServiceContractAttribute : Attribute

The ServiceContractAttribute type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone ServiceContractAttribute Initializes a new instance of the ServiceContractAttribute class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone CallbackContract Gets or sets the type of callback contract when the contract is a duplex contract.
Public propertySupported by Silverlight for Windows Phone ConfigurationName Gets or sets the name used to locate the service in an application configuration file.
Public propertySupported by Silverlight for Windows Phone Name Gets or sets the name for the <portType> element in Web Services Description Language (WSDL).
Public propertySupported by Silverlight for Windows Phone Namespace Gets or sets the namespace of the <portType> element in Web Services Description Language (WSDL).

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

Use the ServiceContractAttribute attribute on an interface (or class) to define a service contract. Then use the OperationContractAttribute attribute on one or more of the class (or interface) methods to define the contract's service operations.

The information expressed by a ServiceContractAttribute and its interface are loosely related to the Web Services Description Language (WSDL) <portType> element. A service contract is used on the service to specify what the service’s endpoint exposes to callers. It is also used on the client to specify the contract of the endpoint with which the client communicates.

NoteNote:

An interface or class that is marked with ServiceContractAttribute must also have at least one method marked with the OperationContractAttribute attribute to expose any functionality. See the Examples section for a code example of the most basic use of the two attributes to define and implement a service.

Use the ServiceContractAttribute properties to modify the service contract.

  • The Name and Namespace properties control the name and namespace of the contract in the WSDL <portType> element.

  • The CallbackContract property specifies the return contract in a two-way (duplex) conversation.

Services implement service contracts, which represent the data exchange that a service type supports. A service class can implement a service contract (by implementing an interface marked with ServiceContractAttribute that has methods marked with OperationContractAttribute) or it can be marked with the ServiceContractAttribute and apply the OperationContractAttribute attribute to its own methods. (If a class implements an interface marked with ServiceContractAttribute, it cannot be itself marked with ServiceContractAttribute.) Methods on service types that are marked with the OperationContractAttribute are treated as part of a default service contract specified by the service type itself.

For more information about

service operations, see OperationContractAttribute.

By default, the Name and Namespace properties are the name of the contract type and http://tempuri.org, respectively. It is recommended that service contracts explicitly set their names and namespaces using these properties. Doing so builds a contract that is not directly connected to the managed type information, enabling you to re-factor your managed code and namespaces without breaking the contract as it is expressed in WSDL.

Clients either use the service contract interface (the interface marked with ServiceContractAttribute) to create a channel to the service or they use the client objects (which combine the type information of the service contract interface with the ClientBase<TChannel> class) to communicate with your service.

Using a ServiceContractAttribute class or interface to inherit from another ServiceContractAttribute class or interface extends the parent contract. For example, if an IChildContract interface is marked with ServiceContractAttribute and inherited from another service contract interface, IParentContract, the IChildContract service contract contains the methods of both IParentContract and IChildContract. Extending contracts (whether on classes or interfaces) is very similar to extending managed classes and interfaces.

The most flexible approach to creating clients is to define service contract interfaces first and then have your client class implement that interface. Building clients directly by marking a class with ServiceContractAttribute and its methods with OperationContractAttribute works as well.

Examples

    'The following code contains an example of a duplex contract that contains a callback contract.
    <ServiceContract(Name := "SampleContract", Namespace := "Silverlight", CallbackContract := GetType(IDuplexClient))> _
    Public Interface IDuplexService
        <OperationContract(IsOneWay := True)> _
        Sub Order(ByVal name As String, ByVal quantity As Integer)
    End Interface

    <ServiceContract> _
    Public Interface IDuplexClient
        <OperationContract(IsOneWay := True)> _
        Sub Receive(ByVal order As Order)
    End Interface

    Public Class Order
        Private privateStatus As OrderStatus
        Public Property Status() As OrderStatus
            Get
                Return privateStatus
            End Get
            Set(ByVal value As OrderStatus)
                privateStatus = value
            End Set
        End Property
        Private privatePayload As List(Of String)
        Public Property Payload() As List(Of String)
            Get
                Return privatePayload
            End Get
            Set(ByVal value As List(Of String))
                privatePayload = value
            End Set
        End Property
    End Class

    Public Enum OrderStatus
        Processing
        Completed
    End Enum

    //The following code contains an example of a duplex contract that contains a callback contract.
    [ServiceContract(
        Name = "SampleContract", 
        Namespace = "Silverlight", 
        CallbackContract = typeof(IDuplexClient))]
    public interface IDuplexService
    {
        [OperationContract(IsOneWay = true)]
        void Order(string name, int quantity);
    }

    [ServiceContract]
    public interface IDuplexClient
    {
        [OperationContract(IsOneWay = true)]
        void Receive(Order order);
    }

    public class Order
    {
        public OrderStatus Status { get; set; }
        public List<string> Payload { get; set; }
    }

    public enum OrderStatus
    {
        Processing,
        Completed
    }

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.