Reputation: 1
I'm attempting to use a WSDL for a service I'm not able to control/modify. Using VS2022 I added a connected service to the WSDL via Add Connected service.
I hope someone can review this and provide me a better answer than I'm getting from vendor. Vendor states "cannot provide coding assistance" which I understand why.
The issue here is caused by a change from previous versions which now requires an array of values verses a single value.
The type of array is defined as object[] Items; which is expected to be either an array of string or array of Guid. The array of Guid is preferred per the vendor. This is the problem the array of Guid.
I get either a compiler error of "Cannot implicitly convert type 'System.Guid[]' to 'object[]'" or "Cannot convert type 'System,Guid[]' to object[]'" depending on using a cast or not.
After assigning correct type (object[]) I get runtime error "Failed (CancelIndiciumRequest): Call Error: mscorlib: There was an error in serializing body of message CancelIndiciumRequest: 'There was an error generating the XML document.'. Please see InnerException for more details. ==> System.Xml: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Guid may not be used in this context."
main called method is:
internal int CancelLabel(string trackNum)
{
CancelIndiciumRequest req = new CancelIndiciumRequest
{
Item = GetCredsType(),
//Isue assignment begins:
//Items = (object[]) new Guid[] { new Guid(trackNum) }, <= Syntax error(s)
//These get runtime error
//Items = (new List<Guid> { new Guid(trackNum) }).ToArray(),
//Items = GetArrayGuid(trackNum),
//Items = GetGuids(trackNum).ToArray();
//End issue
};
...removed excess code
resp = GetResponse(req);
}
Support methods:
private List<Guid> GetGuids(string TxID)
{
return new List<Guid> { new Guid(TxID) };
}
private object[] GetArrayGuid(string sGuid)
{
object[] oAry = new object[1];
oAry[0] = new Guid(sGuid);
return oAry;
}
My Called code is:
private CancelIndiciumResponse GetResponse(CancelIndiciumRequest req)
{
XSoap x = sFactory.CreateChannel();
CancelIndiciumResponse resp = null;
int retry = 0;
while (resp == null && retry < 1)
{
try
{
resp = x.CancelIndicium(req); ,=== this is the call into Connected service genned code
_authToken = resp.Authenticator;
}
catch (FaultException ex)
{
...Error handling here
} // catch (FaultException ex)
catch (Exception ex)
{
...Error handling here
}
retry++;
} //while (resp == null && retry < 2)
return resp;
}
The Connected service code is(removed refs to actual url and namespace):
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName="CancelIndicium", WrapperNamespace="http://x.com/xml/namespace/2023/07/x/x", IsWrapped=true)]
public partial class CancelIndiciumRequest {
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://x.com/xml/namespace/2023/07/x/x", Order=0)]
[System.Xml.Serialization.XmlElementAttribute("Authenticator", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Credentials", typeof(Credentials))]
public object Item;
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://x.com/xml/namespace/2023/07/x/x", Order=1)]
[System.Xml.Serialization.XmlElementAttribute("xTxIDs", typeof(System.Guid[]))]
[System.Xml.Serialization.XmlElementAttribute("xNumbers", typeof(string[]))]
public object[] Items;
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://x.com/xml/namespace/2023/07/x/x", Order=2)]
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public System.Nullable<bool> SendEmail;
public CancelIndiciumRequest() {
}
public CancelIndiciumRequest(object Item, object[] Items, System.Nullable<bool> SendEmail) {
this.Item = Item;
this.Items = Items;
this.SendEmail = SendEmail;
}
}
I've not had any luck in finding a fix. AS I dug deeper this is the complete exception using exception.tostring();
System.ServiceModel.CommunicationException: There was an error in serializing body of message CancelIndiciumRequest: 'There was an error generating the XML document.'. Please see InnerException for more details. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Guid may not be used in this context.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSwsimV135Soap.Write269_CancelIndicium(Object[] p)
at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer72.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object returnValue, Object[] parameters)
at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, Object[] parameters, Object returnValue, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.BodyWriterMessage.OnWriteBodyContents(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota)
at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
at System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(Message message, Boolean shouldRecycleBuffer)
at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at USPostal.SWSIM.SwsimV135Soap.CancelIndicium(CancelIndiciumRequest request)
at USPostal.StampsApiInterface.GetResponse(CancelIndiciumRequest req) in C:\Users\lawre\Source\Repos\USPostalAPI\USPostal\InterfaceClasses\StampsApiInterface.cs:line 1954
Upvotes: 0
Views: 68
Reputation: 1
I've been digging around and really found nothing more than I posted about trying.
Since the code for the connected service is genned by a code gen engine I thought about how I could maybe extend the code and make this work. I'm a real newbie in this area.
From top of Refernce.cs:
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
Here's what I did:
I placed the method which was in a class, I wanted into another file. It was already a partial class and the parms I wanted to change should be a valid change. but the compiler attributes prevented that from working as I hoped.
I placed the Class I needed into the same name space but in a different file. See above code for the identified class. I changed the code as needed basically replacing object[] with Guid[].
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName = "CancelIndicium", WrapperNamespace = "http://x.com/xml/namespace/2023/07/x/xV135", IsWrapped = true)]
public partial class CancelIndiciumRequest
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://x.com/xml/namespace/2023/07/x/xV135", Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("Authenticator", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Credentials", typeof(Credentials))]
public object Item;
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://x.com/xml/namespace/2023/07/x/xV135", Order = 1)]
[System.Xml.Serialization.XmlElementAttribute("xTxIDs", typeof(System.Guid[]))]
public Guid[] Items;
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://x.com/xml/namespace/2023/07/x/xV135", Order = 2)]
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public System.Nullable<bool> SendEmail;
public CancelIndiciumRequest()
{
}
public CancelIndiciumRequest(object Item, Guid[] Items, System.Nullable<bool> SendEmail)
{
this.Item = Item;
this.Items = Items;
this.SendEmail = SendEmail;
}
}
I had to comment out the code in the codegen Reference.cs.
I ran my UI tests and got back the expected response every time. I know there has to be a cleaner way and that If I re-gen the code from the WSDL I'm manually editing.
Upvotes: 0