Sie können einen erstellen IClientMessageInspector
/IEndpointBehavior
diesen Wert wie folgt festzulegen: (ja dieser Code ist ausführlich, aber das ist die Art und Weise WCF funktioniert;)
public class AuthorizationHeaderMessageInspector : IClientMessageInspector, IEndpointBehavior
{
object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty prop;
Object obj;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
{
prop = (HttpRequestMessageProperty)obj; // throws a cast exception if invalid type
}
else
{
prop = new HttpRequestMessageProperty();
request.Properties.Add(HttpRequestMessageProperty.Name, prop);
}
prop.Headers[HttpRequestHeader.Authorization] = "your authorization value here";
return null;
}
void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
{
}
void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
{
}
}
Dann, wenn Sie Ihre Client die Nachricht hinzufügen erstellen Inspektor wie folgt:
MemberAccountPortClient clientTransaction = new MemberAccountPortClient ("SERVICE");
clientTransaction.Endpoint.Behaviors.Add(new AuthorizationHeaderMessageInspector());
SearchTransactionResponseType res = clientTransaction.searchTransaction (OBJECT_1, OBJECT_2);
ich glaube, dass WCF eine Art und Weise hat die IEndpointBehavior
mit Konfiguration als auch zu bewerben, aber ich gehe in der Regel gerade Code für diese Art von Dingen.
Vielen Dank @dana! – Crasher