August 2004 - Posts

FotoVision C# Source Code

Here is the C# version of the FotoVision Web Application I converted from the VB.NET version released by Microsoft. I have two downloads available, one with just the source, and one containing a working sample (.NET 1.1). The source version has NAnt scripts to build it for Mono and .NET 1.1. You will need the latest version (.85) of NAnt to use the NAnt scripts.

Compiled Sample

Source Code

IKVM - Using ActiveMQ JMS from C#

Wishing to have a publish/subscribe message queue available in .NET, I have looking for ways to allow .NET to talk to a JMS implementation. After reading the article An Introduction to IKVM by Avik Sengupta, I took the latest release of ActiveMQ and IKVM, complied the jar files (as described in the article), and modified one of the examples from the ActiveMQ distribution and created a C# version (see below). Everything seems to work. The example submits items to the queue, and them retrieves them.
using System;
using System.Threading;
using ikvm.lang;
using org.codehaus.activemq;
using org.codehaus.activemq.util;
using org.codehaus.activemq.message;
using javax.jms;
using java.util;


namespace DefaultNamespace
{
    class MainClass : MessageListener
    {
        protected int messageCount = 100;
        protected String[] data;
        protected ActiveMQConnectionFactory connectionFactory;
        protected Session session;
        protected MessageConsumer consumer;
        protected MessageProducer producer;
        protected Destination destination;
        protected Connection connection;
        
        protected Connection receiveConnection;
        protected Session receiveSession;
        
        protected List messages = Collections.synchronizedList(new ArrayList());
        protected bool topic = true;
       
        protected ActiveMQMessage createMessage() 
        {
            return new ActiveMQMessage();
        }

        protected Destination createDestination(String subject) 
        {
            return new ActiveMQTopic(subject);
        }

        public void testSendReceive() 
        {
            messages.clear();
        
            for (int i = 0; i < data.Length; i++) 
            {
                Message message = session.createTextMessage(data[i]);
        
                Console.WriteLine("About to send a message: " + message + " with text: " + data[i]);
        
                producer.send(destination, message);
            }
        
            // lets wait a little while
            Thread.Sleep(4000);
        
            Console.WriteLine("should have received a message: {0} {1} {2}", messages, data.Length, messages.size());
        
            for (int i = 0; i < data.Length; i++) 
            {
                TextMessage received = (TextMessage) messages.get(i);
                String text = received.getText();
        
                Console.WriteLine("Received Text: " + text);
        
            }
        }
    
    
        public MainClass() 
        {
            topic = true;
            
            data = new String[messageCount];
            for (int i = 0; i < messageCount; i++) 
            {
                data[i] = "Text for message: " + i + " at " + DateTime.Now.ToLongTimeString();
            }
            
            connectionFactory = new ActiveMQConnectionFactory();
            connectionFactory.setBrokerURL("vm://localhost");
            connectionFactory.setUseEmbeddedBroker(true);
            
            connection = connectionFactory.createConnection();
            receiveConnection = connectionFactory.createConnection();
    
            Console.WriteLine("Created connection: " + connection);
    
            session = connection.createSession(false, 1);
            receiveSession = receiveConnection.createSession(false, 1);

            Console.WriteLine("Created session: " + session);
            producer = session.createProducer(null);
    
            Console.WriteLine("Created producer: " + producer);
    
            if (topic) 
            {
                destination = receiveSession.createTopic("FOO.BAR");
            }
            else 
            {
                destination = session.createQueue("FOO.QUEUE");
            }
    
            consumer = receiveSession.createConsumer(destination);
            consumer.setMessageListener(this);
            receiveConnection.start();
                connection.start();
    
            Console.WriteLine("Created connection: " + connection);
            
            testSendReceive();
            
            Console.WriteLine("Dumping stats...");
            connectionFactory.getFactoryStats().dump(new IndentPrinter());
    
            Console.WriteLine("Closing down connection");
    
            session.close();
            receiveSession.close();
    
            connection.close();
            receiveConnection.close();


            connectionFactory.stop();
            connection.close();
            
        }
    
    
        public void onMessage(Message message) 
        {
            Console.WriteLine("Received message: " + message);
    
            messages.add(message);            
        }
        
        public static void Main(string[] args)
        {
            MainClass mc = new MainClass();
        }
    }
}

FotoVision in C#

One of the things I have been working on in my spare time is converting the FotoVision web application to C#. I was looking for a simple ASP.NET photo gallery that would run on my Linux web server under Mono. The other requirement was that it have a Windows client. I looked into nGallery, but currently it does not have a windows client.

To do the "heavy lifting" of converting the project from VB.NET to C#, I utilized Instant C# Snippet Edition. The only thing I needed to do was make a few minor changes to the aspx/asmx files to convert them to C#.

I am doing some code cleanup at the moment, but if anyone is interested, I can make the code available after I have finished.

Web Service Encryption Options

I have a web service and client that I need to encrypt some of the messages utilizing TripleDES. I currently investigating WSE2 as a solution. Does anyone know of any good examples using WSE2 and symetrical encryption? Are there other options?

Update: One option I have found is to use a SoapExtension to encrypt/decrypt the data. Here is a link to an article by Rob Howard describing the process.

Mono Project 1.0.1 Released

Mono Project is an Open Source implementation of the various ECMA and .NET framework technologies for Unix, MacOS X, and Windows. This version, 1.0.1, is a bugfix release.

How do you create an audit trail of changes made to an object

I have been working on a C# WinForms project which has a requirement of logging to a database, all of the changes made to an object/form to allow the users to produce a report on who/when/what was done within the application.

My question is... What is the best way to do this?

Currently, the way it has been implemented (by another engineer) is, the original data (before any updates) is kept within the object itself as a member variable and then compared with the current data when the object  is saved, logging the differences in each member to the database.

Here is a basic example of how the current implementation works.

1public class MySample
2{
3   public int var1;
4   public string var2;
5 
6   private MySample original;
7 
8   public void Read()
9   {
10     // Open database and populate fields
11 
12     original = new MySample();
13     original.ReadFrom( this );
14   }
15 
16   public void ReadFrom( MySample source )
17   {
18     var1 = source.var1;
19     var2 = source.var2;
20   }
21 
22   public void Write()
23   {
24     if ( original.var1 != var1 )
25     {
26        // Add to list of changed fields
27     }
28 
29     if ( ! original.var2.Equals( var2 ) )
30     {
31       // Add to list of changed fields
32     }
33
34     // Write change log to DB
35 
36     // Update original
37     original.ReadFrom( this );
38 
39     // Write fields to DB (excluding original field)
40   }
41}

This code just feels wrong to me. My first thought to fix this is to implement something utilizing reflection. I am looking for other people's experiences to help me come up with a better way.

The other thing that seems odd to me is, how is it possible to have an instance of MyObject inside itself?

Update: I have updated the sample code to be correct. That is what you get when trying to write something from memory late at night.

First Post

Welcome to my blog, Learning .NET. I will be using this blog to talk about some of the things I have learned and are still learning about developing with .NET using C#.

My name is Christopher Steen, and I am a Lead Software Engineer for company producing solutions for the hospitality and retail industries. 

More to come. Thanks for listening.