<?xml version="1.0" encoding="utf-8" ?> <PropertyWrapper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/" />As you can see no properties and values.
using System; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace SampleService { [Serializable()] public class PropertyWrapper : ISerializable { #region " Private Members " [NonSerialized] private Dictionary<string, object> _runtTimeProperties = new Dictionary<string, object>(); #endregion #region " Ctors " /// <summary> /// Default constuctor. /// </summary> public PropertyWrapper() { } /// <summary> /// This constructor is special and required for deserialization. /// </summary> /// <param name="info"></param> /// <param name="context"></param> protected PropertyWrapper(SerializationInfo info, StreamingContext context) { // Enumerate through SerializationInfo and rebuild the internal dictionary. SerializationInfoEnumerator e = info.GetEnumerator(); while (e.MoveNext()) { _runtTimeProperties.Add(e.Name, e.Value); } } #endregion #region " Public Members " #region " AddProperty(string vfpStylePropertyName, object vfpStylePropertyValue) " /// <summary> /// This method is a simple wrapper that allows you to add a property/value /// to the internal dictionary as a key/value pair. /// </summary> /// <param name="vfpStylePropertyName"></param> /// <param name="vfpStylePropertValue"></param> public void AddProperty(string vfpStylePropertyName, object vfpStylePropertyValue) { try { _runtTimeProperties.Add(vfpStylePropertyName, vfpStylePropertyValue); } catch (Exception e) { // Handle duplicate key added to Dictionary. throw new Exception("Error occured adding property", e); } } #endregion #region " GetProperties() " /// <summary> /// Return the dictionary. /// </summary> /// <returns></returns> public Dictionary<string, object> GetProperties() { return _runtTimeProperties; } #endregion #region " GetValue(string propertyName) " /// <summary> /// Get an individual property value. /// </summary> /// <param name="propertyName"></param> /// <returns></returns> public object GetValue(string propertyName) { object outValue = null; // If the propertyName cannot be found in _runtTimeProperties, TryGetValue() // returns false which on this occasion can be discarded. If the value isn't // found, outValue stays null _runtTimeProperties.TryGetValue(propertyName, out outValue); return outValue; } #endregion #endregion #region " ISerializable Members " #region " GetObjectData(SerializationInfo info, StreamingContext context) " /// <summary> /// Add data into the serialization stream. /// </summary> /// <param name="info"></param> /// <param name="context"></param> public void GetObjectData(SerializationInfo info, StreamingContext context) { foreach (KeyValuePair<string, object> pair in _runtTimeProperties) { info.AddValue(pair.Key, pair.Value); } } #endregion #endregion } }3. I edited the service.cs file to look like this:
using System; using System.Data; using System.Web; using System.Collections; using System.Collections.Generic; using System.Web.Services; using System.Web.Services.Protocols; using System.ComponentModel; namespace SampleService { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { [WebMethod] public PropertyWrapper GetInfo() { PropertyWrapper wrapper = new PropertyWrapper(); // Add a few properties to it. wrapper.AddProperty("Name", "Bernard Bout"); wrapper.AddProperty("DOB", Convert.ToDateTime("01/01/1970")); wrapper.AddProperty("Phone", "+44 161 777 5555"); wrapper.AddProperty("Email", "bernard@someplace.com"); return wrapper; } } }4. I edited the service.asmx file to add my namespace:
<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="SampleService.Service" %>The other files I did not change. I compiled and ran and tested the Web Service. As I showed above I did not get any properties nor values. What am I doing wrong? You now have my web service sample code so please help. How is it that your console class can Serialize/DeSerialize properly while my above Web Service does not?
> ... > > PropertyWrapper wrapper = new PropertyWrapper(); > > // Add a few properties to it. > wrapper.AddProperty("Name", "Bernard Bout"); > wrapper.AddProperty("DOB", Convert.ToDateTime("01/01/1970")); > wrapper.AddProperty("Phone", "+44 161 777 5555"); > wrapper.AddProperty("Email", "bernard@someplace.com"); > return wrapper; > >>