示例#1
0
        /// <summary>
        ///  Registers Rpc-related services with the Dependency Injection provider
        /// </summary>
        private void ConfigureRpcServices(IServiceCollection services)
        {
            // Register our RPC services and tie it to a sample service
            services.AddSingleton <IRpcService>((services) => {
                var deviceClient = services.GetService <DeviceClient>();
                var registration = services.GetService <IDeviceRegistrationProvider>();

                var handler = new DispatchingClientMessageHandler();
                var jsonRpc = new JsonRpc(handler);

                // Receive
                deviceClient.SetMethodHandlerAsync(_ConfigurationOptions.RpcMethodName, (request, context) => {
                    handler.Dispatch(request.DataAsJson, registration.DeviceId);
                    return(Task.FromResult(new MethodResponse(new byte[0], 200)));
                }, null).ConfigureAwait(false).GetAwaiter().GetResult();

                // Send
                handler.SendAsync = async(message, clientId) => {
                    var iotMessage = new Message(Encoding.UTF8.GetBytes(message));
                    await deviceClient.SendEventAsync(iotMessage);
                };

                jsonRpc.StartListening();

                return(jsonRpc.Attach <IRpcService>());
            });
        }
示例#2
0
 /// <summary>
 /// Constructs a new EventGridRpcFunction class
 /// </summary>
 /// <param name="jsonRpc">The JsonRpc instance we'll use monitor for message processing completion</param>
 /// <param name="messageHandler">The message handler we'll use to dispatch Message to the RPC service</param>
 /// <param name="rpcService">
 /// The service instance the RPC will call against.  This is not needed to be used, but we need to have
 /// a constructor reference to it so DI will generate it and the JsonRpc instance can call methods against it.
 ///</param>
 public EventGridRpcFunction(
     JsonRpc jsonRpc,
     DispatchingClientMessageHandler messageHandler,
     IRpcService rpcService
     )
 {
     _JsonRpc        = jsonRpc;
     _MessageHandler = messageHandler;
     _RpcService     = rpcService;
 }
示例#3
0
        /// <summary>
        ///  Registers Rpc-related services with the Dependency Injection provider
        /// </summary>
        private void ConfigureRpcServices(IServiceCollection services)
        {
            // Register a Dispatching RPC Message Handler scoped for each function invocation
            // Configure this handler to send outgoing messages using IoT CloudToDeviceMethods
            services.AddScoped <DispatchingClientMessageHandler>((services) => {
                var serviceClient = services.GetService <ServiceClient>();
                var logger        = services.GetService <ILogger <FunctionsStartup> >();

                var handler = new DispatchingClientMessageHandler();

                handler.SendAsync = async(message, clientId) => {
                    logger.LogTrace($"Sending RPC message to {clientId}");
                    logger.LogTrace(message);

                    var method = new CloudToDeviceMethod(CLOUD_TO_DEVICE_METHOD_NAME);
                    method.SetPayloadJson(message);

                    await serviceClient.InvokeDeviceMethodAsync(clientId, method);

                    logger.LogTrace($"Successfully sent RPC message to {clientId}");
                };

                return(handler);
            });

            // Register a Function-scoped JsonRpc that uses the Message Handler
            services.AddScoped <JsonRpc>((services) =>
                                         new JsonRpc(services.GetService <DispatchingClientMessageHandler>())
            {
                CancelLocallyInvokedMethodsWhenConnectionIsClosed = false
            }
                                         );

            // Register a new SampleService instance that's an invocation target for JsonRpc
            services.AddScoped <IRpcService>((services) => {
                var jsonRpc = services.GetService <JsonRpc>();
                var handler = services.GetService <DispatchingClientMessageHandler>();
                var service = new RpcService();

                jsonRpc.AddLocalRpcTarget(service);
                jsonRpc.StartListening();

                return(service);
            });
        }