Example #1
0
        public override async Task InvokeMethodGrpcAsync(string appId, string methodName, CancellationToken cancellationToken = default)
        {
            ArgumentVerifier.ThrowIfNullOrEmpty(appId, nameof(appId));
            ArgumentVerifier.ThrowIfNullOrEmpty(methodName, nameof(methodName));

            var envelope = new Autogenerated.InvokeServiceRequest()
            {
                Id      = appId,
                Message = new Autogenerated.InvokeRequest()
                {
                    Method = methodName,
                },
            };

            var options = CreateCallOptions(headers: null, cancellationToken);

            try
            {
                _ = await this.Client.InvokeServiceAsync(envelope, options);
            }
            catch (RpcException ex)
            {
                throw new InvocationException(appId, methodName, ex);
            }
        }
Example #2
0
        public override async Task <TResponse> InvokeMethodGrpcAsync <TRequest, TResponse>(string appId, string methodName, TRequest data, CancellationToken cancellationToken = default)
        {
            ArgumentVerifier.ThrowIfNullOrEmpty(appId, nameof(appId));
            ArgumentVerifier.ThrowIfNullOrEmpty(methodName, nameof(methodName));

            var envelope = new Autogenerated.InvokeServiceRequest()
            {
                Id      = appId,
                Message = new Autogenerated.InvokeRequest()
                {
                    Method      = methodName,
                    ContentType = Constants.ContentTypeApplicationGrpc,
                    Data        = Any.Pack(data),
                },
            };

            var options = CreateCallOptions(headers: null, cancellationToken);

            try
            {
                var response = await this.Client.InvokeServiceAsync(envelope, options);

                return(response.Data.Unpack <TResponse>());
            }
            catch (RpcException ex)
            {
                throw new InvocationException(appId, methodName, ex);
            }
        }
        private (Autogenerated.InvokeServiceRequest, CallOptions) MakeInvokeRequestAsync(
            string appId,
            string methodName,
            Any data,
            HttpInvocationOptions httpOptions,
            CancellationToken cancellationToken = default)
        {
            var      protoHTTPExtension = new Autogenerated.HTTPExtension();
            Metadata headers            = null;

            string contentType;

            if (httpOptions != null)
            {
                protoHTTPExtension.Verb = ConvertHTTPVerb(httpOptions.Method);

                if (httpOptions.QueryString != null)
                {
                    foreach (var(key, value) in httpOptions.QueryString)
                    {
                        protoHTTPExtension.Querystring.Add(key, value);
                    }
                }

                if (httpOptions.Headers != null)
                {
                    headers = new Metadata();
                    foreach (var(key, value) in httpOptions.Headers)
                    {
                        headers.Add(key, value);
                    }
                }

                contentType = httpOptions.ContentType ?? Constants.ContentTypeApplicationJson;
            }
            else
            {
                protoHTTPExtension.Verb = Autogenerated.HTTPExtension.Types.Verb.Post;
                contentType             = Constants.ContentTypeApplicationJson;
            }

            var invokeRequest = new Autogenerated.InvokeRequest()
            {
                Method        = methodName,
                Data          = data,
                ContentType   = contentType,
                HttpExtension = protoHTTPExtension
            };

            var request = new Autogenerated.InvokeServiceRequest()
            {
                Id      = appId,
                Message = invokeRequest,
            };

            var callOptions = new CallOptions(headers: headers ?? new Metadata(), cancellationToken: cancellationToken);

            // add token for dapr api token based authentication
            var daprApiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");

            if (daprApiToken != null)
            {
                callOptions.Headers.Add("dapr-api-token", daprApiToken);
            }

            return(request, callOptions);
        }