private bool WriteBadValues(WriteValueCollection nodesToWrite)
{
bool success = true;
StatusCodeCollection results;
DiagnosticInfoCollection diagnosticInfos;
RequestHeader requestHeader = new RequestHeader();
requestHeader.ReturnDiagnostics = 0;
try
{
Session.Write(
requestHeader,
nodesToWrite,
out results,
out diagnosticInfos);
}
catch (System.ServiceModel.CommunicationException e)
{
Log("WARNING: Communication error (random data may have resulted in a message that is too large). {0}", e.Message);
return true;
}
catch (System.Xml.XmlException e)
{
Log("WARNING: XML parsing error (random data may have resulted in a message that is too large). {0}", e.Message);
return true;
}
catch (ServiceResultException e)
{
if (e.StatusCode == StatusCodes.BadEncodingLimitsExceeded)
{
Log("WARNING: Communication error (random data may have resulted in a message that is too large). {0}", e.Message);
return true;
}
throw new ServiceResultException(new ServiceResult(e));
}
ClientBase.ValidateResponse(results, nodesToWrite);
ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToWrite);
// check diagnostics.
if (diagnosticInfos != null && diagnosticInfos.Count > 0)
{
Log("Returned non-empty DiagnosticInfos array during Write.");
return false;
}
// check results.
ReadValueIdCollection nodesToRead = new ReadValueIdCollection();
for (int ii = 0; ii < nodesToWrite.Count; ii++)
{
WriteValue request = nodesToWrite[ii];
TestVariable variable = (TestVariable)request.Handle;
// allow access denied even if the node was theorectically writeable.
if (results[ii] == StatusCodes.BadUserAccessDenied)
{
continue;
}
if (results[ii] == StatusCodes.BadNotWritable)
{
Log(
"Write failed when writing a writeable value '{0}'. NodeId = {1}, Value = {2}, StatusCode = {3}",
variable.Variable,
variable.Variable.NodeId,
request.Value.WrappedValue,
results[ii]);
success = false;
break;
}
TypeInfo typeInfo = TypeInfo.IsInstanceOfDataType(
request.Value.Value,
variable.Variable.DataType,
variable.Variable.ValueRank,
Session.NamespaceUris,
Session.TypeTree);
if (typeInfo != null)
{
if (results[ii] != StatusCodes.Good && results[ii] != StatusCodes.BadTypeMismatch && results[ii] != StatusCodes.BadOutOfRange)
{
Log(
"Unexpected error when writing a valid value for a Variable '{0}'. NodeId = {1}, Value = {2}, StatusCode = {3}",
variable.Variable,
variable.Variable.NodeId,
request.Value.WrappedValue,
results[ii]);
success = false;
break;
}
continue;
}
if (results[ii] != StatusCodes.BadTypeMismatch && results[ii] != StatusCodes.BadOutOfRange)
{
Log(
"Unexpected error when writing a bad value for a Variable '{0}'. NodeId = {1}, Value = {2}, StatusCode = {3}",
variable.Variable,
variable.Variable.NodeId,
request.Value.WrappedValue,
results[ii]);
success = false;
break;
}
}
return success;
}