我在C#上遇到了WCF服务的问题。
我尝试定义小服务:
using System.ServiceModel;
namespace TestApp
{
[ServiceContractAttribute(Namespace = "http://testApp.ru/LittleService", Name = "ILittleService", ConfigurationName = "ILittleService")]
public interface ILittleService
{
[OperationContract(Action = "urn:GetArrayOfCars")]
[TransactionFlow(TransactionFlowOption.Allowed)]
[XmlSerializerFormat]
GetArrayOfCarsResponse GetArrayOfCars(GetArrayOfCarsRequest request);
[OperationContract(Action = "urn:GetArrayOfTrains")]
[TransactionFlow(TransactionFlowOption.Allowed)]
[XmlSerializerFormat]
GetArrayOfTrainsResponse GetArrayOfTrains(GetArrayOfTrainsRequest request);
}
}
每个回复都会列出一系列汽车/火车:
namespace TestApp.ResponseWrappers
{
[System.ServiceModel.MessageContractAttribute(WrapperName = "GetArrayOfCarsResponse", WrapperNamespace = "http://testApp.ru/LittleService", IsWrapped = true)]
public class GetArrayOfCarsResponse
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "", Order = 0)]
[System.Xml.Serialization.XmlArrayAttribute()]
[System.Xml.Serialization.XmlArrayItemAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public CarResult[] result { get; set; }
public GetArrayOfCarsResponse()
{
}
public GetArrayOfCarsResponse(CarResult[] result)
{
this.result = result;
}
}
}
和
namespace TestApp.ResponseWrappers
{
[System.ServiceModel.MessageContractAttribute(WrapperName = "GetArrayOfTrainsResponse", WrapperNamespace = "http://testApp.ru/LittleService", IsWrapped = true)]
public class GetArrayOfTrainsResponse
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "", Order = 0)]
[System.Xml.Serialization.XmlArrayAttribute()]
[System.Xml.Serialization.XmlArrayItemAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public TrainResult[] result { get; set; }
public GetArrayOfTrainsResponse()
{
}
public GetArrayOfTrainsResponse(TrainResult[] result)
{
this.result = result;
}
}
}
当我尝试通过SoupUI使用此服务时:
GetArrayOfCars方法正常
对于GetArrayOfTrains方法,我遇到了一个奇怪的错误:
Wed Sep 13 14:45:23 GMT+03:00 2017:ERROR:org.apache.http.conn.HttpHostConnectException: Connection to http://jcaps2-kv:8080 refused
org.apache.http.conn.HttpHostConnectException: Connection to http://jcaps2-kv:8080 refused
at com.eviware.soapui.impl.wsdl.support.http.SoapUIMultiThreadedHttpConnectionManager$SoapUIClientConnectionOperator.openConnection(SoapUIMultiThreadedHttpConnectionManager.java:290)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:561)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport$Helper.execute(HttpClientSupport.java:233)
at com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport.execute(HttpClientSupport.java:323)
at com.eviware.soapui.impl.wsdl.submit.transports.http.HttpClientRequestTransport.submitRequest(HttpClientRequestTransport.java:297)
at com.eviware.soapui.impl.wsdl.submit.transports.http.HttpClientRequestTransport.sendRequest(HttpClientRequestTransport.java:227)
at com.eviware.soapui.impl.wsdl.WsdlSubmit.run(WsdlSubmit.java:119)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
at com.eviware.soapui.impl.wsdl.support.http.SoapUIMultiThreadedHttpConnectionManager$SoapUIClientConnectionOperator.openConnection(SoapUIMultiThreadedHttpConnectionManager.java:273)
... 16 more
但服务方法运行正常(我调试它们)。在结果序列化阶段检测错误(通过SvcTraceViewer):
Massage: Unable to cast object of type 'TestApp.Data.TrainResult[]' to type 'TestApp.Data.CarResult[]'.
StackTrace: Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterIProductCatalogV2Part1.Write89_GetRegionalCatalogInfoResponse(Object[] p)
System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
在研究了这个问题后,我获得了服务的WSDL,并在其中找到了非常有趣的行:
<xs:complexType name="ArrayOfCarResult">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" form="unqualified" name="CarResult" nillable="true" type="CarResult"/>
</xs:sequence>
</xs:complexType>
<xs:element name="result" type="ArrayOfCarResult"/>
<xs:element name="GetArrayOfCarsResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" ref="result"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetArrayOfTrainsResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" ref="result"/>
</xs:sequence>
</xs:complexType>
</xs:element>
如果我在响应中对数组使用不同的名称,则所有方法都可以正常工作。但我需要使用相似的名字。
对XmlArrayAttribute使用ElementName属性对我没有帮助(如果我使用&#34;结果&#34;字符串)。
有没有人遇到过这样的问题?