示例#1
0
        static void Main(string[] args)
        {
            // Create the WcfClient connector
            WcfConnectionStringBuilder builder = new WcfConnectionStringBuilder();

            builder.EndpointName = "ApplicationClient";
            WcfClientConnector client = new WcfClientConnector(builder.GenerateConnectionString()); // Endpoint name should match the ep name in app.config

            // Setup the formatter
            client.Formatter = new MARC.Everest.Formatters.XML.ITS1.Formatter();
            client.Formatter.GraphAides.Add(new DatatypeFormatter());

            // We want the service to validate our messages, not the formatter, so lets just turn off the
            // formatter validation for now
            (client.Formatter as MARC.Everest.Formatters.XML.ITS1.Formatter).ValidateConformance = false;

            // Open the connection
            client.Open();

            // Start the async send
            IAsyncResult iaSendResult = client.BeginSend(CreateInstance(), null, null);

            Console.WriteLine("Formatting and sending, please wait...");
            iaSendResult.AsyncWaitHandle.WaitOne(); // Wait until the response is received

            // Now we have to check that the message was actually sent to the remote system
            WcfSendResult sndResult = client.EndSend(iaSendResult) as WcfSendResult;

            // The result of sending the message wasn't good.
            if (sndResult.Code != ResultCode.Accepted && sndResult.Code != ResultCode.AcceptedNonConformant)
            {
                Console.WriteLine("The message wasn't sent!");
            }
            else // The connector has sent the message
            {
                // Receive the result (this involves waiting...)
                IAsyncResult iaRcvResult = client.BeginReceive(sndResult, null, null);
                Console.WriteLine("Parsing result...");
                iaRcvResult.AsyncWaitHandle.WaitOne(); // Wait for deserialization
                WcfReceiveResult rcvResult = client.EndReceive(iaRcvResult) as WcfReceiveResult;

                // Now lets print out the structure
                client.Formatter.Graph(Console.OpenStandardOutput(), rcvResult.Structure);
            }

            // Close the connection
            client.Close();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        /// <summary>
        /// Send HL7v3 messages to a specified endpoint.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="endpointName">Name of the endpoint.</param>
        /// <returns><c>true</c> If the message sent successfully, <c>false</c> otherwise.</returns>
        public static bool Sendv3Messages(IGraphable message, string endpointName)
        {
            var retVal = true;

            var client = new WcfClientConnector($"endpointName={endpointName}");

            var formatter = new XmlIts1Formatter
            {
                ValidateConformance = true
            };

            client.Formatter = formatter;
            client.Formatter.GraphAides.Add(new DatatypeFormatter());

            client.Open();

            var sendResult = client.Send(message);

            traceSource.TraceEvent(TraceEventType.Verbose, 0, "Sending HL7v3 message to endpoint: " + client.ConnectionString);

            if (sendResult.Code != ResultCode.Accepted && sendResult.Code != ResultCode.AcceptedNonConformant)
            {
                traceSource.TraceEvent(TraceEventType.Error, 0, "Send result: " + Enum.GetName(typeof(ResultCode), sendResult.Code));
                retVal = false;
            }

            var receiveResult = client.Receive(sendResult);

            if (receiveResult.Code != ResultCode.Accepted && receiveResult.Code != ResultCode.AcceptedNonConformant)
            {
                traceSource.TraceEvent(TraceEventType.Error, 0, "Receive result: " + Enum.GetName(typeof(ResultCode), receiveResult.Code));
                retVal = false;
            }

            var result = receiveResult.Structure;

            if (result == null)
            {
                traceSource.TraceEvent(TraceEventType.Error, 0, "Receive result structure is null");
                retVal = false;
            }

            client.Close();

            return(retVal);
        }
        /// <summary>
        /// Notify
        /// </summary>
        /// <param name="workItem"></param>
        public void Notify(NotificationQueueWorkItem workItem)
        {
            ILocalizationService locale = this.Context.GetService(typeof(ILocalizationService)) as ILocalizationService;

            // Create a message utility
            MessageUtility msgUtil = new MessageUtility()
            {
                Context = this.Context
            };

            // Create the EV formatters
            XmlIts1Formatter formatter = new XmlIts1Formatter()
            {
                ValidateConformance = false
            };

            formatter.GraphAides.Add(new DatatypeFormatter()
            {
                ValidateConformance = false
            });

            // Iterate through the targets attempting to notify each one
            using (WcfClientConnector wcfClient = new WcfClientConnector(Target.ConnectionString))
            {
                wcfClient.Formatter = formatter;
                wcfClient.Open();

                // Build the message
                Trace.TraceInformation("Sending notification to '{0}'...", this.Target.Name);
                IInteraction notification = msgUtil.CreateMessage(workItem.Event, workItem.Action, this.Target);

                // Send it
                var sendResult = wcfClient.Send(notification);
                if (sendResult.Code != Everest.Connectors.ResultCode.Accepted &&
                    sendResult.Code != Everest.Connectors.ResultCode.AcceptedNonConformant)
                {
                    Trace.TraceWarning(string.Format(locale.GetString("NTFW002"), this.Target.Name));
                    DumpResultDetails(sendResult.Details);
                    return;
                }

                // Receive the response
                var rcvResult = wcfClient.Receive(sendResult);
                if (rcvResult.Code != Everest.Connectors.ResultCode.Accepted &&
                    rcvResult.Code != Everest.Connectors.ResultCode.AcceptedNonConformant)
                {
                    Trace.TraceWarning(string.Format(locale.GetString("NTFW003"), this.Target.Name));
                    DumpResultDetails(rcvResult.Details);
                    return;
                }

                // Get structure
                var response = rcvResult.Structure as MCCI_IN000002UV01;
                if (response == null)
                {
                    Trace.TraceWarning(string.Format(locale.GetString("NTFW003"), this.Target.Name));
                    return;
                }

                if (response.Acknowledgement.Count == 0 ||
                    response.Acknowledgement[0].TypeCode != AcknowledgementType.AcceptAcknowledgementCommitAccept)
                {
                    Trace.TraceWarning(string.Format(locale.GetString("NTFW004"), this.Target.Name));
                    return;
                }


                // Close the connector and continue
                wcfClient.Close();
            }
        }