본문 바로가기

.NET/WCF

[WCF] 클라이언트에서 ArrayList를 WCF서비스 인자로 넘기기 꼼수

반응형

-- Client 단

List<string> list2 = new List<string>();
ArrayList arList = new ArrayList();

for(int j = 0 ; j = 3; j++)
{
 for (int i = 0; i < 10; i++)
{
list2.Add(i.ToString());
}
arList.Add(list2);
}

//ArrayList를 직렬화 
//Soap 메세지가 XML기반이끼대문에 String으로 받아야함
string t123 = CommonClass.SerializeListStringArrayList(arList);

serviceClient.Insert(LoginData.CommonCode, list1.ToArray(), t123 );
....


    public static class CommonClass
    {
        public static string SerializeListStringArrayList(ArrayList obj)
        {
            XmlDocument doc = new XmlDocument();
            Type[] extraTypes = new Type[1];
            extraTypes[0] = typeof(List<string>);
            XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), extraTypes);
            MemoryStream stream = new MemoryStream();
            try
            {
                serializer.Serialize(stream, obj);
                stream.Position = 0;
                doc.Load(stream);
                return doc.InnerXml;
            }

            catch { throw; }

            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }
      }




-- Service 단

[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public int Insert(string commCode, string[] merchandiseKey, string merchandiseDetailKey)
{
//ArrayList로 역직렬화
ArrayList alist = SystemClass.DeSerializeListStringArrayList (merchandiseDetailKey);
....
}



class SystemClass
{
class SystemClass
{
        public static ArrayList DeSerializeListStringArrayList(string serializedData)
        {
            ArrayList list = new ArrayList();
            Type[] extraTypes = new Type[1];
            extraTypes[0] = typeof(List<string>);
XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), extraTypes);
            XmlReader xReader = XmlReader.Create(new StringReader(serializedData));
            try
            {
                object obj = serializer.Deserialize(xReader);
                list = (ArrayList)obj;
            }

            catch
            {
                throw;
            }

            finally
            {
                xReader.Close();
            }

            return list;
        }
}
}