オブジェクト指向の方法ですべてのWCF呼び出しにカスタムHTTPヘッダーを追加する場合は、これ以上探す必要はありません。
Mark Goodやpaulwhitの回答と同様にIClientMessageInspector、カスタムHTTPヘッダーをWCF要求に挿入するためにサブクラスを作成する必要があります。ただし、追加するヘッダーを含むディクショナリを受け入れることにより、インスペクターをより汎用的にすることができます。
public class HttpHeaderMessageInspector : IClientMessageInspector
{
    private Dictionary<string, string> Headers;
    public HttpHeaderMessageInspector(Dictionary<string, string> headers)
    {
        Headers = headers;
    }
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        // ensure the request header collection exists
        if (request.Properties.Count == 0 || request.Properties[HttpRequestMessageProperty.Name] == null)
        {
            request.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());
        }
        // get the request header collection from the request
        var HeadersCollection = ((HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name]).Headers;
        // add our headers
        foreach (var header in Headers) HeadersCollection[header.Key] = header.Value;
        return null;
    }
    // ... other unused interface methods removed for brevity ...
}
Mark Goodとpaulwhitの答えと同様に、WCFクライアントIEndpointBehaviorに注入するためにサブクラス化する必要がありHttpHeaderMessageInspectorます。
public class AddHttpHeaderMessageEndpointBehavior : IEndpointBehavior
{
    private IClientMessageInspector HttpHeaderMessageInspector;
    public AddHttpHeaderMessageEndpointBehavior(Dictionary<string, string> headers)
    {
        HttpHeaderMessageInspector = new HttpHeaderMessageInspector(headers);
    }
    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.ClientMessageInspectors.Add(HttpHeaderMessageInspector);
    }
    // ... other unused interface methods removed for brevity ...
}
オブジェクト指向のアプローチを完了するために必要な最後の部分は、WCF自動生成クライアントのサブクラスを作成することです(私はMicrosoftのWCF Webサービスリファレンスガイドを使用しました)を使用してWCFクライアントを生成しました)。
私の場合、APIキーを x-api-key HTMLヘッダーます。
サブクラスは次のことを行います。
- 必須パラメーターを使用して基本クラスのコンストラクターを呼び出します(私の場合は EndpointConfiguration、コンストラクターに渡す列挙型が生成されました-おそらく実装にはこれがありません)
- すべてのリクエストに添付する必要があるヘッダーを定義します
- AddHttpHeaderMessageEndpointBehaviorクライアントの- Endpoint動作にアタッチします
public class Client : MySoapClient
{
    public Client(string apiKey) : base(EndpointConfiguration.SomeConfiguration)
    {
        var headers = new Dictionary<string, string>
        {
            ["x-api-key"] = apiKey
        };
        var behaviour = new AddHttpHeaderMessageEndpointBehavior(headers);
        Endpoint.EndpointBehaviors.Add(behaviour);
    }
}
最後に、クライアントを使用してください!
var apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
var client = new Client (apiKey);
var result = client.SomeRequest()
結果のHTTPリクエストにはHTTPヘッダーが含まれ、次のようになります。
POST http://localhost:8888/api/soap HTTP/1.1
Cache-Control: no-cache, max-age=0
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8
Accept-Encoding: gzip, deflate
x-api-key: XXXXXXXXXXXXXXXXXXXXXXXXX
SOAPAction: "http://localhost:8888/api/ISoapService/SomeRequest"
Content-Length: 144
Host: localhost:8888
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <SomeRequestxmlns="http://localhost:8888/api/"/>
  </s:Body>
</s:Envelope>