WCF事件在服务器端 [英] WCF events in server-side

查看:109
本文介绍了WCF事件在服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在WCF应用程序,并要接收的事件在服务器端。

I'm working on an application in WCF and want to receive events in the server side.

我有一个网络的要求需要注册的指纹。 网页请求的设备的连接,然后每秒15秒请求应答

I have a web that upon request needs to register a fingerprint. The web page request the connection of the device and then every second for 15 seconds requests the answer.

服务器端code显然是简单,但不起作用。

The server-side code is apparently "simple" but doesn't work.

下面是:

[ServiceContract]
interface IEEtest
{
    [OperationContract]
    void EEDirectConnect();
}

class EETest : IEEtest
{
    public void EEDirectConnect()
    {
        CZ ee = new CZ(); // initiates the device dll

        ee.Connect_Net("192.168.1.200", 4011);

        ee.OnFinger += new _IEEEvents_OnFingerEventHandler(ee_OnFinger);

    }

    public void ee_OnFinger()
    {
        //here i have a breakpoint;
    }
}

每一次我把我的手指的时候,就应该触发事件。事实上,如果我

every time I put my finger, it should fire the event. in fact if I

static void Main() 
{
    EETest pp = new EETest();
    pp.EEDirectConnect();
}

它工作正常。但是从我的代理不会触发事件。

It works fine. but from my proxy it doesn't fire the event.

你有任何提示,建议,或者你可以看到错误?

do you have any tips, recommendations, or can you see the error?

谢谢大家。

推荐答案

我可以看到两个问题你code,这可能是也可能不是问题。

I can see two issues with your code, which may or may not be the problem.

A)活动报名竞争情况

A) Event Registration Race Condition

您拨打CZ.Connect_Net(),然后您注册的事件处理程序。因此,如果调用Connect_Net(),你注册一个方法来处理事件之间的事件触发,那么你就看不到它。先注册事件处理程序,然后调用Connect_Net()。

You call CZ.Connect_Net() and THEN you register with the event handler. So if your event fires between calling Connect_Net() and you registering a method to handle the event then you'll not see it. Register the event handler first and then call Connect_Net().

B)EEtest一生。

B) EEtest lifetime.

您EEtest类的寿命取决于你在WPF中使用实例化模式,参见<一href="http://mkdot.net/blogs/dejan/archive/2008/04/29/wcf-service-behaviors-instance-and-concurrency-management.aspx" rel="nofollow">http://mkdot.net/blogs/dejan/archive/2008/04/29/wcf-service-behaviors-instance-and-concurrency-management.aspx.通常默认为按呼叫,这意味着EEtest的新实例被创建只是为了服务该呼叫到EEDirectConnect。所以,当你调用方法EEDirectConnect你会得到这样的:

The life time of your EEtest class depends on the Instancing Mode you use in WPF, see http://mkdot.net/blogs/dejan/archive/2008/04/29/wcf-service-behaviors-instance-and-concurrency-management.aspx. Generally the default is Per-Call which means a new instance of EEtest is created just to service the call to EEDirectConnect. So when you invoke the method EEDirectConnect you get this:

  1. EEDirectConnect调用启动。
  2. WCF使得新EEtest类。
  3. 在WCF调用上EEtest的方法。
  4. 在该方法的新闻了CZ并调用连接网的方法。
  5. 在该事件处理程序连接。
  6. 的方法EEDirectConnect完成。
  7. EEtest现在是无根通过WCF - 这是符合GC,因此CZ可享获GC

因此​​,也许它需要一个很短的时间(或它的同步),问题是A,还是它是异步的,它需要一点点的时间,它的B点。

So perhaps it takes a very short time (or it's synchronous) and the problem is A, or it's asynchronous and it takes a little bit longer and it's B.

BTW:要解决b。您可以使用(如事件)某种同步机制来阻止,直到ee_Onfinger火灾

BTW: To fix B you could use some sort of synchronisation mechanism (eg an Event) to block until ee_Onfinger fires.

这篇关于WCF事件在服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆