To change the endpoint or company you are manipulating data in from an AIF web service you need to create the message header and call the service operation inside of an OperationContextScope. There is a full example of how to do this on msdn – http://msdn.microsoft.com/en-us/library/cc652581.aspx . Below is a partial example of how to setup the message header and call it within an instance of the OperationContextScope class:
public void createCustomer()
{
System.ServiceModel.OperationContextScope operContextScope;
customerClient = new AIF.CustomerServiceClient();
try
{
operContextScope = new System.ServiceModel.OperationContextScope(customerClient.InnerChannel);
using (operContextScope)
{
this.messageHeader = new MessageHeader(this.destinationEndpoint, this.sourceEndpointUser, this.sourceEndpoint);
this.performService();
}
}
catch (Exception e)
{
Console.Out.WriteLine(“Unable to create the customer” + this.customerInfo.AccountNum + “. Please check the Exceptions window within AX.”);
Console.Out.WriteLine(e.ToString());
}
}
class MessageHeader
{
private string destinationEndpoint;
private string endpointUser;
private string sourceEndpoint;
/// <summary>
/// Creates the message header
/// </summary>
/// <param name=”destinationEndpoint”>The DestinationEndpoint</param>
/// <param name=”sourceEndpointUser”>The SourceEndpointUser</param>
/// <param name=”sourceEndpoint”>The SourceEndpoint</param>
public MessageHeader(string destinationEndpoint, string sourceEndpointUser, string sourceEndpoint)
{
this.destinationEndpoint = destinationEndpoint;
this.endpointUser = sourceEndpointUser;
this.sourceEndpoint = sourceEndpoint;
this.prepareHeader();
}
/// <summary>
/// Sets up the MessageId, DestinationEndpoint, and SourceEndpointUser in
/// the header information
/// </summary>
private void prepareHeader()
{
this.setHeaderDestinationEndpoint();
this.setEndpointUser();
this.setHeaderMsgId();
}
private void setHeaderMsgId()
{
UniqueId guidUniqueId = new UniqueId(Guid.NewGuid());
OperationContext.Current.OutgoingMessageHeaders.MessageId = guidUniqueId;
}
private void setHeaderDestinationEndpoint()
{
System.ServiceModel.Channels.MessageHeader messageHeader = System.ServiceModel.Channels.MessageHeader.CreateHeader
(“DestinationEndpoint”, “http://schemas.microsoft.com/dynamics/2008/01/services“, this.destinationEndpoint);
OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
}
private void setEndpointUser()
{
AddressHeader addressHeader;
EndpointAddressBuilder addressBuilder;
EndpointAddress endpointAddress;
addressHeader = AddressHeader.CreateAddressHeader
(“SourceEndpointUser”, “http://schemas.microsoft.com/dynamics/2008/01/services“, this.endpointUser);
endpointAddress = new EndpointAddress(new Uri(“urn:” + this.sourceEndpoint), addressHeader);
addressBuilder = new EndpointAddressBuilder(endpointAddress);
OperationContext.Current.OutgoingMessageHeaders.From = addressBuilder.ToEndpointAddress();
}
}