c# - ServiceHandle is 0 -
i'm trying write simple wcf service self hosted in windows service servicehandle of windows service 0
i need detect hardware change using registerdevicenotification 1 of it's parameters handle, in case servicehandle
public partial class myservice : servicebase, imyservice { private servicehost host; public static void main() { servicebase.run(new myservice()); } public myservice() { initializecomponent(); } protected override void onstart(string[] args) { try { host = new servicehost(typeof(myservice), new uri(@"net.pipe://localhost/myservice")); host.open(); } catch (exception e) { eventlog.writeentry("myservice:", e.message); } } protected override void onstop() { host.close(); } #region imyservice members public void register() { //here servicehost 0 } #endregion
}
what can cause problem? thanks
the servicehandle
- no matter value - not required host wcf service windows service. instantiate servicehost
in onstart
, close in onstop
, should fine.
this why servicehandle
0 in case:
your windows service class implements wcf service contract. not thing , cause servicehandle
property being 0. every call wcf service new instance of myservice
class instantiated (if didn't change defaults). instance normal instance of class doesn't know it's windows service, windows service related properties have default values. instance that's created windows service manager has appropriate properties set.
you can try yourself: in onstart
, insert following line , inspect value myservicevar.servicehandle
. you'll see 0
:
myservice myservicevar = new myservice();
what really want following: have different class implement service contract, example that:
public class mywcfservice : imyservice { public static intptr servicehandle; public void register() { // use mywcfservice.servicehandle here } }
in onstart
method, set servicehandle
variable of mywcfservice
:
protected override void onstart(string[] args) { try { mywcfservice.servicehandle = this.servicehandle; host = new servicehost(typeof(mywcfservice), new uri(@"net.pipe://localhost/myservice")); host.open(); } catch (exception e) { eventlog.writeentry("mywcfservice:", e.message); } }
Comments
Post a Comment