Exemple #1
0
        /// <summary>
        /// Sends a synchonous/blocking message to the appv service.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <remarks>This function blocks until it can connect to the appv service, ie it waits until the server can make the connection.</remarks>
        /// <returns>A response from the service</returns>
        public Response SendMessage(Request request)
        {
            // This lock ensures that no other client thread can connect until we're done this function(which includes this function getting a valid response first)
            lock (ThreadLock)
            {
                LogMessage(MethodBase.GetCurrentMethod().Name, "Sending message to service...");
                var pipeClient = new NamedPipeClientStream(".", AppVServicePipeName, PipeDirection.InOut, PipeOptions.WriteThrough, TokenImpersonationLevel.Impersonation);

                LogMessage(MethodBase.GetCurrentMethod().Name, "Waiting to connect to service...");
                pipeClient.Connect();

                LogMessage(MethodBase.GetCurrentMethod().Name, "Connected to service.");
                return(ServiceBrokerProtocolHelper.SendRequest(request, pipeClient, LogMessage));
            }
        }
        public static Response SendRequest(Request request, Stream pipeClient, Action <string, string> loggingFunction)
        {
            loggingFunction(MethodBase.GetCurrentMethod().Name, "Sending request to service...");
            var streamString = new StreamString(pipeClient);

            loggingFunction(MethodBase.GetCurrentMethod().Name, "Serializing request...");
            var requestXml = ServiceBrokerProtocolHelper.Serialize(request);

            loggingFunction(MethodBase.GetCurrentMethod().Name, "Transmitting request...");

            // Send request
            streamString.WriteString(requestXml);
            loggingFunction(MethodBase.GetCurrentMethod().Name, "Request sent to service.");

            loggingFunction(MethodBase.GetCurrentMethod().Name, "Receiving response from service...");

            //Recieve the response
            var response = ServiceBrokerProtocolHelper.Deserialize <Response>(streamString.ReadString());

            loggingFunction(MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "Response received from service: Code={0}, MessageBody={1}", response.ResponseCode, response.MessageBody));
            return(response);
        }
        public string SendToAppVService(string payload)
        {
            string ret;

            try
            {
                var request = ServiceBrokerProtocolHelper.Deserialize <Request>(payload);
                // This client requires that the CtxAppVService disable name pipe security to work.
                var client   = new AppVService((methodName, message) => {
                    //  string tempPath = Path.GetTempPath();
                    //  var line = string.Format("{0} : {1}",methodName,message);
                    //File.AppendAllText("TestClassLibrary", line );
                });
                var response = client.SendMessage(request);
                ret = ServiceBrokerProtocolHelper.Serialize(response);
                return(ret);
            }
            catch (Exception e)
            {
                const int max_length = 200;
                string    message    = "Er:" + e.Message + e.StackTrace;
                return(message.Length <= max_length ? message : message.Substring(0, max_length));
            }
        }