Получение значения в заголовке SOAP из OperationContext - PullRequest
8 голосов
/ 21 июня 2011

У меня есть следующий код в C#, который ищет apiKey в следующем SOAP заголовке:

Заголовок SOAP:

<soap:Header>
   <Authentication>
       <apiKey>CCE4FB48-865D-4DCF-A091-6D4511F03B87</apiKey>
   </Authentication>
</soap:Header>

C #:

Это то, что я имею до сих пор:

public string GetAPIKey(OperationContext operationContext)
{
    string apiKey = null;

    // Look at headers on incoming message.
    for (int i = 0; i < OperationContext.Current.IncomingMessageHeaders.Count; i++)
    {
        MessageHeaderInfo h = OperationContext.Current.IncomingMessageHeaders[i];

        // For any reference parameters with the correct name.
        if (h.Name == "apiKey")
        {
            // Read the value of that header.
            XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
            apiKey = xr.ReadElementContentAsString();
        }
    }

    // Return the API key (if present, null if not).
    return apiKey;
}

ПРОБЛЕМА: Возвращение null вместо фактического apiKey значения:

CCE4FB48-865D-4DCF-A091-6D4511F03B87

ОБНОВЛЕНИЕ 1:

Я добавил некоторые записи. Похоже, что h.Name на самом деле «Аутентификация», что означает, что он фактически не будет искать «apiKey», что означает, что он не сможет получить значение.

Есть ли способ захватить <apiKey /> внутри <Authentication />?

ОБНОВЛЕНИЕ 2:

Завершается с помощью следующего кода:

if (h.Name == "Authentication")
{
  // Read the value of that header.
  XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
  xr.ReadToDescendant("apiKey");
  apiKey = xr.ReadElementContentAsString();
}

Ответы [ 3 ]

7 голосов
/ 22 июня 2011

Я думаю, что ваш h.Name равен Authentication, потому что это корневой тип, а apiKey является свойством типа Authentication.Попробуйте записать значения h.Name в некоторый файл журнала и проверьте, что он возвращает.

if (h.Name == "Authentication")
{
    // Read the value of that header.
    XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
    //apiKey = xr.ReadElementContentAsString();
    xr.ReadToFollowing("Authentication");
    apiKey = xr.ReadElementContentAsString();
}
2 голосов
/ 30 октября 2014

Существует более короткое решение:

public string GetAPIKey(OperationContext operationContext)
{
    string apiKey = null;
    MessageHeaders headers = OperationContext.Current.RequestContext.RequestMessage.Headers;

    // Look at headers on incoming message.
    if (headers.FindHeader("apiKey","") > -1)
        apiKey = headers.GetHeader<string>(headers.FindHeader("apiKey",""));        

    // Return the API key (if present, null if not).
    return apiKey;
}
2 голосов
/ 22 июня 2011

Завершается с помощью следующего кода:

if (h.Name == "Authentication")
{
  // Read the value of that header.
  XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
  xr.ReadToDescendant("apiKey");
  apiKey = xr.ReadElementContentAsString();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...