Wednesday, January 28, 2009

How to Use the ObjectArray Class with a Web Service

I would think that it’s obvious how this class should be used, but there’s always a chance that the person reading this article is new to .NET and could use an explanation.

Save the class to a folder in your project and change the Namespace of the class to match the location in your project.

Let’s say that the Web Service provided you with an array of “Persons” ( Person [ ] ). Create a new Person Array:

Person[] personList;

Call the Web Service method that returns the Person Array:

personList = Course.GetPersonList();

Instantiate the downloaded “Object Array” class with the personList:

ObjectArray objectArray = new ObjectArray(personList);

Create a DataSet and convert your ObjectArray to a DataSet using the method:

DataSet ds = new DataSet();
ds = objectArray.ToDataSet();

Object Array to DataSet

I’m working with a Web Service right now that returns an array of objects. What I want to do, is bind the Object Array to a DataGrid. I was able to convert the Object Array to a DataSet and then bind it to the DataGrid.

Code Snippet

using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


namespace BusinessObject.Util
{
///
/// Utility Class for Object Arrays.
///

public class ObjectArray
{
private Object[] _objectArray;

public ObjectArray(Object[] objectArray)
{
this._objectArray = objectArray;
}

public DataSet ToDataSet()
{
DataSet ds = new DataSet();

XmlSerializer xmlSerializer =
new XmlSerializer(_objectArray.GetType());
StringWriter writer = new StringWriter();
xmlSerializer.Serialize(writer, _objectArray);
StringReader reader =
new StringReader(writer.ToString());

ds.ReadXml(reader);
return ds;
}
}
}

Replacing Old Classic ASP COM Componenets With .NET Assemblies

Why? Because this change allows you to update your COM components on the fly, without reseting and sometimes rebooting you web server to get it to unlock the .dll...

All you need to do is open your COM component in VS.NET... It does most of the work for you.. You should end up with a nice simple class and namespace like

namespace MyWebDLL
{
///


/// Summary description for Hello.
///
public class Hello
{
public string Echo(string name){return name;}
}
}

Then you can set up your asp page to invoke your DLL by using

Dim foo
Set foo = Server.CreateObject("MyWebDLL.Hello")
Response.Write foo.Echo("blah")

In order for ASP to be able to create the object you are going to have to register your assembly... You have two options. According to Microsoft, the preferred method it to generate a strong name and put the assembly in the GAC, which assures that no-one else can make a DLL with the same name as yours and impersonate your class... To do this:

1. Generate a key pair with sn -k MyWebDLL.snk

2. Add the assembly attribute to the assembly

3. Rebuild the assembly

4. Install it into the GAC with gacutil /i MyWebDLL.dll

5. Register it on the machine with regasm /tlb MyWebDLL.dll

I personally do not like to use the GAC for my web apps... I just use the regasm utility with the /codebase switch and ignore the warning that my assembly should be strongly named. (regasm MyWebDLL.dll /tlb /codebase)


WordBreaker in C#

public static string WordBreaker(string strWord, int breakingLength)
{
int len = strWord.Length;
if (len < breakingLength)
{
return strWord;
}

string output = string.Empty;
for (int count = 0; count < len; count += breakingLength)
{
// if(len/charCount+50)
int rem = len / (count + breakingLength);
if (rem != 0)
{
output += strWord.Substring(count, breakingLength) + "
";
}
else
{
output += strWord.Substring(count, (len - count)) + " ";
}
}
return output;
}