Example #1
0
        public ISMEV3Result SendRequest <T>(T requestData, IDictionary <string, byte[]> attachments)
        {
            var result = new SMEV3Result {
                MessageId = GuidGenerator.GenerateTimeBasedGuid().ToString()
            };

            try
            {
                var senderRequestData = new SenderProvidedRequestData();
                senderRequestData.Id                 = "SIGNED_BY_CONSUMER";
                senderRequestData.MessageID          = result.MessageId;
                senderRequestData.ReferenceMessageID = senderRequestData.MessageID;

                if (testmessage)
                {
                    senderRequestData.TestMessage = new Smev3Service.Void();
                }

                senderRequestData.MessagePrimaryContent = SerializeDetails <T>(requestData);

                AttachmentContentType[] contentList = null;
                if (attachments != null && attachments.Count > 0)
                {
                    // передача будет через MTOM
                    if (attachments.Sum(o => o.Value.Length) < 5242880)
                    {
                        var attachementHeaders  = new List <AttachmentHeaderType>();
                        var attachementContents = new List <AttachmentContentType>();

                        foreach (var attachment in attachments)
                        {
                            var attachementHeader = new AttachmentHeaderType
                            {
                                contentId      = attachment.Key,
                                MimeType       = "application/octet-stream",
                                SignaturePKCS7 = SignedPkcs7.ComputeSignature(attachment.Value)
                            };
                            var attachementContent = new AttachmentContentType
                            {
                                Id      = attachment.Key,
                                Content = attachment.Value
                            };
                            attachementHeaders.Add(attachementHeader);
                            attachementContents.Add(attachementContent);
                        }
                        senderRequestData.AttachmentHeaderList = attachementHeaders.ToArray();
                        contentList = attachementContents.ToArray();
                    }
                    else                     // Передача через FTP
                    {
                        var refAttachements = new List <RefAttachmentHeaderType>();
                        foreach (var attachment in attachments)
                        {
                            var uuid = GuidGenerator.GenerateTimeBasedGuid().ToString();

                            FtpUpLoad(uuid, attachment.Key, attachment.Value);

                            var hash = SignedPkcs7.ComputeDigest(attachment.Value);

                            var refAttachement = new RefAttachmentHeaderType
                            {
                                uuid           = uuid,
                                Hash           = Convert.ToBase64String(hash),
                                MimeType       = "application/octet-stream",
                                SignaturePKCS7 = SignedPkcs7.ComputeSignatureDigest(hash)
                            };
                            refAttachements.Add(refAttachement);
                        }
                        senderRequestData.RefAttachmentHeaderList = refAttachements.ToArray();
                    }
                }

                var request  = new SendRequest(senderRequestData, null, null);
                var smevSign = SerializeWithSign(request, "SIGNED_BY_CONSUMER");
                request.CallerInformationSystemSignature = smevSign;
                request.AttachmentContentList            = contentList;

                var response = smev.SendRequest(request);
                result.SoapRequest  = behavior.SoapRequest;
                result.SoapResponse = behavior.SoapResponse;
                if (!response.MessageMetadata.StatusSpecified)
                {
                    result.Status = SMEV3ResultStatus.OK;
                    return(result);
                }

                var status = response.MessageMetadata.Status;
                if (status == InteractionStatusType.requestIsQueued ||
                    status == InteractionStatusType.requestIsAcceptedBySmev ||
                    status == InteractionStatusType.underProcessing)
                {
                    result.Status = SMEV3ResultStatus.OK;
                }
                else
                {
                    result.Status    = SMEV3ResultStatus.Fail;
                    result.ErrorText = "Статус сообщения, обработка которого не была предусмотрена";
                }
            }
            catch (FaultException e)
            {
                result.SoapRequest  = behavior.SoapRequest;
                result.SoapResponse = behavior.SoapResponse;
                result.Status       = SMEV3ResultStatus.Fail;
                result.ErrorText    = e.Message;
            }
            catch (Exception e)
            {
                result.Status    = SMEV3ResultStatus.Error;
                result.ErrorText = "";
                while (e != null)
                {
                    result.ErrorText += e.Message + "\n";
                    result.ErrorText += e.StackTrace + "\n\n";
                    e = e.InnerException;
                }
            }

            return(result);
        }
Example #2
0
        /// <summary>
        ///  Отправить ответ
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="responseTo">Адрес доставки ответа, подставляется в /To, берется из запроса потребителя /ReplyTo</param>
        /// <param name="responseData"></param>
        /// <param name="attachments"></param>
        /// <returns></returns>
        public ISMEV3Result SendResponse <T>(string responseTo, T responseData, IDictionary <string, byte[]> attachments)
        {
            var result = new SMEV3Result {
                MessageId = GuidGenerator.GenerateTimeBasedGuid().ToString()
            };

            try
            {
                var senderResponseData = new SenderProvidedResponseData();
                senderResponseData.Id        = "SIGNED_BY_PROVIDER";
                senderResponseData.MessageID = result.MessageId;
                senderResponseData.To        = responseTo;

                senderResponseData.MessagePrimaryContent = SerializeDetails <T>(responseData);

                AttachmentContentType[] contentList = null;
                if (attachments != null && attachments.Count > 0)
                {
                    // передача будет через MTOM
                    if (attachments.Sum(o => o.Value.Length) < 5242880)
                    {
                        var attachementHeaders  = new List <AttachmentHeaderType>();
                        var attachementContents = new List <AttachmentContentType>();

                        foreach (var attachment in attachments)
                        {
                            var attachementHeader = new AttachmentHeaderType
                            {
                                contentId      = attachment.Key,
                                MimeType       = "application/octet-stream",
                                SignaturePKCS7 = SignedPkcs7.ComputeSignature(attachment.Value)
                            };
                            var attachementContent = new AttachmentContentType
                            {
                                Id      = attachment.Key,
                                Content = attachment.Value
                            };
                            attachementHeaders.Add(attachementHeader);
                            attachementContents.Add(attachementContent);
                        }
                        contentList = attachementContents.ToArray();
                        senderResponseData.AttachmentHeaderList = attachementHeaders.ToArray();
                    }
                    else                     // Передача через FTP
                    {
                        var refattachements = new List <RefAttachmentHeaderType>();
                        foreach (var attachment in attachments)
                        {
                            var uuid = GuidGenerator.GenerateTimeBasedGuid().ToString();

                            FtpUpLoad(uuid, attachment.Key, attachment.Value);

                            var hash = SignedPkcs7.ComputeDigest(attachment.Value);

                            var refAttachement = new RefAttachmentHeaderType
                            {
                                uuid           = uuid,
                                Hash           = Convert.ToBase64String(hash),
                                MimeType       = "application/octet-stream",
                                SignaturePKCS7 = SignedPkcs7.ComputeSignatureDigest(hash)
                            };
                            refattachements.Add(refAttachement);
                        }
                        senderResponseData.RefAttachmentHeaderList = refattachements.ToArray();
                    }
                }

                var request  = new SendResponse(senderResponseData, null, null);
                var smevSign = SerializeWithSign(request, "SIGNED_BY_PROVIDER");
                request.CallerInformationSystemSignature = smevSign;
                request.AttachmentContentList            = contentList;

                var response = smev.SendResponse(request);
                result.SoapRequest  = behavior.SoapRequest;
                result.SoapResponse = behavior.SoapResponse;
                result.Status       = SMEV3ResultStatus.OK;
            }
            catch (FaultException e)
            {
                result.SoapRequest  = behavior.SoapRequest;
                result.SoapResponse = behavior.SoapResponse;
                result.Status       = SMEV3ResultStatus.Fail;
                result.ErrorText    = e.Message;
            }
            catch (Exception e)
            {
                result.Status     = SMEV3ResultStatus.Error;
                result.ErrorText  = e.Message;
                result.ErrorText += "\n" + e.StackTrace;
            }

            return(result);
        }