.NET Remoting with Service 使用 DAAB 和 Unity 给出空引用异常

发布于 2024-07-27 00:45:41 字数 3114 浏览 1 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文