|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe new .NET technologies, Remoting and Web Services has made life much easier than the days of trying to get DCOM to work. Although with anything that has been made easier there are some details that have been made too easy. In the case of Remoting or calling a web service, the Microsoft .NET Framework includes an automatic feature that converts all returned BackgroundThe problem seems to only occur whenever you send a Using the code1. In the web service we first need to convert the using System.Data;
using System.IO;
using System.Web.Services;
...
namespace NYDataServices
{
...
// Web service is running in New York City
public class MyWebService : System.Web.Services.WebService
{
...
[WebMethod]
public string GetData()
{
DataTable dataTable = null;
// Get data from database as a DataTable
...
// Now convert the DataTable to an xml string and return it to client
return convertDataTableToString( dataTable );
}
private string convertDataTableToString( DataTable dataTable )
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add( dataTable );
StringWriter writer = new StringWriter();
dataSet.WriteXml( writer, XmlWriteMode.WriteSchema );
return writer.ToString();
}
2. On the client side we make the call to get the data and receive the data as an xml string. using System.Data;
using System.Text.RegularExpressions;
...
namespace SeattleClient
{
...
// Client program running in Seattle
public class MyClient : System.Windows.Form
{
...
public void GetDataFromServer()
{
NYDataServices.MyWebService ws = new NYDataServices.MyWebService();
string xmlString = ws.GetData();
DataTable dataTable = convertStringToDataTable( xmlString );
// Do something with dataTable
...
}
3. Converting the xml string back to a private DataTable convertStringToDataTable( string xmlString )
{
// Search for datetime values of the format
// --> 2004-08-22T00:00:00.0000000-05:00
string rp = @"(?<DATE>\d{4}-\d{2}-\d{2})(?<TIME>T\d{2}:\d{2}:\d{2}."+
"\d{7}-)(?<HOUR>\d{2})(?<LAST>:\d{2})";
// Replace UTC offset value
string fixedString = Regex.Replace( xmlString, rp,
new MatchEvaluator( getHourOffset ) );
DataSet dataSet = new DataSet();
StringReader stringReader = new StringReader( fixedString );
dataSet.ReadXml( stringReader );
return dataSet.Tables[ 0 ];
}
private static string getHourOffset( Match m )
{
// Need to also account for Daylights Savings
// Time when calculating UTC offset value
DateTime dtLocal = DateTime.Parse( m.Result( "${date}" ) );
DateTime dtUTC = dtLocal.ToUniversalTime();
int hourLocalOffset = dtUTC.Hour - dtLocal.Hour;
int hourServer = int.Parse( m.Result( "${hour}" ) );
string newHour = ( hourServer + ( hourLocalOffset -
hourServer ) ).ToString( "0#" );
string retString = m.Result( "${date}" + "${time}" +
newHour + "${last}" );
return retString;
}
Points of InterestI know this problem happens when sending back DataTables. I'm not sure if the same applies to custom classes, although I suspect it does. Here are links that I found very useful --
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||