/// <summary> /// Enables inspection or modification of a message before a request message is sent to a service. /// </summary> /// <param name="request">The message to be sent to the service.</param> /// <param name="channel">The client object channel.</param> /// <returns> /// The object that is returned as the correlationState argument of /// the <see cref="M:System.ServiceModel.Dispatcher.IClientMessageInspector.AfterReceiveReply(System.ServiceModel.Channels.Message@,System.Object)"/> /// method. This is null if no correlation state is used.The best practice is to make this a <see cref="T:System.Guid"/> /// to ensure that no two correlationState objects are the same. /// </returns> public object BeforeSendRequest(ref Message request, IClientChannel channel) { if (MessageHeaderData == null) { // custermize the message header data messageHeaderData = new CustomizedMessageHeaderData(); } int index = request.Headers.FindHeader(CustomizedMessageHeaderConstants.MessageHeaderDataName, CustomizedMessageHeaderConstants.MessageHeaderDataNamespace); if (index < 0) { // modify the message headers before the request message is sent out MessageHeader header = MessageHeader.CreateHeader(CustomizedMessageHeaderConstants.MessageHeaderDataName, CustomizedMessageHeaderConstants.MessageHeaderDataNamespace, MessageHeaderData); request.Headers.Add(header); } return(null); }
/// <summary> /// 将定制的消息头内容填入至消息中 /// </summary> /// <typeparam name="TContract">服务契约类型</typeparam> /// <param name="factory">服务通道创建工厂</param> /// <param name="endpoint">服务通道终结点</param> /// <param name="messageHeader">定制的消息头</param> private static void FixInternal <TContract>(ChannelFactory <TContract> factory, ServiceEndpoint endpoint, CustomizedMessageHeaderData messageHeader) where TContract : class { if (endpoint != null) { foreach (OperationDescription operation in factory.Endpoint.Contract.Operations) { DataContractSerializerOperationBehavior behavior; if (!operation.Behaviors.Contains(typeof(DataContractSerializerOperationBehavior))) { behavior = new DataContractSerializerOperationBehavior(operation); operation.Behaviors.Add(behavior); } else { behavior = operation.Behaviors[typeof(DataContractSerializerOperationBehavior)] as DataContractSerializerOperationBehavior; } foreach (OperationDescription originalOperation in endpoint.Contract.Operations) { if (operation.Name == originalOperation.Name) { DataContractSerializerOperationBehavior originalBehavior = originalOperation.Behaviors[typeof(DataContractSerializerOperationBehavior)] as DataContractSerializerOperationBehavior; if (behavior != null && originalBehavior != null) { behavior.IgnoreExtensionDataObject = originalBehavior.IgnoreExtensionDataObject; behavior.MaxItemsInObjectGraph = originalBehavior.MaxItemsInObjectGraph; } break; } } } } if (!factory.Endpoint.Behaviors.Contains(typeof(CustomizedMessageBehavior))) { if (messageHeader == null) { factory.Endpoint.Behaviors.Add(new CustomizedMessageBehavior()); } else { factory.Endpoint.Behaviors.Add(new CustomizedMessageBehavior(messageHeader)); } } }
/// <summary> /// 将定制的消息头内容填入至消息中 /// </summary> /// <typeparam name="TContract">服务契约类型</typeparam> /// <param name="factory">服务通道创建工厂</param> /// <param name="endpoint">服务通道终结点</param> /// <param name="messageHeader">定制的消息头</param> public static void Fix <TContract>(ChannelFactory <TContract> factory, ServiceEndpoint endpoint, CustomizedMessageHeaderData messageHeader) where TContract : class { FixInternal <TContract>(factory, endpoint, messageHeader); }
/// <summary> /// 将定制的消息头内容填入至消息中 /// </summary> /// <typeparam name="TContract">服务契约类型</typeparam> /// <param name="factory">服务通道创建工厂</param> /// <param name="messageHeader">定制的消息头</param> public static void Fix <TContract>(ChannelFactory <TContract> factory, CustomizedMessageHeaderData messageHeader) where TContract : class { FixInternal <TContract>(factory, null, messageHeader); }
/// <summary> /// 定制化消息行为 /// </summary> /// <param name="messageHeader">定制化消息头数据</param> public CustomizedMessageBehavior(CustomizedMessageHeaderData messageHeader) { messageHeaderData = messageHeader; }
/// <summary> /// Enables inspection or modification of a message before a request message is sent to a service. /// </summary> /// <param name="request">The message to be sent to the service.</param> /// <param name="channel">The client object channel.</param> /// <returns> /// The object that is returned as the correlationState argument of /// the <see cref="M:System.ServiceModel.Dispatcher.IClientMessageInspector.AfterReceiveReply(System.ServiceModel.Channels.Message@,System.Object)"/> /// method. This is null if no correlation state is used.The best practice is to make this a <see cref="T:System.Guid"/> /// to ensure that no two correlationState objects are the same. /// </returns> public object BeforeSendRequest(ref Message request, IClientChannel channel) { if (MessageHeaderData == null) { // custermize the message header data messageHeaderData = new CustomizedMessageHeaderData(); } int index = request.Headers.FindHeader(CustomizedMessageHeaderConstants.MessageHeaderDataName, CustomizedMessageHeaderConstants.MessageHeaderDataNamespace); if (index < 0) { // modify the message headers before the request message is sent out MessageHeader header = MessageHeader.CreateHeader(CustomizedMessageHeaderConstants.MessageHeaderDataName, CustomizedMessageHeaderConstants.MessageHeaderDataNamespace, MessageHeaderData); request.Headers.Add(header); } return null; }