我用了同样的例子(”
WCF WSDL Soap Header on all operations
),并且出现了相同的错误。为了解决这个错误,我在IDispatchMessageInspector的函数“AfterReceiveRequest”中删除了收入请求的标题。我以这样一种方式修改了这个示例,头只添加到传入消息中。
[DataContract(Name = "AuthHeader", Namespace = "web")]
public class AuthHeader
{
[DataMember(Order = 1)]
public string UserName { get; set; }
[DataMember(Order = 2)]
public string Password { get; set; }
}
public class SoapHeaderEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior
{
#region BehaviorExtensionElement Members
public override Type BehaviorType
{
get
{
return typeof(SoapHeaderEndpointBehavior);
}
}
protected override object CreateBehavior()
{
return new SoapHeaderEndpointBehavior();
}
#endregion
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
//throw new NotImplementedException();
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
var inspector = new FvsMessageInspector();
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
var channelDispatcher = endpointDispatcher.ChannelDispatcher;
if (channelDispatcher == null)
return;
foreach (var ed in channelDispatcher.Endpoints)
{
var inspector = new FvsMessageInspector();
ed.DispatchRuntime.MessageInspectors.Add(inspector);
}
foreach (OperationDescription operationDescription in endpoint.Contract.Operations)
{
string nmm = operationDescription.Name;
foreach (MessageDescription msgDescription in operationDescription.Messages)
{
if (msgDescription.Direction == MessageDirection.Input)
{
MessageHeaderDescription header = new MessageHeaderDescription("AuthHeader", "web");
header.Type = typeof(AuthHeader);
msgDescription.Headers.Add(header);
}
}
}
}
public void Validate(ServiceEndpoint endpoint)
{
//throw new NotImplementedException();
}
#endregion
}
public class FvsMessageInspector : IDispatchMessageInspector, IClientMessageInspector
{
#region IDispatchMessageInspector
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
int i = request.Headers.FindHeader("AuthHeader", "web");
if (i >= 0)
{
AuthHeader header = request.Headers.GetHeader<AuthHeader>(i);
//Use header info here
request.Headers.RemoveAt(i);
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
//No need to do anything else
}
#endregion
#region IClientMessageInspector
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
//No need to do anything else
}
#endregion
}
https://wcfextrasplus.codeplex.com/wikipage?title=SOAP%20Headers&referringTitle=Documentation