public void Start()
        {
            if (_serviceHost != null)
                throw new InvalidOperationException("_serviceHost already started");

            if (_automationController != null)
                throw new InvalidOperationException("_automationController already created");

            InvokeTrace("building host...");

            // build the service
            var phoneAutomationService = new PhoneAutomationService();
            phoneAutomationService.Trace += (sender, args) => InvokeTrace(args);
            var serviceHost = new ServiceHost(phoneAutomationService, BindingAddress);

            // Enable metadata publishing
            var smb = new ServiceMetadataBehavior
            {
                HttpGetEnabled = true,
                MetadataExporter = { PolicyVersion = PolicyVersion.Policy15 }
            };
            serviceHost.Description.Behaviors.Add(smb);

            // build SOAP ServiceEndpoint
            serviceHost.AddServiceEndpoint(
                                            typeof(IPhoneAutomationService),
                                            GetHttpBinding(),
                                            BindingAddress + "/automate");

            // build JSON ServiceEndpoint
            var jsonServiceEndpoint = serviceHost.AddServiceEndpoint(
                                                        typeof(IPhoneAutomationService),
                                                        GetWebHttpBinding(),
                                                        BindingAddress + "/jsonAutomate");
            var webHttpBehavior = new WebHttpBehavior()
                                      {
                                          DefaultOutgoingRequestFormat = WebMessageFormat.Json,
                                          DefaultOutgoingResponseFormat = WebMessageFormat.Json,
                                          DefaultBodyStyle = WebMessageBodyStyle.Wrapped
                                      };
            jsonServiceEndpoint.Behaviors.Add(webHttpBehavior);

            // open the host
            InvokeTrace("opening host...");
            serviceHost.Open();
            InvokeTrace("host open");

            _automationController = new PhoneAutomationController(phoneAutomationService, AutomationIdentification);
            _serviceHost = serviceHost;
        }
 public void Stop()
 {
     _automationController = null;
     if (_serviceHost != null)
     {
         InvokeTrace("closing host");
         _serviceHost.Close();
         InvokeTrace("host closed");
         _serviceHost = null;
     }
 }