X

Solution to Jira web service getWorklogs method error: Object of type System.Xml.XmlNode[] cannot be stored in an array of this type

When using Jira web service methods that operate on work logs you may get the following error when running your .NET application: Object of type System.Xml.XmlNode[] cannot be stored in an array of this type. In this posting I will show you solution to this problem.

I don’t want to go to deep in details about this problem. I think it’s enough for this posting to mention that this problem is related to one small conflict between .NET web service support and Axis. Of course, Jira team is trying to solve it but until this problem is solved you can use solution provided here.

There is good solution to this problem given by Jira forum user Kostadin. You can find it from Jira forum thread RemoteWorkLog serialization from Soap Service in C#. Solution is simple – you have to use SOAP extension class to replace new class names with old ones that .NET found from WSDL. Here is the code by Kostadin.

public class JiraSoapExtensions : SoapExtension
{
    private Stream _streamIn;
    private Stream _streamOut;
 
    public override void ProcessMessage(SoapMessage message)
    {
        string messageAsString;
        StreamReader reader;
        StreamWriter writer;
 
        switch (message.Stage)
        {
            case SoapMessageStage.BeforeSerialize:
                break;
            case SoapMessageStage.AfterDeserialize:
                break;
            case SoapMessageStage.BeforeDeserialize:
                reader = new StreamReader(_streamOut);
                writer = new StreamWriter(_streamIn);
                messageAsString = reader.ReadToEnd();
                switch (message.MethodInfo.Name)
                {
                    case "getWorklogs":
                    case "addWorklogWithNewRemainingEstimate":
                    case "addWorklogAndAutoAdjustRemainingEstimate":
                    case "addWorklogAndRetainRemainingEstimate":
                        messageAsString = messageAsString
                            .Replace("RemoteWorklogImpl", "RemoteWorklog")
                            .Replace("service", "beans");
                        break;
                }
                writer.Write(messageAsString);
                writer.Flush();
                _streamIn.Position = 0;
                break;
            case SoapMessageStage.AfterSerialize:
                _streamIn.Position = 0;
                reader = new StreamReader(_streamIn);
                writer = new StreamWriter(_streamOut);
                messageAsString = reader.ReadToEnd();
                writer.Write(messageAsString);
                writer.Flush(); break;
        }
    }
 
    public override Stream ChainStream(Stream stream)
    {
        _streamOut = stream;
        _streamIn = new MemoryStream();
        return _streamIn;
    }
 
    public override object GetInitializer(Type type)
    {
        return GetType();
    }
 
    public override object GetInitializer(LogicalMethodInfo info,
        SoapExtensionAttribute attribute)
    {
        return null;
    }
 
    public override void Initialize(object initializer)
    {
    }
}

To get this extension work with Jira web service you have to add the following block to your application configuration file (under system.web section).

<webServices> 
  <soapExtensionTypes> 
   <add type="JiraStudioExperiments.JiraSoapExtensions,JiraStudioExperiments" 
         priority="1"/> 
  </soapExtensionTypes> 
</webServices>

Weird thing is that after successfully using this extension and disabling it everything still works.

Liked this post? Empower your friends by sharing it!
Categories: .NET
Related Post