public void NextJob(NbtAuth auth, PrinterSetupData aprinter) { NbtPublicClient NbtClient = _getClient(auth); NextJobEventArgs e = new NextJobEventArgs(); e.printer.PrinterKey = aprinter.PrinterKey; e.printer.PrinterName = aprinter.PrinterName; e.printer.LPCname = aprinter.LPCname; e.auth.AccessId = auth.AccessId; e.auth.UserId = auth.UserId; e.auth.Password = auth.Password; e.auth.baseURL = auth.baseURL; e.auth.useSSL = auth.useSSL; CswNbtLabelJobRequest labelReq = new CswNbtLabelJobRequest(); labelReq.PrinterKey = e.printer.PrinterKey; CswNbtLabelJobResponse Ret; using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { WebOperationContext.Current.OutgoingRequest.Headers.Add("X-NBT-SessionId", auth.sessionId); Ret = NbtClient.LpcGetNextJob(labelReq); } if (Ret.Authentication.AuthenticationStatus == "NonExistentSession") { //the old session has timed out, and we need to authenticate again CswNbtWebServiceSessionCswNbtAuthReturn authAttempt = _Authenticate(auth, e, NbtClient); if (authAttempt.Authentication.AuthenticationStatus == "Authenticated") { NextJob(auth, aprinter); } } else if (Ret.Authentication.AuthenticationStatus == "Authenticated") { if (Ret.Status.Success) { e.Succeeded = true; e.Job = Ret; aprinter.LPCname = Ret.PrinterName; } else { e.Message = "Error calling NextLabelJob web service. "; if (Ret.Status.Errors.Length > 0) { e.Message += Ret.Status.Errors[0].Message; } } if (OnNextJob != null) { OnNextJob(e); } } }//NextJob()
public void LabelById(NbtAuth auth, string labelid, string targetid, PrinterSetupData aprinter) { NbtPublicClient NbtClient = _getClient(auth); LabelByIdEventArgs e = new LabelByIdEventArgs(); e.printer.PrinterName = aprinter.PrinterName; NbtPrintLabelRequestGet nbtLabelget = new NbtPrintLabelRequestGet(); nbtLabelget.LabelId = labelid; nbtLabelget.TargetId = targetid; CswNbtLabelEpl Ret; using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { WebOperationContext.Current.OutgoingRequest.Headers.Add("X-NBT-SessionId", auth.sessionId); Ret = NbtClient.LpcGetLabel(nbtLabelget); } if (Ret.Authentication.AuthenticationStatus == "NonExistentSession") { //the old session has timed out, and we need to authenticate again CswNbtWebServiceSessionCswNbtAuthReturn authAttempt = _Authenticate(auth, e, NbtClient); if (authAttempt.Authentication.AuthenticationStatus == "Authenticated") { LabelById(auth, labelid, targetid, aprinter); } } else if (Ret.Authentication.AuthenticationStatus == "Authenticated") { if (Ret.Status.Success) { if (Ret.Data.Labels.Length < 1) { e.Message = "No labels returned."; } else { e.Succeeded = true; foreach (PrintLabel p in Ret.Data.Labels) { e.LabelData += p.EplText + "\r\n"; } } } else { e.Message += Ret.Status.Errors[0].Message; } if (OnLabelById != null) { OnLabelById(e); } } } // LabelById()
public void Register(NbtAuth auth, PrinterSetupData aprinter) { RegisterEventArgs e = new RegisterEventArgs(); try { NbtPublicClient NbtClient = _getClient(auth); LabelPrinter lblPrn = new LabelPrinter(); lblPrn.LpcName = aprinter.LPCname; lblPrn.Description = aprinter.Description; CswNbtLabelPrinterReg Ret; using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { WebOperationContext.Current.OutgoingRequest.Headers.Add("X-NBT-SessionId", auth.sessionId); Ret = NbtClient.LpcRegister(lblPrn); } if (Ret.Authentication.AuthenticationStatus == "NonExistentSession") { //the old session has timed out, and we need to authenticate again CswNbtWebServiceSessionCswNbtAuthReturn authAttempt = _Authenticate(auth, e, NbtClient); if (authAttempt.Authentication.AuthenticationStatus == "Authenticated") { Register(auth, aprinter); } }//if previous authentication timed out else if (Ret.Authentication.AuthenticationStatus == "Authenticated") { if (Ret.Status.Success) { e.printer.PrinterKey = Ret.PrinterKey; e.printer.Message = "Registered PrinterKey=" + e.printer.PrinterKey; e.printer.Succeeded = true; } else { e.printer.Message = "Printer \"" + aprinter.LPCname + "\" registration failed. "; e.printer.PrinterKey = string.Empty; if (Ret.Status.Errors.Length > 0) { e.printer.Message += Ret.Status.Errors[0].Message; } } if (OnRegisterLpc != null) { OnRegisterLpc(e); } } //else when authentication was successful } //try catch (Exception Error) { e.Message = "Printer registration failed. Please check server settings."; e.printer.Message = "Printer registration failed. Please check server settings."; } } // Register()
public void updateJob(NbtAuth auth, string jobKey, bool success, string errorMsg) { NbtPublicClient NbtClient = _getClient(auth); UpdateJobEventArgs e = new UpdateJobEventArgs(); CswNbtLabelJobUpdateRequest Request = new CswNbtLabelJobUpdateRequest(); Request.JobKey = jobKey; Request.Succeeded = success; Request.ErrorMessage = errorMsg; CswNbtLabelJobUpdateResponse Ret; using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { WebOperationContext.Current.OutgoingRequest.Headers.Add("X-NBT-SessionId", auth.sessionId); Ret = NbtClient.LpcUpdateJob(Request); } if (Ret.Authentication.AuthenticationStatus == "NonExistentSession") { //the old session has timed out, and we need to authenticate again CswNbtWebServiceSessionCswNbtAuthReturn authAttempt = _Authenticate(auth, e, NbtClient); if (authAttempt.Authentication.AuthenticationStatus == "Authenticated") { updateJob(auth, jobKey, success, errorMsg); } } else if (Ret.Authentication.AuthenticationStatus == "Authenticated") { if (Ret.Status.Success) { e.Succeeded = true; } else { e.Message = "Error updating job: "; if (Ret.Status.Errors.Length > 0) { e.Message += Ret.Status.Errors[0].Message; } } if (OnUpdateJob != null) { OnUpdateJob(e); } } } // updateJob()
private CswNbtWebServiceSessionCswNbtAuthReturn _Authenticate(NbtAuth auth, ServiceThreadEventArgs e, NbtPublicClient NbtClient) { CswNbtWebServiceSessionCswNbtAuthReturn ret = null; try { using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { ret = NbtClient.SessionInit(new CswWebSvcSessionAuthenticateDataAuthenticationRequest() { CustomerId = auth.AccessId, UserName = auth.UserId, Password = auth.Password, IsMobile = true, SuppressLog = true }); if (ret.Authentication.AuthenticationStatus == "Authenticated") { auth.sessionId = WebOperationContext.Current.IncomingResponse.Headers["X-NBT-SessionId"]; } else { e.Message += "Authentication error: " + ret.Authentication.AuthenticationStatus; } } } catch (Exception ex) { e.Message += "Authentication error: " + ex.Message; } finally { NbtClient.Close(); } return(ret); } // _Authenticate
/// <summary> /// Starts a new NBT session and performs the action specified. /// </summary> /// <param name="RequestCallback">The delegate to be executed when the authentication attempt succeeds or fails</param> public string PerformAction(SessionCompleteEvent RequestCallback) { string StatusText = ""; NbtPublicClient NbtClient = new NbtPublicClient(); try { string EndpointUrl = _formatUrl(baseURL); useSSL = (EndpointUrl.ToLower().IndexOf("https:") > -1); NbtClient.Endpoint.Address = new EndpointAddress(EndpointUrl); NbtClient.Endpoint.Binding = new WebHttpBinding { AllowCookies = true, Security = new WebHttpSecurity { Mode = useSSL ? WebHttpSecurityMode.Transport : WebHttpSecurityMode.None } }; //create a scope so that we can send the sessionId header using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { WebOperationContext.Current.OutgoingRequest.Headers.Add("X-NBT-SessionId", sessionId); StatusText = RequestCallback(NbtClient); } if (StatusText == "NonExistentSession") {//if the session doesn't exist, make an authentication request to get a new session ID //create a scope so we can extract the session id header using (OperationContextScope Scope = new OperationContextScope(NbtClient.InnerChannel)) { CswNbtWebServiceSessionCswNbtAuthReturn AuthenticationRequest = NbtClient.SessionInit(new CswWebSvcSessionAuthenticateDataAuthenticationRequest { CustomerId = AccessId, UserName = UserId, Password = Password, IsMobile = true, SuppressLog = true }); StatusText = AuthenticationRequest.Authentication.AuthenticationStatus; if (StatusText == "Authenticated") {//if the authentication was successful, re-send the web request sessionId = WebOperationContext.Current.IncomingResponse.Headers["X-NBT-SessionId"]; PerformAction(RequestCallback); } } //operationContextScope } //if the session doesn't exist } //try catch (Exception Ex) { StatusText += Ex.Message; } if (StatusText != "Authenticated") { announceBalanceTimer.Stop(); } NbtClient.Close(); return(StatusText); }//performAction()
/// <summary> /// Attached to Test Connection button on the NBT tab. /// Try to connect to NBT with the values entered, and display result to output box. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void testConnectionButton_Click(object sender, EventArgs e) { //define how to update UI when the authentication attempt resolves NbtAuth.SessionCompleteEvent PrintStatusMessage = delegate(NbtPublicClient Client) { string StatusText = ""; try { CswNbtWebServiceSessionCswNbtAuthReturn AuthenticationRequest = Client.SessionInit(new CswWebSvcSessionAuthenticateDataAuthenticationRequest { CustomerId = _authenticationClient.AccessId, UserName = _authenticationClient.UserId, Password = _authenticationClient.Password, IsMobile = true, SuppressLog = true }); StatusText = AuthenticationRequest.Authentication.AuthenticationStatus; } catch (Exception E) { StatusText += E.Message; } switch (StatusText) { case "Failed": ConnectionResultsOutput.Invoke((Action)(() => { ConnectionResultsOutput.Text += "You have supplied an incorrect username or password for this account.\r\n"; })); break; case "NonExistentAccessId": ConnectionResultsOutput.Invoke((Action)(() => { ConnectionResultsOutput.Text += "The Customer Id you have supplied does not exist in the database.\r\n"; })); break; case "Authenticated": ConnectionResultsOutput.Invoke((Action)(() => { ConnectionResultsOutput.Text += "Connection successful.\r\n"; })); if (false == _authenticationClient.announceBalanceTimer.Enabled) { _authenticationClient.announceBalanceTimer.Start(); } break; case "Object reference not set to an instance of an object.": ConnectionResultsOutput.Invoke((Action)(() => { ConnectionResultsOutput.Text += "Please enter the connection details you use to connect to NBT.\r\n"; })); break; case "Invalid URI: The format of the URI could not be determined.": ConnectionResultsOutput.Invoke((Action)(() => { ConnectionResultsOutput.Text += "Please check the host address for your connection.\r\n"; })); break; case "ShowLicense": ConnectionResultsOutput.Invoke((Action)(() => { ConnectionResultsOutput.Text += "An administrator must accept the license for Cispro Cloud before using the client.\r\n"; })); break; default: ConnectionResultsOutput.Invoke((Action)(() => { ConnectionResultsOutput.Text += "Connection Error: " + StatusText + "\r\n"; })); break; } //switch ( StatusText ) return(StatusText); }; //SessionCompleteEvent PrintStatusMessage //Perform a test connection asynchronously, using the managed thread pool ConnectionResultsOutput.Text += "Attempting to connect...\r\n"; BackgroundWorker NbtTask = new BackgroundWorker(); NbtTask.DoWork += _authenticationClient.PerformActionAsync; NbtTask.RunWorkerAsync(PrintStatusMessage); }//testConnectionButton_Click( object sender, EventArgs e )