访问托管服务的对象

发布于 2024-11-04 18:08:54 字数 1533 浏览 3 评论 0原文

我正在使用 WCF 4 和 C#。我将 InstanceContextMode 设置为 Single。实现我的合约的类的实例是使用构造函数创建的,该构造函数接受托管服务的对象的引用。服务对象在创建和托管服务主机时传递。

服务实现代码:

[ServiceBehavior(UseSynchronizationContext = false, 
     InstanceContextMode = InstanceContextMode.Single)]   
public class ServiceImpl : IMyContract
{
   private ServiceHoster _sh;
   ServiceImpl(ServiceHoster sh)
   {
      _sh = sh;
   }

   public string Call(string input)
   {
      ... //Do some processing on the input string

      return _sh.ProcessCall(string input);
   }
}

ServiceHoster 代码:

public class ServiceHoster
{
    private ServiceImpl ns;

    ServiceHoster()
    {
       ...
       Start();
    }

    private void Start()
    {
       //Host Service
       ns = new ServiceImpl(this);

        //Instantiate NetTCP service
        _tcpServiceHost = new ServiceHost(ns, new Uri("net.tcp://localhost:8089"));
        _tcpServiceHost.Open();
    }

    private void Stop()
    {
      if(ns != null && ns.State == CommunicationState.Opened)
        ns.Close();
    }

    public string ProcessCall(string input)
    {
       ...
       return result;
    }
}

我现在面临的问题是,我们需要客户端和服务器之间进行双工通信。对于双工连接,我们需要将 InstanceContextMode 设置为 PerSession

我的问题是:

  1. 我可以使用多个值吗 InstanceContextMode 怎么样(其中 我觉得不可能)?

  2. ServiceImpl还有其他方法吗 对象来获取引用 托管它的对象的?

  3. 有什么我可以做的吗 以不同的方式解决问题?

谢谢。

I'm using WCF 4 with C#. I'm using InstanceContextMode set to Single. The instance of the class implementing my contract is created using the constructor which accepts the reference of the object which hosts the service. The service object is passed when the service host is being created and hosted.

Service Implementation Code:

[ServiceBehavior(UseSynchronizationContext = false, 
     InstanceContextMode = InstanceContextMode.Single)]   
public class ServiceImpl : IMyContract
{
   private ServiceHoster _sh;
   ServiceImpl(ServiceHoster sh)
   {
      _sh = sh;
   }

   public string Call(string input)
   {
      ... //Do some processing on the input string

      return _sh.ProcessCall(string input);
   }
}

ServiceHoster Code:

public class ServiceHoster
{
    private ServiceImpl ns;

    ServiceHoster()
    {
       ...
       Start();
    }

    private void Start()
    {
       //Host Service
       ns = new ServiceImpl(this);

        //Instantiate NetTCP service
        _tcpServiceHost = new ServiceHost(ns, new Uri("net.tcp://localhost:8089"));
        _tcpServiceHost.Open();
    }

    private void Stop()
    {
      if(ns != null && ns.State == CommunicationState.Opened)
        ns.Close();
    }

    public string ProcessCall(string input)
    {
       ...
       return result;
    }
}

The problem that I'm facing now is, we need duplex communication between client and server. For duplex connection we need to set InstanceContextMode set to PerSession.

My questions are:

  1. Can I use multiple values for
    InstanceContextMode some how (which
    I think is not possible)?

  2. Is there any other way for ServiceImpl
    object to get hold of the reference
    of the object which is hosting it?

  3. Is there anything I can do
    differently to solve the problem?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-11-11 18:08:54

首先,我认为您不需要对 InstanceContextMode 做任何事情。有关如何保留已连接客户端列表的详细信息,请参阅此 MSDN/WCF 论坛主题:http://social.msdn.microsoft.com/forums/en-US/wcf/thread/463dd4a2-f9db-4773-b373-7dd470e65f90/

如果您仍然想这样做,我建议您实现一个实例提供程序(通过实现 IInstanceProvider 实例,并以您选择的行为将其插入 ServiceHost。)

如果您在 Google 上搜索 IInstanceProvider,您会找到有关如何完成此操作的示例 - 如果您如果使用 IoC 容器,您很可能会找到以这种方式工作的 WCF 集成。

First off, I don't think you need to do anything about your InstanceContextMode. See this MSDN/WCF forum thread for more information on how you can keep a list of the connected clients: http://social.msdn.microsoft.com/forums/en-US/wcf/thread/463dd4a2-f9db-4773-b373-7dd470e65f90/

If you still want to go that way, I would suggest that you implement an instance provider (by implementing the IInstanceProvider instance and plug it into the ServiceHost with a behavior of your choice.)

If you Google for IInstanceProvider, you'll find examples on how it is done - if you use an IoC container, you'll most likely find a WCF integration for it that works this way.

宛菡 2024-11-11 18:08:54

获取当前请求的 serviceHost 引用,如下所示:

OperationContext.Current.Host

Get a reference to the serviceHost for the current request like this:

OperationContext.Current.Host
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文