| using System.Windows.Forms;
|
| 1:using System.ServiceModel;
|
| 2:namespace HostService
|
| 3:{
|
| 4: public partial class frmHost : Form
|
| 5: {
|
| 6: private ServiceHost host = null;
|
| 7: private string urlMeta, urlService = "";
|
| 8: private List<ServiceHost> objHostList;
|
| 9:
|
| 10: public frmHost()
|
| 11: {
|
| 12: InitializeComponent();
|
| 13: }
|
| 14:
|
| 15:
|
| 16: private enum ServicePort
|
| 17: {
|
| 18: //here we can allocate port for each module.
|
| 19: Module1 = 8020,
|
| 20: Module2 = 8021
|
| 21:
|
| 22: }
|
| 23:
|
| 24: private void btnStartService_Click(object sender, EventArgs e)
|
| 25: {
|
| 26: // this can be pickedup from app.config also
|
| 27: OpenHost(typeof(ServiceAgent.Module1.RoomAgent), typeof(ServiceAgent.Module1.IRoomAgent), ServicePort.Module1);
|
| 28: }
|
| 29:
|
| 30: void OpenHost(Type Service, Type IService, ServicePort portnumber)
|
| 31: {
|
| 32: try
|
| 33: {
|
| 34:
|
| 35:
|
| 36: // Returns a list of ipaddress configuration. use system.net
|
| 37: IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());
|
| 38:
|
| 39: // Select the first entry. I hope it's this maschines IP
|
| 40: IPAddress _ipAddress = ips.AddressList[0];
|
| 41: string sPort = Convert.ToString((int)portnumber);
|
| 42: string sPortName = portnumber.ToString();
|
| 43: string sMetaPort = Convert.ToString(((int)portnumber) + 1);
|
| 44:
|
| 45: // Create the url that is needed to specify where the service should be started
|
| 46: urlService = "net.tcp://" + _ipAddress.ToString() + ":" + sPort + "/" + sPortName;
|
| 47:
|
| 48: NetTcpBinding httpBinding = new NetTcpBinding();
|
| 49:
|
| 50:
|
| 51: host = new ServiceHost(Service);
|
| 52: host.Opening += new EventHandler(hostOpening);
|
| 53: host.Opened += new EventHandler(hostOpened);
|
| 54: host.Closing += new EventHandler(hostClosing);
|
| 55: host.Closed += new EventHandler(hostClosed);
|
| 56: host.AddServiceEndpoint(IService, httpBinding, urlService);
|
| 57:
|
| 58: objHostList.Add(host);
|
| 59:
|
| 60: host.Open();
|
| 61: }
|
| 62: catch (Exception ex)
|
| 63: {
|
| 64: txtStatus.Text += ex.ToString();
|
| 65: }
|
| 66: }
|
| 67:
|
| 68: private void btnStopService_Click(object sender, EventArgs e)
|
| 69: {
|
| 70: host.Close();
|
| 71: }
|
| 72:
|
| 73:
|
| 74: }
|
| 75:}
|
| 76: |