I have a project that is using Unity and I am trying to implement NET Remoting. I have a NET Remoting Server for an existing Service Layer that has a Data Layer using the Enterpise Library 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:
[Serializable]
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.AddNewExtension<EnterpriseLibraryCoreExtension>();
container.AddNewExtension<DataAccessBlockExtension>();
container.RegisterType<ITestRepository, TestRepository>();
container.RegisterType<ITestService, TestService>();
testService = container.Resolve<TestService>();
TcpServerChannel channel = new TcpServerChannel(9988);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(TestService), "TestService", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Press Any Key");
System.Console.ReadKey();
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);
}
Console.ReadKey();
Here is the full exception:
line 40 is calling repository.GetBooks();
at Test.Services.IssueService.GetBooks() in TestService.cs:line 40 at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)
Thanks,
John