I am a beginner to WCF. I am trying to make a device driver network accessible.
Code I have right now, simplified:
A.cs
public class A {
public int source;
}
Driver.cs
public class Driver {
// some fields here
A a;
// singleton class
private Driver() {
a = null;
// some more code
}
// static methods
public static Driver OpenDevice(int n) {
Driver d = null;
// some code
i = GetBoardNumber();
// some native code to actually open driver ONLY IF it wasn't already opened!
d = new Driver();
return d;
}
public static int GetDeviceNumber() {
// some native code get device number
return someInt;
}
// some non static methods which use native code
// example:
public int ResetDevice(){
// some code
// call native i_ResetDevice() method
return code;
}
}
DllImport.cs
public class DllImport {
// some code to import method definitions from dll
// example:
[DllImport("MyDeviceDriverProvidedForWindows.dll")]
public static extern int i_ResetDevice();
// some more code
}
This Driver works very well for me. In the examples that control the device, I simply add reference to this Driver and control the Device using driver’s methods.
MyService d = Driver.OpenDevice(0);
d.ControlDevice();
My job is to make this Driver remotely accessible, and I chose to do it using WCF.
Since, I already had implementation, I extracted an interface and turned it into a valid WCF service contract by putting OperationContracts and proper places. This extracted interface did not have the OpenDevice and GetDeviceNumber methods because, they were static.
The problems I am having are:
1) Opening WSDL file tells me to use some code like this in client:
MyServiceClient client = new MyServiceClient();
// access client operations
// close client
client.Close();
what is this MyServiceRefernce.MyServiceClient(MyServiceReference is name of the service reference I added to the client)? Why is it calling the private constructor of MyService class on server?
2) How can I do something like this in the client?
MyServiceReference.Driver d = MyServiceRefernce.Driver.OpenDevice(0);
I have read about InstanceContextMode but not really sure how to use it my situation.
I understand it is hard to understand what I am asking exactly, but it really is hard for me to explain too! I wish I had someone with me who knew WCF well.
The actual driver source I am using is in Assembly folder in this file.
You not make a WCF service use a single instance by making the service class a singleton. You need to configure the WCF service so it uses only a single instance. Also you can create a session-based WCF service. See this, for example.