Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HttpRequestMessageProperty.Method Property

Gets or sets the HTTP verb for the HTTP request.

Namespace:  System.ServiceModel.Channels
Assemblies:   System.ServiceModel.Http (in System.ServiceModel.Http.dll)
  System.ServiceModel (in System.ServiceModel.dll)

public string Method { get; set; }

Property Value

Type: System.String
The HTTP verb for the HTTP request.

ExceptionCondition
ArgumentNullException

The value is set to null.

By default, WCF uses the POST verb for HTTP messages. The GET verb is used by WCF to display help information when accessing a ServiceHost's base address. This is useful for checking whether a WCF service is active when using a Web browser. Other methods defined by the HTTP RFC are PUT, DELETE, HEAD, OPTIONS, TRACE, and CONNECT. These methods have special behaviors when interoperating with other services.

The following code gets an instance of this class from the message and then dispatches to different methods based on this property.

public Message ProcessMessage(Message request)
{
    Message response = null;

    //The HTTP Method (e.g. GET) from the incoming HTTP request 
    //can be found on the HttpRequestMessageProperty. The MessageProperty 
    //is added by the HTTP Transport when the message is received.
    HttpRequestMessageProperty requestProperties =
        (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];

    //Here we dispatch to different internal implementation methods 
    //based on the incoming HTTP verb. 
    if (requestProperties != null)
    {
        if (String.Equals("GET", requestProperties.Method,
            StringComparison.OrdinalIgnoreCase))
        {
            response = GetCustomer(request);
        }
        else if (String.Equals("POST", requestProperties.Method,
            StringComparison.OrdinalIgnoreCase))
        {
            response = UpdateCustomer(request);
        }
        else if (String.Equals("PUT", requestProperties.Method,
            StringComparison.OrdinalIgnoreCase))
        {
            response = AddCustomer(request);
        }
        else if (String.Equals("DELETE", requestProperties.Method,
            StringComparison.OrdinalIgnoreCase))
        {
            response = DeleteCustomer(request);
        }
        else
        {
            //This service doesn't implement handlers for other HTTP verbs (such as HEAD), so we 
            //construct a response message and use the HttpResponseMessageProperty to 
            //set the HTTP status code to 405 (Method Not Allowed) which indicates the client  
            //used an HTTP verb not supported by the server.
            response = Message.CreateMessage(MessageVersion.None, String.Empty, String.Empty);

            HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty();
            responseProperty.StatusCode = HttpStatusCode.MethodNotAllowed;

            response.Properties.Add( HttpResponseMessageProperty.Name, responseProperty );
        }
    }
    else
    {
        throw new InvalidOperationException( "This service requires the HTTP transport" );
    }

    return response;
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft