public static void CloseAfariaServiceGroups(GroupsServiceClient svcGroups) { try { if (svcGroups != null) { // Get the state of the service System.ServiceModel.CommunicationState commState = svcGroups.State; // If the service is faulted, we still need to abort if (commState == System.ServiceModel.CommunicationState.Faulted) { svcGroups.Abort(); } // If the state is not already closed or in the process of closing, we should close the context else if (commState != System.ServiceModel.CommunicationState.Closing || commState != System.ServiceModel.CommunicationState.Closed) { // Get the context info to get the context ID, although we saved this in the context string variable Groups.ContextInfo ci = svcGroups.GetContextInfo(); // Assure that there is a valid context ID if (!string.IsNullOrWhiteSpace(ci.ContextId)) { // Close the context svcGroups.CloseContext(); } // Now close the service svcGroups.Close(); } // If the channel is closing/closed, attempt to close out the context, but don't need to close state else { // We will likely throw an exception here. svcGroups.CloseContext(); } svcGroups = null; } } catch (Exception ex) { // Just ouput the exception, proper handling should initiate a new service, initiate the context to the previous, // and then close out the context and service Console.WriteLine(ex.Message.ToString()); logger.Error("Error while closing Device service", ex); } }
public static GroupsServiceClient GetAfariaServiceGroups(Dictionary<string, string> server, Dictionary<string, string> credentials) { string groupsApiAddr = String.Format("net.tcp://{0}:{1}/AfariaService/{2}", server["IPAddr"], server["Port"], "Groups"); GroupsServiceClient svcGroups = new GroupsServiceClient("NetTcpBinding_IGroupsService", groupsApiAddr); svcGroups.ClientCredentials.Windows.ClientCredential.Domain = credentials["Domain"]; svcGroups.ClientCredentials.Windows.ClientCredential.UserName = credentials["UserName"]; svcGroups.ClientCredentials.Windows.ClientCredential.Password = credentials["Password"]; logger.Info("Creating Groups service: {0}", groupsApiAddr); return svcGroups; }