Invoking multiple WCF services

In versions below 1.1.9, WCFStorm can invoke a method (or run a test case) but it is unable to use the response as an input to another WCF method.  With the support for IronPython scripting in version 1.1.9 this is now possible.

Suppose we have the scenario described below

----------
Scenario:
----------
 
    1. A WCF Service (IService1) has a method called GetToken() which returns a string token.
 
public string GetToken()  {  returns "A string token"; }
 
     2. Another WCF Service (IService2) has methods which expects a token as one of the parameters to its method.

  public bool DoSomething(string token, int someValue)  
  {
   //Do something
    return true;
  }
 
-----------
TestCase
-----------

 
     1.  Invoke service1.GetToken() to retrieve the token
     2.  Pass the token to service2.DoSomething().
 
--------------------
Scripting solution
--------------------
import sys
import clr
clr.AddReference("System.XML.dll")

import System.Xml
import System.Diagnostics
from System.Xml import XmlDocument
from  System.Diagnostics import Stopwatch

class MySvc:

  def __init__(self, url, contractName):
     self.url = url
     self.contractName = contractName
  def InvokeMethod(self, methodName, xmlInput):
     xmlResp = task.InvokeMethod(self.url, self.contractName , methodName, xmlInput)
     return xmlResp
  
class MyTestWorkFlow:
  """this is the documentation for the test workflow"""

  xmlInputGetToken = "\
  <GetToken>\
    <MethodParameters>\
      <username>myusername</username>\
      <password>mypassword</password>\
    </MethodParameters>\
  </GetToken>"
  
  xmlInputDoSOmething = "\
  <DoSomething>\
    <MethodParameters>\
      <token>sometoken</token>\
      <someValue>42</someValue>\
    </MethodParameters>\
  </DoSomething>"
  
  def Run(self):
    svc1 = MySvc("http://localhost:57195/Service1.svc", "IService1")
    svc2 = MySvc("http://localhost:57195/Service1.svc", "IService2")
 
    print "Invoking GetToken...\n"
    svc1Resp = svc1.InvokeMethod("GetToken", self.xmlInputGetToken)
    print "Response is: " + svc1Resp
 
    svc1Doc = XmlDocument()
    svc1Doc.LoadXml(svc1Resp)
    token = svc1Doc.SelectSingleNode("/GetToken/MethodParameters/String").InnerText
    print "token retrieved from IService1 is " + token

    docDoSomething = XmlDocument()
    docDoSomething .LoadXml(self.xmlInputDoSOmething)
 
    #Use the token retrieved from svc1
    docDoSomething .SelectSingleNode("/DoSomething/MethodParameters/token").InnerText = token
 
    #invoke IService2 with token retrieved from IService1
    print "Invoking DoSomething..."
    print "Input is " +  docDoSomething .OuterXml
    doSomethingXmlResp = svc2.InvokeMethod("DoSomething", docDoSomething.OuterXml)
    doSomethingXmlDoc = XmlDocument()
    doSomethingXmlDoc.LoadXml(doSomethingXmlResp)
    
 
    if doSomethingXmlDoc.SelectSingleNode("/DoSomething/MethodParameters/Boolean").InnerText == "True":
     print "token " + token + " is valid!"
    else:
     print "invalid token"
     #sys.exit("Oh no what happened??? You're estimate was arbitarily reduced again, right?")

test = MyTestWorkFlow() 
test.Run()

#w = Stopwatch.StartNew() 
#test = MyTestWorkFlow() 
#for i in range(1,100): 
#  test.Run()  
#w.Stop() 
#print "\n\nTest Elapsed %f seconds" % w.Elapsed.TotalSeconds
 
After pressing F5 to execute the script, the output isExecuting script...

---------------
Script output
---------------
Executing script...
 
Invoking GetToken...
 
Response is: <GetToken>   <MethodParameters>     <String>a4324edc-94ba-4b23-907c-c6d8b6d5e1f5</String>   </MethodParameters> </GetToken>
token retrieved from IService1 is a4324edc-94ba-4b23-907c-c6d8b6d5e1f5
 
Invoking DoSomething...
Input is <DoSomething><MethodParameters><token>a4324edc-94ba-4b23-907c-c6d8b6d5e1f5</token><someValue>42</someValue></MethodParameters></DoSomething>
token a4324edc-94ba-4b23-907c-c6d8b6d5e1f5 is valid!
 
 
Download sample files : 
 

 


Site Map | Printable View | © 2008 - 2024 WCFStorm Solutions

Powered by mojoPortal | XHTML 1.0 | CSS | Design by styleshout