우리는 모두 InstanceContextMode = PerCall과 함께 작동하는 여러 WCF 서비스를 가지고 있으며 모든 WCF 서비스 인스턴스는 Unity (IOC)를 사용하고 IInstanceProvider를 구현하여 생성됩니다.WCF Percall 모드 및 스레드 고정 변수
상관 ID는 동일한 ID로 모든 메소드 호출 및 데이터베이스 프로세스를 감사하는 데 사용됩니다.
이렇게하기 위해 IDispatchBehavior를 구현하여 끝점 동작을 만들고 AfterReceiveRequest 메서드에서 GUID를 생성하고 ThreadStatic (CommonData
) 속성에 할당합니다. 이 속성은 응용 프로그램의 모든 레이어에서 액세스 할 수 있습니다. 다음 코드 블록은 CommonData 및 CommonData 클래스의 채우기를 보여줍니다.
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
CommonData.ClearDictionary();
//the lines between are deleted as they are not relevant to the question
CommonData.Current.Add(MessageHeaderCodes.CorrelationId, Guid.NewGuid().ToString());
return null;
}
및 commondata 클래스 :
public class CommonData
{
[ThreadStatic]
private static Dictionary<string, string> headerData;
public static Dictionary<string, string> Current
{
get
{
if (headerData == null)
{
headerData = new Dictionary<string, string>();
}
return headerData;
}
}
private CommonData()
{
}
public static string GetHeader(string header)
{
string headerValue = string.Empty;
KeyValuePair<string, string> headerKeyValuePair = CommonData.Current.SingleOrDefault(p => p.Key == header);
headerValue = headerKeyValuePair.Value;
return headerValue;
}
public static void ClearDictionary()
{
Current.Clear();
}
}
여기서 문제는 다음과 같다; 일부 서비스에서 개발자는 상관 식별자가 null을 반환한다고보고했습니다. 문제가 간헐적이기 때문에 현재 전체 스택 추적을 가질 수 없습니다. 또한 IIS를 다시 설정하면이 문제가 일시적으로 해결됩니다. 내가 혼란스러워하고
나는 몇 가지 코드를하고 싶었을 제외하고이 정말 귀하의 질문에 대답하지 않고, 코멘트했을
식별자를 검색하는 방법 (GetHeader 메서드로 추측)은 무엇입니까? – Bond
클라이언트에서 식별자를 얻지 못했습니다. 각 호출에 대해 AfterReceiveRequest 메서드에서 새 식별자를 만듭니다. CommonData.Current.Add (MessageHeaderCodes.CorrelationId, Guid.NewGuid(). ToString()); – daryal
네,하지만 어떻게 든 나중에 다시 검색하려고합니까? 방법? 그렇지 않다면 - 정확히 null이 무엇입니까? 사실 정확한 질문 인 – Bond