Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# servicestack serverevents由eventhandler/操作触发_C#_Events_Interface_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack_Eventhandler - Fatal编程技术网 servicestack,eventhandler,C#,Events,Interface,servicestack,Eventhandler" /> servicestack,eventhandler,C#,Events,Interface,servicestack,Eventhandler" />

C# servicestack serverevents由eventhandler/操作触发

C# servicestack serverevents由eventhandler/操作触发,c#,events,interface,servicestack,eventhandler,C#,Events,Interface,servicestack,Eventhandler,背景: 我正在为我的应用程序使用ServiceStack框架(4.0.42)。它是自托管的,并作为windows服务运行。主要目的是使某些设备能够通过web服务(RESTful和ServerEvent)进行通信 为此,我为所有类型的设备创建了一个通用接口,如下所示(简化): 这里是AppHost类: public class AppHost : AppSelfHostBase { public IDevice DeviceAdapter; public AppHost()

背景: 我正在为我的应用程序使用ServiceStack框架(4.0.42)。它是自托管的,并作为windows服务运行。主要目的是使某些设备能够通过web服务(RESTful和ServerEvent)进行通信

为此,我为所有类型的设备创建了一个通用接口,如下所示(简化):

这里是AppHost类:

public class AppHost : AppSelfHostBase
{
    public IDevice DeviceAdapter;

    public AppHost()
        : base("grob.MachineConnector.Service", typeof(ServiceInterface).Assembly)
    { }

    public override void Configure(Funq.Container container)
    {            
        this.Plugins.Add(new ServerEventsFeature());

        switch (UsedAdapter)
        {
            case enAdapterTyp.DeviceTyp1:
                DeviceAdapter = new DeviceTyp1();
                break;
            case enAdapterTyp.DeviceTyp2:
                DeviceAdapter = new DeviceTyp2();

                break;
            default:
                throw new AdapterTypException("Wrong or no Adaptertyp is configured:" + UsedAdapter.ToString());
        }            
        container.Register(new ServiceInterface(DeviceAdapter));                       
    }       
也许它就在Funq的某个地方。我不确定DI、IoC和自动布线的具体情况。我试图为这个框架编写自己的插件。我不知道如何在我的设备出现事件时获取IServerEvents的有效实例。 也许我做了一些一般的设计错误。对于OOP和C#我是初学者


任何提示都是非常受欢迎的。

服务依赖项仅在请求的生命周期内可用,超过此时间,服务及其依赖项将被释放/处置。从事件中不清楚它是否仅在请求期间引发:

DeviceAdapter.RaiseSomeEvent += DeviceAdapter_RaiseSomeEvent; 
您也不应该注册服务,因为它们是由ServiceStack自动注册和自动连接的:

 //Don't register Services
 //container.Register(new ServiceInterface(DeviceAdapter));         
否则,
IServEvents
只是一个正常的单例依赖项,当

通常,您只需像访问任何其他依赖项一样访问它,您只需在需要它的依赖项中解析它,即:

container.Register<IDevice>(c => new DeviceTyp1 { 
    ServerEvents = c.Resolve<IServerEvents>()
});
但是,如果此事件不是对服务请求的响应,则该事件不应绑定到服务,也就是说,您可以在AppHost中设置处理程序:

public override void Configure(Funq.Container container)
{            
    DeviceAdapter.RaiseSomeEvent += DeviceAdapter_RaiseSomeEvent;       
}

public void DeviceAdapter_RaiseSomeEvent(object sender, EventArgs e)
{
    var serverEvents = Container.Resolve<IServerEvents>();
    serverEvents.NotifyAll("cmd.Handler", "Something happend!");            
}
public override void Configure(Funq.Container)
{            
DeviceAdapter.RaiseSomeEvent+=DeviceAdapter_RaiseSomeEvent;
}
public void DeviceAdapter_RaiseSomeEvent(对象发送方,事件参数e)
{
var serverEvents=Container.Resolve();
NotifyAll(“cmd.Handler”,“Something happend!”);
}
请参阅,以便您知道应使用哪个选择器发送消息


通过不同的方式查看此答案,您可能也会有所帮助。

hello mythz谢谢您的帮助。您的解释非常具有说明性。在您的示例中,在这一行“var serverEvents=Container.Resolve”中缺少一个>
container.Register<IDevice>(c => new DeviceTyp1 { 
    ServerEvents = c.Resolve<IServerEvents>()
});
public class ServiceInterface : Service
{
    public IDevice DeviceAdapter { get; set; }         

    public object Any(Request request)
    {
        //DeviceAdapter also has access to IServerEvents
        DeviceAdapter.Exec(request); 
    }
}
public override void Configure(Funq.Container container)
{            
    DeviceAdapter.RaiseSomeEvent += DeviceAdapter_RaiseSomeEvent;       
}

public void DeviceAdapter_RaiseSomeEvent(object sender, EventArgs e)
{
    var serverEvents = Container.Resolve<IServerEvents>();
    serverEvents.NotifyAll("cmd.Handler", "Something happend!");            
}