我使用内置 WSDL 类型提供程序编写了以下 F# 3.0 程序来自动生成 Amazon WSDL 的 F# 版本:
open Microsoft.FSharp.Data.TypeProviders
type azn = WsdlService<"http://soap.amazon.com/schemas2/AmazonWebServices.wsdl">
let authorRequest author =
azn.ServiceTypes.AuthorRequest(author=author)
do
let client = azn.GetAmazonSearchPort()
let response = client.AuthorSearchRequest(authorRequest "Harrop")
printfn "%s" response.TotalResults
当我运行这个程序时,我在运行时从 Microsoft 工具堆栈中得到了一个令人兴奋的内部异常:
Unhandled Exception: System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (410) Gone. ---> System.Net.WebException: The remote server returned an error: (410) Gone.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(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 Program.azn.ServiceTypes.AmazonSearchPort.AuthorSearchRequest(AuthorRequest AuthorSearchRequest1)
at Program.azn.ServiceTypes.AmazonSearchPortClient.AuthorSearchRequest(AuthorRequest AuthorSearchRequest1)
at Program.azn.ServiceTypes.SimpleDataContextTypes.AmazonSearchPortClient.AuthorSearchRequest(AuthorRequest )
at <StartupCode$ConsoleApplication2>.$Program.main@() in c:\users\jon\documents\visual studio 11\Projects\ConsoleApplication2\ConsoleApplication2\Program.fs:line 5
我发现这里有一个更新的架构:
type azn = WsdlService<"http://soap.amazon.com/schemas2/AmazonWebServices.wsdl">
但这并不能解决我令人兴奋的错误消息。有什么问题以及如何解决它?
最佳答案
我不知道端到端解决方案,但可能可以帮助您更进一步
您现在使用的网址对应于 API 的过时版本,我相信更新的版本是 http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl
如果您只是将此 url 传递给 WsdlService 类型提供程序,那么在设计时一切都会很好,但在运行时会出现奇怪的错误,例如“序列化消息 ItemSearchRequest1 正文时出错:'无法生成临时类(结果 = 1)。错误 CS0030:无法将类型“Program.Amazon.ServiceTypes.ImageSet[]”转换为“Program.Amazon.ServiceTypes.ImageSet”;错误 CS0029:无法隐式转换类型“Program.Amazon.ServiceTypes”。 ImageSet' 更改为 'Program.Amazon.ServiceTypes.ImageSet[]'”。
这似乎是已知错误( here ),要修复它,您应该设置 ForceUpdate=false 和 LocalSchemaFile='your local schema file' ,然后在本地架构文件中修复 ImagesSet 的定义
<xs:element minOccurs="0" maxOccurs="unbounded" name="ImageSets">
至
<xs:element minOccurs="0" maxOccurs="1" name="ImageSets">
type Amazon = Microsoft.FSharp.Data.TypeProviders.WsdlService<
@"http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl",
ForceUpdate=false,
LocalSchemaFile="amazon.wsdlschema"
>
let searchAuthor author =
Amazon.ServiceTypes.ItemSearch(Request = [| Amazon.ServiceTypes.ItemSearchRequest(Author = author) |])
[<EntryPoint>]
let main argv =
let amazon = Amazon.GetAWSECommerceServicePort()
let result = amazon.ItemSearch (searchAuthor "Harrop")
0
然而,这仍然不是故事的结局 - 这段代码抛出 MessageSecurityException:“客户端身份验证方案‘匿名’禁止 HTTP 请求”。它看起来也像已知问题(即 here ),但要检查解决方案,您需要 Amazon 用户 ID 和 key (我没有)。
关于f# - 将 F# 3.0 中的 Amazon WSDL Web 服务与类型提供程序结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027270/