[C#/COMMON/PROTO.ACTOR/.NET6] Props 클래스 : WithReceiverMiddleware/WithSenderMiddleware 메소드를 사용해 미들웨어 설정하기
■ Props 클래스의 WithReceiverMiddleware/WithSenderMiddleware 메소드를 사용해 미들웨어를 설정하는 방법을 보여준다. ▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
using Proto; namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { MessageHeader messageHeader = new MessageHeader ( new Dictionary<string, string> { { "TraceID", Guid.NewGuid().ToString() }, { "SpanID" , Guid.NewGuid().ToString() } } ); ActorSystem actorSystem = new ActorSystem(); RootContext rootContext = new RootContext ( actorSystem, messageHeader, next => async (context, target, envelope) => { MessageEnvelope newEnvelope = envelope .WithHeader("TraceID" , context.Headers.GetOrDefault("TraceID")) .WithHeader("SpanID" , Guid.NewGuid().ToString()) .WithHeader("ParentSpanID", context.Headers.GetOrDefault("SpanID")); Console.WriteLine(" 1 루트 컨텍스트 발송자 미들웨어 진입"); Console.WriteLine(" 1 TraceID : " + newEnvelope.Header.GetOrDefault("TraceID" )); Console.WriteLine(" 1 SpanID : " + newEnvelope.Header.GetOrDefault("SpanID" )); Console.WriteLine(" 1 ParentSpanID : " + newEnvelope.Header.GetOrDefault("ParentSpanID")); await next(context, target, newEnvelope); Console.WriteLine(" 1 루트 컨텍스트 발송자 미들웨어 이탈 - 송신은 비동기식입니다. 설계상 순서가 잘못되었습니다."); } ); Props props = Props.FromFunc ( context => { if(context.Message is string) { Console.WriteLine( " 3 액터 진입"); Console.WriteLine($" 3 TraceID : {context.Headers.GetOrDefault("TraceID" )}"); Console.WriteLine($" 3 SpanID : {context.Headers.GetOrDefault("SpanID" )}"); Console.WriteLine($" 3 ParentSpanID : {context.Headers.GetOrDefault("ParentSpanID")}"); Console.WriteLine($" 3 액터 수신 : {context.Message.GetType()}:{context.Message}"); context.Respond("세계!"); Console.WriteLine( " 3 액터 이탈"); } return Task.CompletedTask; } ) .WithReceiverMiddleware ( receiver => async (context, envelope) => { if(envelope.Message is string) { MessageEnvelope newEnvelope = envelope .WithHeader("TraceID" , envelope.Header.GetOrDefault("TraceID")) .WithHeader("SpanID" , Guid.NewGuid().ToString()) .WithHeader("ParentSpanID", envelope.Header.GetOrDefault("SpanID")); Console.WriteLine(" 2 액터 수신자 미들웨어 진입"); Console.WriteLine(" 2 TraceID : " + newEnvelope.Header.GetOrDefault("TraceID" )); Console.WriteLine(" 2 SpanID : " + newEnvelope.Header.GetOrDefault("SpanID" )); Console.WriteLine(" 2 ParentSpanID : " + newEnvelope.Header.GetOrDefault("ParentSpanID")); await receiver(context, newEnvelope); Console.WriteLine(" 2 액터 수신자 미들웨어 이탈"); } else { await receiver(context, envelope); } } ) .WithSenderMiddleware ( sender => async (context, target, envelope) => { MessageEnvelope newEnvelope = envelope .WithHeader("TraceID" , context.Headers.GetOrDefault("TraceID")) .WithHeader("SpanID" , Guid.NewGuid().ToString()) .WithHeader("ParentSpanID", context.Headers.GetOrDefault("SpanID")); Console.WriteLine(" 4 액터 발송자 미들웨어 진입"); Console.WriteLine(" 4 TraceID : " + newEnvelope.Header.GetOrDefault("TraceID" )); Console.WriteLine(" 4 SpanID : " + newEnvelope.Header.GetOrDefault("SpanID" )); Console.WriteLine(" 4 ParentSpanID : " + newEnvelope.Header.GetOrDefault("ParentSpanID")); await sender(context, target, envelope); Console.WriteLine(" 4 액터 발송자 미들웨어 이탈"); } ); PID pid = rootContext.Spawn(props); Task.Delay(500).Wait(); Console.WriteLine("0 TraceID : " + rootContext.Headers.GetOrDefault("TraceID" )); Console.WriteLine("0 SpanID : " + rootContext.Headers.GetOrDefault("SpanID" )); Console.WriteLine("0 ParentSpanID : " + rootContext.Headers.GetOrDefault("ParentSpanID")); string result = rootContext.RequestAsync<string>(pid, "안녕하세요").Result; Console.WriteLine("결과 : " + result); Console.WriteLine("프로그램을 종료하기 위해서 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(false); } #endregion } |
TestProject.zip