.NET Remoting with Service 使用 DAAB 和 Unity 给出空引用异常
我有一个使用 Unity 的项目,我正在尝试实现 NET Remoting。 我有一个用于现有服务层的 NET 远程服务器,该服务层具有使用企业库数据访问应用程序块的数据层。 问题是,由于调用 TestService 时 Remoting 需要无参数构造函数,所以我 得到一个空引用异常。 关于如何使用 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;
}
Book 类:
[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();
简单远程客户端:
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();
这是完整的异常:
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)
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 Enterprise 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论