我有一个使用Unity的项目,我正在尝试实现.NET Remoting。我有一个NET Remoting Server,用于现有的服务层,它具有使用Enterpise Library数据访问应用程序块的数据层。问题是由于Remoting在调用TestService时需要无参数构造函数 获取空引用异常。关于如何用Unity实现这个的任何想法?
这是我的代码:
服务层的TestService:
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:
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;
}
图书课程:
[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;
}
}
简单测试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客户端:
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();
以下是完整的例外情况:
第40行正在调用repository.GetBooks();
在TestService.cs中的Test.Services.IssueService.GetBooks():第40行 在System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md,Object [] args,Object server,Int32 methodPtr,Boolean fExecuteInContext,Object []&amp; outArgs) 在System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md,Object [] args,Object server,Int32 methodPtr,Boolean fExecuteInContext,Object []&amp; outArgs) 在System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg,Int32 methodPtr,Boolean fExecuteInContext)
谢谢,
约翰