Skip to content

Instantly share code, notes, and snippets.

@bohdanszymanik
Created May 19, 2015 01:00
Show Gist options
  • Save bohdanszymanik/b10b6d26fe58ac37406e to your computer and use it in GitHub Desktop.
Save bohdanszymanik/b10b6d26fe58ac37406e to your computer and use it in GitHub Desktop.
Example F# wsdlService typeprovider call with authentication and extra attributes (max message size)
open System
open System.ServiceModel
open Microsoft.FSharp.Linq
open Microsoft.FSharp.Data.TypeProviders
(* bit dumb but because service requests authentication, and we can't do that in the wsdlservice tp, we have to download wsdl, load it up locally, set
binding up properly, then use tp *)
open System.Net
open System.IO
open System.Text
let getHtml (url: string) =
let req = System.Net.WebRequest.Create(url)
// req.Proxy.Credentials <- System.Net.CredentialCache.DefaultNetworkCredentials
req.Credentials <- System.Net.CredentialCache.DefaultNetworkCredentials
let resp = req.GetResponse()
let stream = resp.GetResponseStream()
let reader = new StreamReader(stream)
let html = reader.ReadToEnd()
resp.Close()
html
let wsdlSchemaPrepend = @"<?xml version=""1.0"" encoding=""utf-8""?>
<ServiceMetadataFiles>
<ServiceMetadataFile name=""ec2.wsdl"">"
let wsdlSchemaPostpend ="
</ServiceMetadataFile>
</ServiceMetadataFiles>"
let rawWsdl (xmlWsdl : string) =
// strip xml definition
xmlWsdl.Replace(@"<?xml version=""1.0"" encoding=""utf-8""?>", "")
// why this malarchy? http://stackoverflow.com/questions/22096024/compile-error-using-f-wsdlservice-type-provider
let wsdlSchema = wsdlSchemaPrepend
+ rawWsdl(getHtml("http://wherever you put the wsdl/RP.svc?singleWSDL"))
+ wsdlSchemaPostpend
File.WriteAllText(__SOURCE_DIRECTORY__ + @"\RP.wsdlschema", wsdlSchema)
type RP = WsdlService<ServiceUri="N/A",
ForceUpdate = false,
LocalSchemaFile = @"c:\location of stored .wsdlschema">
// more trickery https://stackoverflow.com/questions/18981682/f-wsdlservice-type-provider-proxy
let RPClient = RP.GetBasicHttpBinding_IReportParty()
let binding = RPClient.DataContext.Endpoint.Binding :?> System.ServiceModel.BasicHttpBinding
binding.MaxReceivedMessageSize <- 200000L // our extra attribute... I needed to make the maxmessagesize larger
binding.Security.Transport.ClientCredentialType <- HttpClientCredentialType.Windows
(* if using a proxy server...
binding.ProxyAddress <- System.Uri("http://127.0.0.1:8888")
binding.BypassProxyOnLocal <- false
binding.UseDefaultWebProxy <- false
*)
let req = RP.ServiceTypes..down the hierarchy to someWebMethodRequest()
req.someparameter <- "123456"
// and any other parameters required
let resp = RPClient.someWebMethodToExecute(req)
resp.Result
resp.Errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment