public async Task SubscribeAsync() { var subscriber = await this.subscriberLazy.Value; this.tokenSource.Token.Register(() => { subscriber.StopAsync(CancellationToken.None).Wait(); }); await subscriber.StartAsync(async(PubsubMessage message, CancellationToken token) => { var text = Encoding.UTF8.GetString(message.Data.ToArray()); var type = this.typeHelper.GetType(x => x.FullName == (message.Attributes[nameof(Type)])); var executerType = typeof(IMessageReceiver <>).MakeGenericType(type); try { var obj = JsonConvert.DeserializeObject(text, type); var pair = new TypeValuePair(obj, type); using (var scope = this.scopeProvider.BeginLifetimeScope()) { var executer = scope.Resolve(executerType, pair) as IMessageReceiver; var res = await executer.ExecuteAsync(); return(res == 0 ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack); } } catch (Exception ex) { this.logger.LogError(ex, $"Error {type} {executerType} {text}"); return(SubscriberClient.Reply.Nack); } }); }
public static TResult Assert <TResult>(this AssertionScope scope, TypeValuePair value) { var result = value.GetUnionResult <TResult>(items => AssertNoMethod <TResult>(scope, value.Type, items)); var assertedResult = CheckItemHelper <TResult>(scope, result.TypeValuePair, result.MethodInfo); return(assertedResult); }
internal static UnionResult GetUnionResult <TResult>(this TypeValuePair item, Action <List <UnionMethodInfo> > failedToFind) { var resultType = typeof(TResult); var methodInfo = item.Type.GetAppropriateMethod(resultType, out var possibilities); if (methodInfo == null) { failedToFind(possibilities); } var result = item.Value.GetUnionResult(methodInfo); return(new UnionResult(methodInfo, result)); }
private async Task <int> ExecuteAsync(Message message, CancellationTokenSource source) { var type = this.typeHelper.GetType(x => x.FullName == (message.MessageAttributes[SQSConstans.TypeFullNameKey].StringValue)); var obj = JsonConvert.DeserializeObject(message.Body, type); var executerType = typeof(IMessageReceiver <>).MakeGenericType(type); var inheritTokenSource = new TypeValuePair(source); using (var scope = this.scopeProvider.BeginLifetimeScope(inheritTokenSource)) { var pair = new TypeValuePair(obj, type); var executer = scope.Resolve(executerType, pair) as IMessageReceiver; return(await executer.ExecuteAsync()); } }
public void GetFunc() { var resultInstance = new TypeValuePair(typeof(int), 12345); TypeValuePair Get <T>(T input) => resultInstance; var result = MethodUtils.MakeMatchArg(Get, typeof(string)); var resolvedMethod = result .Should() .BeOfType <Func <string, TypeValuePair> >() .Which; var typePairResult = resolvedMethod .Invoke("Test"); typePairResult.Should().BeEquivalentTo(resultInstance); }
//Action<int> wrappedFunction -- this arg has been removed because I haven't figured out the generics to inline the assertions being tested internal static TypeValuePair GetUnionResult(this object item, UnionMethodInfo unionMethodData) { if (unionMethodData.IsSwitch) { TypeValuePair closureResult = null; void GetSwitch <TValue>(TValue value) => closureResult = new TypeValuePair(typeof(TValue), value); void GetSwitchOptional() => throw new Exception("Optional parameter cannot resolve a value"); var switchMainArgs = unionMethodData.CaseTypes.Select(x => MethodUtils.MakeAction(GetSwitch, x)); var switchFinalArgs = switchMainArgs.Concat( unionMethodData.OptionalLast ? new[] { MethodUtils.MakeAction(GetSwitchOptional) } : new object[0]) .ToArray(); unionMethodData.Method.Invoke(item, switchFinalArgs); return(closureResult); } TypeValuePair GetMatch <TValue>(TValue value) => new TypeValuePair(typeof(TValue), value); TypeValuePair GetMatchOptional() => new TypeValuePair(typeof(None), None.Value); var mainArgs = unionMethodData.CaseTypes.Select(x => MethodUtils.MakeMatchArg(GetMatch, x)); var finalArgs = mainArgs.Concat( unionMethodData.OptionalLast ? new[] { MethodUtils.MakeFunc(GetMatchOptional) } : new object[0]) .ToArray(); return((TypeValuePair)unionMethodData.Method .MakeGenericMethod(typeof(TypeValuePair)) .Invoke(item, finalArgs)); }
internal static TResult CheckItemHelper <TResult>(AssertionScope scope, TypeValuePair typeValuePair, UnionMethodInfo methodInfo) { var itemType = typeValuePair.Type; var expectedType = typeof(TResult); var expectedTypePretty = expectedType.PrettyPrint(); var givenTypes = methodInfo.OptionalLast ? methodInfo.CaseTypes.Concat(new[] { typeof(None) }).ToArray() : methodInfo.CaseTypes; var givenTypesPretty = string.Join(", ", givenTypes.Select(ReflectionUtils.PrettyPrint)); scope .ForCondition(givenTypes.Contains(expectedType)) .FailWith("Value should be one of {0} but found {1} instead.", givenTypesPretty, expectedTypePretty); scope .ForCondition(expectedType.IsAssignableFrom(itemType)) .FailWith("Value should be assignable to {0} but found {1} instead", expectedTypePretty, itemType.PrettyPrint()); return((TResult)typeValuePair.Value); }
public CharacterAttribute(TypeValuePair <AttributeType, float> attributeInfo) { this.AttributeInfo = attributeInfo; }
public ElementalResistance(ElementalType type, float value) { ResistanceInfo = new TypeValuePair <ElementalType, float>(type, value); }
public static TypedParameter Convert(this TypeValuePair pair) => new TypedParameter(pair.Type, pair.Value);