我有时会收到一个错误:
There is not a header with name UserName and namespace http://www.website.com/ in the message.
堆栈跟踪
System.ServiceModel.Channels.MessageHeaders.GetHeader[T](String name, String ns, String[] actor) Common.Utilities.WCF.WcfCallContext.TryGetHeader(String key) Common.Utilities.WCF.WcfCallContext.get_UserName()
这是2种方法:
private static string TryGetHeader( string key )
{
try
{
return GetHeader( key );
}
catch
{
return null;
}
}
private static string GetHeader( string key )
{
var headers = OperationContext.Current.IncomingMessageHeaders;
var value = headers.GetHeader<string>( key, "http://www.website.com/", "Project" );
return value;
}
}
所以 TryGetHeader(with try and catch) 正在调用 GetHeader。显然它打破了:
var value = headers.GetHeader<string>( key, "http://www.website.com/", "Project" );
那么为什么它TryGetHeader
不会将其视为错误并且不返回 a null
?就好像它GetHeader
闯入并停止,而不是在TryGetHeader
?