WebServiceObjectParameter
It is important to note the "incorrect" behavior of IsDirty, the internal logi of the class is not included in the consumer's proxy.
The service class:
using System;
using System.Collections;
using
System.ComponentModel;
using System.Data;
using
System.Diagnostics;
using System.Web;
using
System.Web.Services;
namespace WebServiceObjectParameter
{
///
<summary>
/// Summary description for
Service1.
/// </summary>
public class
ProfileService : System.Web.Services.WebService
{
public
ProfileService()
{
//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
[WebMethod]
public RemoteProfile
Save(RemoteProfile profile)
{
RemoteProfile
savedProfile = new
RemoteProfile(profile.UserId,profile.FirstName);
return
savedProfile;
}
[WebMethod]
public
RemoteProfile GetProfile(string userId)
{
if(userId
== "Me")
{
return new
RemoteProfile(userId,"Andrew");
}
else
{
return
new RemoteProfile(userId,"Guest:" + userId);
}
}
}
}
The consumer web form (all I added was a web reference to the service):
using System;
using System.Drawing;
using
System.Collections;
using System.ComponentModel;
using
System.Windows.Forms;
using System.Data;
//using WebServiceObjectParameter;
namespace WebServiceConsumer
{
///
<summary>
/// Summary description for Form1.
///
</summary>
public class Form1 :
System.Windows.Forms.Form
{
private
System.Windows.Forms.TextBox UserId;
private
System.Windows.Forms.TextBox FirstName;
private
System.Windows.Forms.Button Lookup;
private
System.Windows.Forms.TextBox IsDirty;
private
System.Windows.Forms.Button UpdateButton;
///
<summary>
/// Required designer variable.
///
</summary>
private
System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for
Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after
InitializeComponent call
//
}
/// <summary>
/// Clean up any resources
being used.
/// </summary>
protected
override void Dispose( bool disposing )
{
if(
disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(
disposing );
}
#region Windows Form Designer generated code
///
<summary>
/// Required method for Designer
support - do not modify
/// the contents of this
method with the code editor.
/// </summary>
private
void InitializeComponent()
{
this.Lookup =
new System.Windows.Forms.Button();
this.UserId = new
System.Windows.Forms.TextBox();
this.FirstName = new
System.Windows.Forms.TextBox();
this.IsDirty = new
System.Windows.Forms.TextBox();
this.UpdateButton =
new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Lookup
//
this.Lookup.Location
= new System.Drawing.Point(16, 16);
this.Lookup.Name
= "Lookup";
this.Lookup.TabIndex = 0;
this.Lookup.Text
= "Lookup";
this.Lookup.Click += new
System.EventHandler(this.Lookup_Click);
//
// UserId
//
this.UserId.Location
= new System.Drawing.Point(16, 48);
this.UserId.Name
= "UserId";
this.UserId.TabIndex = 1;
this.UserId.Text
= "";
//
// FirstName
//
this.FirstName.Location = new
System.Drawing.Point(16, 72);
this.FirstName.Name =
"FirstName";
this.FirstName.TabIndex = 2;
this.FirstName.Text
= "";
//
// IsDirty
//
this.IsDirty.Location = new
System.Drawing.Point(16, 96);
this.IsDirty.Name =
"IsDirty";
this.IsDirty.TabIndex = 3;
this.IsDirty.Text
= "";
//
// UpdateButton
//
this.UpdateButton.Location = new
System.Drawing.Point(104, 16);
this.UpdateButton.Name
= "UpdateButton";
this.UpdateButton.TabIndex = 4;
this.UpdateButton.Text
= "Update";
this.UpdateButton.Click += new
System.EventHandler(this.Update_Click);
//
// Form1
//
this.AutoScaleBaseSize
= new System.Drawing.Size(5, 13);
this.ClientSize =
new System.Drawing.Size(292, 266);
this.Controls.AddRange(new
System.Windows.Forms.Control[] {
this.UpdateButton,
this.IsDirty,
this.FirstName,
this.UserId,
this.Lookup});
this.Name = "Form1";
this.Text
= "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for
the application.
/// </summary>
[STAThread]
static
void Main()
{
Application.Run(new
Form1());
}
private void
Lookup_Click(object sender, System.EventArgs e)
{
WebServiceObjectParameter.ProfileService
profileService = new
WebServiceObjectParameter.ProfileService();
WebServiceObjectParameter.RemoteProfile
profile = profileService.GetProfile(UserId.Text);
FirstName.Text = profile.FirstName;
IsDirty.Text
= profile.IsDirty.ToString();
}
private void Update_Click(object sender, System.EventArgs
e)
{
IsDirty.Text = "";
WebServiceObjectParameter.ProfileService
profileService = new
WebServiceObjectParameter.ProfileService();
WebServiceObjectParameter.RemoteProfile
profile =
profileService.GetProfile(UserId.Text);
profile.FirstName
= FirstName.Text;
MessageBox.Show( profile.IsDirty.ToString());
profile
= profileService.Save(profile);
FirstName.Text = profile.FirstName;
IsDirty.Text
= profile.IsDirty.ToString();
}
}
}
The "real" RemoteProfile class is on the service provider end.
using System;
namespace WebServiceObjectParameter
{
public
class RemoteProfile
{
public RemoteProfile()
{
}
public
RemoteProfile(string userId,string firstName)
{
_userId
= userId;
_firstName = firstName;
_isDirty =
false;
}
private string _userId;
private
string _firstName;
private bool _isDirty = false;
public
string UserId
{
get
{
return
_userId;
}
set
{
if(value!=_userId)
{
_isDirty
= true;
}
_userId = value;
}
}
public
string FirstName
{
get
{
return
_firstName;
}
set
{
if(value!=_firstName)
{
_isDirty
= true;
}
_firstName= value;
}
}
public
bool IsDirty
{
get
{
return
_isDirty;
}
// set
// {
// _isDirty
= value;
// }
}
}
}