Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Using simple objects in web services

Consumer : A test from a test class, in a project with a web reference.

[Test]
  public void RemoteInfoTest()
  {
   UserServices _userServices = new UserServices();

   RemoteInfo ri = new RemoteInfo();
   Assert.AreEqual(null,ri.Info,"new proxy Info");
   
   ri = _userServices.SetRemoteInfo(ri);
   Assert.AreEqual("Saved",ri.Info);
   
   ri = _userServices.GetRemoteInfo();
   Assert.AreEqual("Default",ri.Info);

   ri = _userServices.SetRemoteInfo(ri);
   Assert.AreEqual("Saved",ri.Info);
  }

 

In the web site of the web service.

using System;

namespace TemplatedSampleSite.aspmessageboard.Classes
{
 /// <summary>
 /// Summary description for RemoteInfo.
 /// </summary>
 public class RemoteInfo
 {
  private string _info;
  public RemoteInfo()
  {
   
  }
  public string Info
  {
   get { return _info; }
   set { _info = value; }
  }
 }
}

And the service code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using TemplatedSampleSite.aspmessageboard.Classes;

namespace TemplatedSampleSite.aspmessageboard.WebServices
{
 /// <summary>
 /// Summary description for UserServices.
 /// </summary>
 public class UserServices : System.Web.Services.WebService
 {
  public UserServices()
  {
   //CODEGEN: This call is required by the ASP.NET Web Services Designer
   InitializeComponent();
  }

  #region Component Designer generated code
  
  //Required by the Web Services Designer
  private IContainer components = null;
    
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  
  #endregion

  // WEB SERVICE EXAMPLE
  // The HelloWorld() example service returns the string Hello World
  // To build, uncomment the following lines then save and build the project
  // To test this web service, press F5

  [WebMethod]
  public RemoteInfo GetRemoteInfo()
  {
   RemoteInfo ri = new RemoteInfo();
   ri.Info = "Default";
   return ri;
  }
  [WebMethod]
  public RemoteInfo SetRemoteInfo(RemoteInfo remoteInfo)
  {
   remoteInfo.Info="Saved";
   return remoteInfo;
  }
 }
}

 

No Comments