title: WCF Windows service with an API to a windows form client, API return value tells me it could not connect to device. Whats the preffered way?
body: <p>I have a WCF windows service which exposes an API to a windows form application. </p>
  
  <p>API connection to client:</p>
  
  <pre><code>            var serviceType = typeof(Mail2SmsServerApi);
  
              var uri = new Uri("http://localhost:8000/");
              host = new ServiceHost(serviceType, new[] { uri });
  
              var behaviour = new ServiceMetadataBehavior() { HttpGetEnabled = true };
              host.Description.Behaviors.Add(behaviour);
  
              host.AddServiceEndpoint(serviceType, new BasicHttpBinding(), "Hello");
              host.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), "mex");
  
              host.Open();
  </code></pre>
  
  <p>My ServiceContract:</p>
  
  <pre><code>[ServiceContract]
  public class Mail2SmsServerApi
  {
      [OperationContract]
      public string Imei()
      {
          try
          {
              GSMHandler gsm = new GSMHandler();
              return gsm.GetImei();
          }
          catch (Exception ex)
          {
              LogText.Error("API GetImei(), exception: " + ex.ToString());
              return null;
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>My GSMHandler class and method:</p>
  
  <pre><code>    public bool OpenConnection()
      {
  
          modem = new GsmPhone(_comport, _baudrate, _timeout);
  
          if (!comm.IsConnected())
          {
              try
              {
                  modem.Open();
                  return true;
              }
              catch (Exception ex)
              {
                  LogText.Debug("OpenConnection(), exception" + ex.ToString());
                  return false;
              }
          }
          else
          {
              try
              {
                  modem.Close();
                  modem.Open();
                  return true;
              }
              catch (Exception ex)
              {
                  LogText.Debug("OpenConnection(), exception" + ex.ToString());
                  return false;
              }
  
          }
      }
      public string GetImei()
      {
          string imei = "";
          try
          {
              imei = modem.RequestSerialNumber();
              LogText.Debug("IMEI:" + _IMEI);
              return imei;
          }
          catch (Exception ex)
          {
              LogText.Error("Error caught in GetImei(), exception: " + ex.ToString());
              return imei;
          }
      }
  </code></pre>
  
  <p>When the OnStart method in my service are called, I'm opening a connection to the modem with :</p>
  
  <pre><code>                gsm = new GSMHandler();
                  gsm.OpenConnection();
  </code></pre>
  
  <p>When OnStop are called, I'm stopping it with:</p>
  
  <pre><code>                gsm = new GSMHandler();
                  gsm.OpenConnection();
  </code></pre>
  
  <p>My idea was that, with this design the service would handle the communication and the client and service could interact with the modem without getting a com port not open or com port busy problem. This is obviously wrong, since I'm not able to return values from the modem this way. It has to be a design failure from my side. </p>
  
  <p>What I'm trying to achieve is that a client can talk to the modem through the service, and that not both of them make a direct connection to the modem. But that the service can handle the opening and closing of connection to modem, and pass commands in to the modem from the client...</p>
  
  <p>So my question is, what's the appropriate way to design such a scenario? I'm not asking for the code, just how It's usual to design it...</p>
  
  <p>I'm appreciating all answers :) Thanks in advance!</p>