我必须使用C#程序调用webservice。 web服务很可能不是标准格式。接口描述(wsdl和xsd)非常复杂,使用代理生成机制会导致数百个类。生成的类没有什么帮助,因为它们是非常通用的,主要具有简单的对象类型作为成员。最好的选择是手动构建SOAP消息。这也是Web服务提供者建议选择的方式:根据模板获取必须发送的soap/xml消息并构建消息。现在的问题是如何最有效地构建消息。当然硬编码的消息字符串是一种选择,但我想知道是否有更好的选择。如果我在一个字符串中有完整的消息,我该如何最好地发送消息。我应该使用简单的HttpRequest还是可以使用wcf堆栈的机制? 我现在的建立消息的做法是这样的:在没有代理的情况下调用web服务
string msg = envelopeBegin;
RouteType rootType = new RouteType();
XmlSerializer serializer = new XmlSerializer(typeof(RouteType));
StringWriter stringWriter = new StringWriter();
serializer.Serialize(stringWriter, rootType , customNamespace);
msg += stringWriter.ToString();
msg += envelopeEnd;
//发送消息,在导线
的SOAP/XML消息,我不得不产生这样的
<env:Envelope>xmlns:env=http://schemas.xmlsoap.org/soap/envelope/ xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://www.skanska.se/oagis/9/ws/faults">
<env:Body>
<ska:ShowSalesOrder xmlns:ska="http://www.skanska.se/oagis/9" systemEnvironmentCode="UTV" versionID="1.0" releaseID="9.0">
<!--plsql=.74s-->
<ApplicationArea xmlns="http://www.openapplications.org/oagis/9">
<!--user_name=SEBA_RAPPE-->
<ska:Sender>
<LogicalID>OEBS_SE</LogicalID>
<ComponentID>SKAIS017I</ComponentID>
<AuthorizationID>SEBA_RAPPE</AuthorizationID>
<ska:ResponsibilityID>XXOM_INTEGRATION_SVT</ska:ResponsibilityID>
</ska:Sender>
<CreationDateTime>2010-02-26T15:03:27+01:00</CreationDateTime>
<BODID>xxxxxxxxxxxxxxxxx</BODID>
</ApplicationArea>
<ska:DataArea>
<Show xmlns="http://www.openapplications.org/oagis/9">
<ResponseCriteria>
<ResponseExpression actionCode="Never" expressionLanguage="xPath">*</ResponseExpression>
</ResponseCriteria>
</Show>
<ska:SalesOrder>
<SalesOrderHeader xmlns="http://www.openapplications.org/oagis/9">
<DocumentID>
<ID>141779</ID>
</DocumentID>
<RequestedShipDateTime>2009-11-04T07:00:54+01:00</RequestedShipDateTime>
</SalesOrderHeader>
</ska:SalesOrder>
</ska:DataArea>
</ska:ShowSalesOrder>
</env:Body>
</env:Envelope>
是的,这可能是真实的 - 你可以使用非类型化的消息类的实例 - 但你不能没有通过WCF客户端代理栈去调用基于SOAP的WCF服务.... – 2010-02-27 23:04:57
是啊,这这个想法。他仍然可以从WCF客户端渠道和基础设施中获益,但却没有大量客户端类型定义。当你只需要10种左右的类型时,这可能是一种痛苦,WSDL定义了数百种。 – Josh 2010-02-27 23:07:35
@marc_s:如果你愿意,你可以使用一个原始的TCP套接字来访问一个WCF服务,但是他们需要做很多工作,HttpWebRequest会更容易。 “WCF客户端代理栈”是什么意思?创建一个从System.ServiceModel.ClientBase派生的自定义代理很简单,而不必诉诸VS.Net创建的详细代理。 – sipwiz 2010-02-27 23:10:58