Skip to content

Instantly share code, notes, and snippets.

@bohdanszymanik
Created August 11, 2015 21:49
Show Gist options
  • Select an option

  • Save bohdanszymanik/205087212cc9bc13f724 to your computer and use it in GitHub Desktop.

Select an option

Save bohdanszymanik/205087212cc9bc13f724 to your computer and use it in GitHub Desktop.
wsdlservice call when there's proxy's, a need to change http parameters, authentication etc etc
open System
open FSharp.Data
open System
open System.ServiceModel
open Microsoft.FSharp.Linq
open Microsoft.FSharp.Data.TypeProviders
(* execute the following commented stuff only once to get the wsdl, then leave commented
(* 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=""some.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://...some end point... .svc?singleWSDL"))
+ wsdlSchemaPostpend
File.WriteAllText(__SOURCE_DIRECTORY__ + @"\someService.wsdlschema", wsdlSchema)
*)
type RPD = WsdlService<ServiceUri="N/A",
ForceUpdate = false,
LocalSchemaFile = @"c:\users\bszyma02\documents\visual studio 2015\Projects\ALSLookup\someService.wsdlschema">
// more trickery https://stackoverflow.com/questions/18981682/f-wsdlservice-type-provider-proxy
let RPDClient = RPD.GetBasicHttpBinding_ISomethingOrAnother()
let binding = RPDClient.DataContext.Endpoint.Binding :?> System.ServiceModel.BasicHttpBinding
binding.MaxReceivedMessageSize <- 200000L
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 randomReq min max count =
let rand = System.Random()
let timer = System.Diagnostics.Stopwatch.StartNew()
seq{1 .. count}
|> Seq.iter (fun i ->
let an = rand.Next(min, max)
let req = RPD.ServiceTypes.SomeServices.DataContracts.SomeNamespace.SomeRequest()
req.Number <- (int64)an
RPDClient.GetLocation(req) |> ignore
//printfn "%A" resp.AccountLocation
//printfn "%A" resp.Errors
)
let endTime = timer.Elapsed.TotalMilliseconds
printfn "Time taken for %d lookups %gms" count endTime
printfn "Average time per request %fms" (endTime/(float)count)
[<EntryPoint>]
let main(args) =
printfn "args: %A" args
printfn "args length %d" args.Length
if args.Length = 0 then
printfn "Parameters: min IDnumber, max IDnumber, count"
else
randomReq ((int)args.[0]) ((int)args.[1]) ((int)args.[2])
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment