.NET Remoting with Service that uses DAAB with Unity

Topics: Data Access Application Block, General discussion
Jul 9, 2009 at 4:45 PM


I have a project that is using Unity and I am trying to implement Remoting.  I have a Remoting Server for an existing Service Layer that has a Data Layer using the Data Access Application Block. The problem is that due to Remoting requiring a parameterless constructor when the TestService is called I am
getting a null reference exception. Any ideas on how to implement this with Unity?

Here is my code:

TestService from Service Layer:

public class TestService : MarshalByRefObject, ITestService
        private ITestRepository repository;

        public TestService(ITestRepository repository)
            this.repository = repository;        

        public TestService() <--- parameterless constructor

        public List<Book> GetBooks()
            return repository.GetBooks();

TestRepository from Data Layer:

public class TestRepository : ITestRepository
        private Database db;

        public TestRepository(Database db)
            this.db = db;      

        public List<Book> GetBooks()
          List<Book> list = new List<Book>();
            list.Add(new Book(1, "test book 1"));
            list.Add(new Book(2, "test book 2"));
            return list;

Book class:

    public class Book : MarshalByRefObject
        public int ID { get; set; }
        public string Name { get; set; }

        public Book() { }
        public Book(int id, string name)
            this.ID = id;
            this.Name = name;

Simple test RemotingServer:

TestRepository testRepository;
TestService testService;

IUnityContainer container = new UnityContainer();


container.RegisterType<ITestRepository, TestRepository>();

container.RegisterType<ITestService, TestService>();

testService = container.Resolve<TestService>();

TcpServerChannel channel = new TcpServerChannel(9988);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(TestService), "TestService", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Press Any Key");

Simple Remoting Client:

ChannelServices.RegisterChannel(new TcpClientChannel());
            ITestService testService = (ITestService)Activator.GetObject(typeof(ITestService), "tcp://localhost:9988/TestService");
            foreach(Book item in testService.GetBooks())
                Console.WriteLine("Book:\n" + item.Name);    




Jul 10, 2009 at 7:11 AM


I think you get a better answer if you post this in the Unity forums: http://unity.codeplex.com/Thread/List.aspx

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.