WCF xmldocument parameter

来源:互联网 发布:手机淘宝5.6.0.版本 编辑:程序博客网 时间:2024/06/16 17:12

WCF xmldocument parameter

 

前段时间,遇到一个问题,就是XmlDocument作为参数在WCF Service的使用问题. 众所周知,WCF 中只直接对 XMLElement支持.当时我就觉得有些不可思议.因为在之前的WebService都是直接支持XmlDocument作为参数的.难道另有隐情,微软觉得直接以XmlDocument传递不好,或是难以实现.带着这些疑问在网上找了一些资料,最终结合自己的思考.得出了如下的解决方案.红色字体是要注意的地方。

 

一.WCF Server

 

1.Interface的实现

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;

namespace WSL.ATOMSG.Member2ATMService
{

    [ServiceContract]
    [XmlSerializerFormat]
    public interface IService
    {

        [OperationContract]
        string SendXml(XmlDocument xmlDoc);

    } 

}

 

2.Service的实现

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;
using System.Configuration;
using WSL.ATOMSG.PL.Common;
using WSL.ATOMSG.Common;

namespace WSL.ATOMSG.Member2ATMService
{

    public class Service : IService
    {

        public string SendXml(XmlDocument xmlDoc)
        {
            try
            {
                string member2atmExchange = ConfigurationManager.AppSettings["Member2ATMExchange"];
                string member2atmQueue = ConfigurationManager.AppSettings["Member2ATMQueue"];
                RabbitMQ.RabbitmqWrapper wrapper = new WSL.ATOMSG.RabbitMQ.RabbitmqWrapper();
                wrapper.PublishMessage(member2atmExchange, member2atmQueue, "test", xmlDoc.InnerXml);
                return member2atmExchange + member2atmQueue + xmlDoc.InnerXml;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

    }
}

二.WCF Client

1.Client的实现

 

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using WSL.ATOMSG.PL.Common;


namespace WSL.ATOMSG.Member2ATMClient
{
    public partial class ClientForm : Form
    {
        public ClientForm()
        {
            InitializeComponent();
        }

 

        private void btnSendXml_Click(object sender, EventArgs e)
        {
            Member2ATMRef.ServiceClient client = new WSL.ATOMSG.Member2ATMClient.Member2ATMRef.ServiceClient();

            string xml = txtMessageContent.Text;
            xml = xml.Replace("/r", "");
            xml = xml.Replace("/n", "");
           this.GetSendingMessage(this.cboRequestorDN.Text.Trim(), this.cboResponderDN.Text.Trim(), txtServiceCode.Text.Trim(),Guid.NewGuid(), cboMessageType.Text.Trim(), txtReference.Text.Trim(), xml);

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            string result = client.SendXml(xmlDoc.DocumentElement);
            txtInfomation.Text = result;
        }


        private XmlDocument GetSendingMessage(string requestorDN, string responderDN, string serviceCode, Guid msgId, string msgType, string msgRef, string swiftXml)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string errorMessage = string.Empty;

            try
            {
                string msgTypeProject = MessageDetailHelper.GetMsgTypeProject(msgType);
                string prefix = string.Empty;
                if (msgTypeProject != "")
                {
                    prefix = msgTypeProject.Substring(0, msgTypeProject.IndexOf("."));
                }
                else
                {
                    msgTypeProject = "setr.010.001.03";
                    prefix = "setr";
                }
                string wcfIP = "192.168.1.237";

                xmlDoc.LoadXml(string.Format(@"<?xml version='1.0'?>
                                    <ns1:SendMsgRequest xmlns:ns1='http://www.wealthcraft.com/ATOMS/SNFG/1.0/Schemas/{0}/{1}'>
                                     <RequestorIP xmlns=''>{2}</RequestorIP>
                                     <RequestorDN xmlns=''>{3}</RequestorDN>
                                     <ResponderDN xmlns=''>{4}</ResponderDN>
                                     <ServiceCode xmlns=''>{5}</ServiceCode>
                                        <MessageId xmlns=''>{6}</MessageId>
                                     <MsgFormat xmlns=''>XML</MsgFormat>
                                     <MsgType xmlns=''>{7}</MsgType>
                                     <MsgRef xmlns=''>{8}</MsgRef>
                                     <DupRef xmlns=''/>
                                     <MsgBody xmlns=''>{9}</MsgBody>
                                    </ns1:SendMsgRequest>",
                                         prefix, msgTypeProject, wcfIP, requestorDN, responderDN,
                                         serviceCode, msgId, msgType, msgRef, swiftXml));
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }

            return xmlDoc;
        }
    }
}