Callers of a Custom X++ Service can specify CallContext as part of the operation signature on the generated service reference proxy. See http://technet.microsoft.com/en-us/library/hh582244.aspx. However the operation signatures for AX System services, like the Query Service and Metadata Service, do not include a CallContext parameter. The call context needs to be passed in manually as an operation header using a few WCF tricks.

The linked sample code walks through the scenario of listing the companies for a given AX partition. This requires the caller to specify the PartitionKey value as part of the CallContext operation header.  Similar approach can be used for other CallContext scenarios including setting of Company context or specifying a LogOnAs value.

Access the code sample here: http://code.msdn.microsoft.com/Walkthrough-of-using-73c07391

After downloading the sample code run through the following steps:

1. Create a new Parition. http://technet.microsoft.com/EN-US/library/jj677455.aspx
2. Open the AX client to show created partition. http://technet.microsoft.com/EN-US/library/jj670112.aspx
3. Create company accounts. http://technet.microsoft.com/en-us/library/dd362102.aspx
4. Set value of PartitionKey in code as appropriate.
5. Run the application in debug mode.
 
Since the CallContext data contract is not available for these services, the following data contract can be used to code against.
 
/// <summary>
/// Call Context data contract to be used as header with system service calls.
/// </summary>
[DataContract(Name = CallContext.Name, Namespace = CallContext.Namespace)]
public class CallContext
{
    public const string Name = "CallContext";
    public const string Namespace = "http://schemas.microsoft.com/dynamics/2010/01/datacontracts";

    [DataMember]
    public string Company { get; set; }

    [DataMember]
    public string Language { get; set; }

    [DataMember]
    public string LogonAsUser { get; set; }

    [DataMember]
    public string MessageId { get; set; }

    [DataMember]
    public string PartitionKey { get; set; }
}

The code required for adding the call context to the operation header is as follows:
 
// Get client channel and the equivalent object cast to service interface 
var channel = queryClient.InnerChannel; 
var queryChannel = (QueryService.IQueryService)channel; 
 
// Create an operation context scope 
using (OperationContextScope operationContextScope = new OperationContextScope(channel)) 

    // Add Call Context header to current operation context 
    var header = MessageHeader.CreateHeader(CallContext.Name, CallContext.Namespace, callContext); 
    OperationContext.Current.OutgoingMessageHeaders.Add(header); 
 
    // Execute service operation 
    QueryService.Paging paging = null
    dataSet = queryChannel.ExecuteQuery(query, ref paging); 
}