WCF Export
.NET
Given the URL of a WCF service (along with a output prefix and target namespace), this Python script generates C# proxy source and config files. These files can then be imported/copied to a .NET project and used directly to consume the web service.
import os
import string
import subprocess
import sys
class WCFExporter:
def __init__(self, serviceURL, outputPrefix, targetNamespace):
self.serviceURL = serviceURL
self.outputPrefix = outputPrefix
self.targetNamespace = targetNamespace
self.svcUtilCmd = 'C:\\PROGRA~1\\MI2578~1\\Windows\\v6.0A\\Bin\\SvcUtil.exe'
def DropGeneratedCodeAttribute(self):
try:
= self.outputPrefix + '.temp.cs'
mySourceFile = self.outputPrefix + '.cs'
myTargetFile
= open(mySourceFile, 'r')
mySourceHandle = open(myTargetFile, 'w')
myTargetHandle
for inputLine in mySourceHandle:
if 'GeneratedCodeAttribute' not in inputLine:
myTargetHandle.write(inputLine)
myTargetHandle.close()
mySourceHandle.close()
os.remove(mySourceFile)
except Exception as ex:
print '[ERROR] ' + str(ex)
def GenerateFiles(self):
try:
= -1
retcode
= self.svcUtilCmd + ' /t:code ' + self.serviceURL + ' /out:' + self.outputPrefix + '.temp.cs /config:' + self.outputPrefix + '.config /namespace:*,' + self.targetNamespace
myCommandString
= subprocess.call(myCommandString)
retcode
except Exception as ex:
print '[ERROR] ' + str(ex)
return retcode
def UsageMessage(self):
print '\nUSAGE: wcfexport.py <wcf service url> <output prefix> <target namespace>'
print '\n\n\tEXAMPLE: wcfexport.py http://CoolService:9030 MyNewProxy MyCompany.MyPkg'
print '\n\n\tOUTPUT: MyNewProxy.cs and MyNewProxy.config'
### MAIN starts here ###
= WCFExporter('','','')
myWCFExporter
if len(sys.argv) != 4:
myWCFExporter.UsageMessage()1)
sys.exit(
= sys.argv[1]
myWCFExporter.serviceURL = sys.argv[2]
myWCFExporter.outputPrefix = sys.argv[3]
myWCFExporter.targetNamespace
print '\n'
= myWCFExporter.GenerateFiles()
retcode
if retcode == 0:
myWCFExporter.DropGeneratedCodeAttribute()