Beispiel #1
0
        /// <summary>
        /// Creates a UA fault message from an exception.
        /// </summary>
        protected virtual Exception CreateSoapFault(RequestHeader requestHeader, Exception e)
        {
            ServiceFault fault = new ServiceFault();

            fault.ResponseHeader = CreateResponseHeader(requestHeader);

            // specify a code.
            StatusCodeException sce = e as StatusCodeException;

            if (sce != null)
            {
                fault.ResponseHeader.ServiceResult = new StatusCode(sce.Code);
            }
            else
            {
                fault.ResponseHeader.ServiceResult = new StatusCode(StatusCodes.BadUnexpectedError);
            }

            // return the message for the exception (add it to the string table first and store the index in the diagnostics).
            if ((requestHeader.ReturnDiagnostics & (uint)DiagnosticsMasks.ServiceLocalizedText) != 0)
            {
                fault.ResponseHeader.StringTable.Add(e.Message);
                fault.ResponseHeader.ServiceDiagnostics.LocalizedText = fault.ResponseHeader.StringTable.Count - 1;
            }

            // return the stack trace for the exception.
            // this is really handy for debugging but is a security risk and never should be done in a production system.
            if ((requestHeader.ReturnDiagnostics & (uint)DiagnosticsMasks.ServiceAdditionalInfo) != 0)
            {
                fault.ResponseHeader.ServiceDiagnostics.AdditionalInfo = e.StackTrace;
            }

            // construct the fault code and fault reason.
            FaultCode   code   = new FaultCode(StatusCodes.GetBrowseName(fault.ResponseHeader.ServiceResult.Code), Namespaces.OpcUa);
            FaultReason reason = new FaultReason(e.Message);;

            // return the fault.
            return(new FaultException <ServiceFault>(fault, reason, code));
        }
Beispiel #2
0
        /// <summary>
        /// Logs a message.
        /// </summary>
        protected void Log(Exception e, string format, params object[] args)
        {
            if (m_reportMessage != null)
            {
                StringBuilder message = new StringBuilder();

                if (args != null && args.Length > 0)
                {
                    message.AppendFormat(format, args);
                }
                else
                {
                    message.Append(format);
                }

                message.Append("\r\n");

                ServiceResultException sre = e as ServiceResultException;

                if (sre != null)
                {
                    message.AppendFormat(
                        ">>> Exception: StatusCode = {0}, Message = {1}",
                        StatusCodes.GetBrowseName(sre.StatusCode),
                        e.Message);
                }
                else
                {
                    message.AppendFormat(
                        ">>> Exception: Type = {0}, Message = {1}",
                        e.GetType().Name,
                        e.Message);
                }

                m_reportMessage(this, message.ToString(), null);
            }
        }
Beispiel #3
0
        public void WriteDict(Dictionary <string, ushort> valueDict)
        {
            using (var session = Session.Create(_config, new ConfiguredEndpoint(null, _endpointDesc,
                                                                                EndpointConfiguration.Create(_config)), false, "", 60000, null, null).GetAwaiter().GetResult())
            {
                StatusCodeCollection     results = null;
                DiagnosticInfoCollection infos   = null;

                session.Write(null,
                              GetWriteValueCollection(valueDict),
                              out results, out infos);


                if (results.Any(sc => StatusCode.IsNotGood(sc)))
                {
                    var messageBuilder = new StringBuilder();

                    for (int i = 0; i < results.Count; i++)
                    {
                        if (StatusCode.IsNotGood(results[i]))
                        {
                            messageBuilder.AppendLine($"{valueDict.ElementAt(i).Key}: {StatusCodes.GetBrowseName(results[i].Code)}");
                        }
                    }

                    throw new KepServerWriteException(messageBuilder.ToString());
                }
            }
        }