Problem using CSharpCodeProvider to add types at runtime
i having intermittent problem adding c# types powershell. (if isn't right forum, please advise me on post question).
i encountered issue using built-in add-type cmdlet, in order @ root of issue have written own variation of cmdlet:
function new-type { param( [parameter(mandatory=$true,position=1)][string]$typedefinition=$(throw "mandatory parameter -typedefinition missing.") ) $params = new-object system.codedom.compiler.compilerparameters $params.referencedassemblies.addrange($(@("system.dll", $([psobject].assembly.location)))) $params.generateinmemory = $true $temp = $(get-item env:temp).value $params.tempfiles = new-object system.codedom.compiler.tempfilecollection $temp, $false $provider = new-object microsoft.csharp.csharpcodeprovider try { $compilerresults = $provider.compileassemblyfromsource($params, $typedefinition) if ($compilerresults.errors.count -gt 0) { $codelines = $typedefinition -split '[\n]'; $errormessage = "compilation errors:" foreach ($compileerror in $compilerresults.errors) { $errormessage = [string]::concat($errormessage, "`n", $compileerror.tostring()) } write-error $errormessage } } catch [system.exception] { if ($_.gettype().tostring() -eq "system.management.automation.errorrecord") { write-error $_.exception.tostring() } else { write-error $_.tostring() } } } this new function works great, unfortunately no more reliable add-type. have been able stack trace out of it:
system.io.filenotfoundexception: not find file 'c:\users\[name]\appdata\local\temp\abd4anxc.dll'. file name: 'c:\users\[name]\appdata\local\temp\abd4anxc.dll' @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share, int32 buffersize, fileoptions options, string msgpath, boolean bfromproxy) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share) @ microsoft.csharp.csharpcodegenerator.fromfilebatch(compilerparameters options, string[] filenames) @ microsoft.csharp.csharpcodegenerator.fromsourcebatch(compilerparameters options, string[] sources) @ microsoft.csharp.csharpcodegenerator.system.codedom.compiler.icodecompiler.compileassemblyfromsourcebatch(compilerparameters options, string[] sources) @ system.codedom.compiler.codedomprovider.compileassemblyfromsource(compilerparameters options, string[] sources) @ compileassemblyfromsource(object , object[] ) @ system.management.automation.dotnetadapter.auxiliarymethodinvoke(object target, object[] arguments, methodinformation methodinformation, object[] originalarguments)
a few other folks seem have observed similar issues before, experiencing compiler errors. not -- appear running error wherein compiler fails serialize assembly temp file (which little "trick" performs pretend compilation happening in memory).
unfortunately, seems worst kind of error, 1 happens "sometimes" not always. once put code in loop, , failed 20k times in row. when it's broken, stays broken (for little while anyway). re-starting powershell clear problem, not always. what's going on??
i avoid having ship dlls powershell modules, adding types dynamically extremely appealing. how can determine what's causing intermittent error?
thanks!
hi ,
thank posting issue in forum.
i trying involve familiar topic further @ issue. there might time delay. appreciate patience.
thank understanding , support.
best regards,
andy qi
andy qi
technet community support
Windows Server > Windows PowerShell

Comments
Post a Comment