I have the following wcf service:

namespace Scanner
{        
    [ServiceContract(Namespace="scan.rsna.org/WebServices/BarcodeScanner")]
    public interface IBarcodeScanner
    {
        [OperationContract]
        bool InsertScanEvent(BarcodeScanner.ScannedItem barcode);
    }
}

namespace Scanner
{    
    public class BarcodeScanner : IBarcodeScanner
    {
        string dbConnection = ConfigurationManager.ConnectionStrings["annualMeeting"].ConnectionString;

        [DataContract]
        public class ScannedItem
        {
            Guid _scanID = new Guid();
            string _badge;
            DateTime _scanTime;
            string _computer = Dns.GetHostName();

            public ScannedItem(Guid scanID, string badge, DateTime scanTime)
            {
                this._scanID = scanID;
                this._badge = badge;
                this._scanTime = scanTime;
            }

            [DataMember]
            public Guid scanID { get { return _scanID; } }

            [DataMember]
            public string badge { get { return _badge; } }

            [DataMember]
            public DateTime scanTime { get { return _scanTime; } }

            [DataMember]
            public string computer { get { return _computer; } }
        }

        public bool InsertScanEvent(ScannedItem barcode)
        {
            bool success = true;

            SqlConnection scanConnect = new SqlConnection(dbConnection);

            try
            {
                scanConnect.Open();

                SqlCommand insertCommand = new SqlCommand("scan_InsertScanEvent", scanConnect);
                insertCommand.CommandType = CommandType.StoredProcedure;
                insertCommand.Parameters.AddWithValue("@scanID", barcode.scanID);
                insertCommand.Parameters.AddWithValue("@badge", int.Parse(barcode.badge));
                insertCommand.Parameters.AddWithValue("@scanTime", SqlDbType.DateTime).Value = barcode.scanTime;
                insertCommand.Parameters.AddWithValue("@computer", barcode.computer);
                insertCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                success = false;
            }
            finally
            {
                scanConnect.Close();
            }

            return success;
        }
    }
}

And I'm trying to call it from a WinForm:

namespace BarcodeScanner
{
    public partial class scanForm : Form
    {
        public class ScannedItem
        {
            Guid _scanID = new Guid();
            string _badge;
            DateTime _scanTime;
            string _computer = Dns.GetHostName();

            public ScannedItem(Guid scanID, string badge, DateTime scanTime)
            {
                this._scanID = scanID;
                this._badge = badge;
                this._scanTime = scanTime;
            }

            public Guid scanID { get { return _scanID; } }

            public string badge { get { return _badge; } }

            public DateTime scanTime { get { return _scanTime; } }

            public string computer { get { return _computer; } }                
        }            

        // some other code here...

        ScannedItem barcode = new ScannedItem(Guid.NewGuid(), badge.badgeNumber, scanTime);

        BarcodeScannerClient client = new BarcodeScannerClient();

        Scanner.BarcodeScannerScannedItem serviceScannedItem = new Scanner.BarcodeScannerScannedItem();
        serviceScannedItem.badge = barcode.badge;
        serviceScannedItem.computer = barcode.computer;
        serviceScannedItem.scanID = barcode.scanID;
        serviceScannedItem.scanTime = barcode.scanTime;

        bool success = client.InsertScanEvent(serviceScannedItem);

        client.Close();

I get this error though: There was an error while trying to deserialize parameter scan.rsna.org/WebServices/BarcodeScanner:barcode. System.Runtime.Serialization.InvalidDataContractException: No set method for property 'computer' in type 'Scanner.BarcodeScanner+ScannedItem'. The class cannot be deserialized.

I tried using an XmlSerializer to deserialize but that doesn't seem to be possible as ScannedItem does not have a parameterless constructor. And I tried moving the initialization of _computer into the ScannedItem constructor, but that didn't work either.

How can I get this to work?

No set method for property 'computer' in type 'Scanner.BarcodeScanner+ScannedItem'. The class cannot be deserialized.

You cannot deserialize a string if it doesn't have a setter. You are sending a string, but the receiving end cannot store the value in your class property. In short, add a setter.

How simple! I misunderstood the 'set method' part of the error message. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.