Last active
April 4, 2016 23:35
-
-
Save jamessantiago/a6c95e156563a802e0eb3c1d52e215c9 to your computer and use it in GitHub Desktop.
See https://stackoverflow.com/questions/35761583/wcf-maxes-cpu-when-waiting-on-transparantproxystub-crosscontext-function-during/36395754#36395754 for explanation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$template = @" | |
namespace AxlNetClient | |
{{ | |
public partial class AXLPortClient | |
{{ | |
private static AXLPortClient{0} _axlPortClient{0}; | |
private static AXLPortClient{0} axlPortClient{0} => _axlPortClient{0} == null || | |
_axlPortClient{0}.State != System.ServiceModel.CommunicationState.Opened ? (_axlPortClient{0} = LoadClient{0}()) : _axlPortClient{0}; | |
private static AXLPortClient{0} LoadClient{0}() | |
{{ | |
var client = new AXLPortClient{0}(EndpointConfiguration, EndpointAddress); | |
client.Endpoint.EndpointBehaviors.Add(new AxlNetClient.AuthenticationBehavior(User, Password)); | |
OnClose += Close{0}; | |
OnAbort += Abort{0}; | |
return client; | |
}} | |
private static void Close{0}(object sender, System.EventArgs e) | |
{{ | |
axlPortClient{0}.Close(); | |
}} | |
private static void Abort{0}(object sender, System.EventArgs e) | |
{{ | |
axlPortClient{0}.Abort(); | |
}} | |
{1} | |
}} | |
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://www.cisco.com/AXLAPIService/", ConfigurationName = "AxlNetClient.AXLPort")] | |
public interface AXLPort{0} | |
{{ | |
{2} | |
}} | |
public class AXLPortClient{0} : System.ServiceModel.ClientBase<AxlNetClient.AXLPort{0}>, AxlNetClient.AXLPort{0} | |
{{ | |
public AXLPortClient{0}(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : | |
base(binding, remoteAddress) | |
{{ | |
}} | |
{3} | |
}} | |
}} | |
"@ | |
$cmtemplate = @" | |
{0} | |
{{ | |
return axlPortClient{1}.{2}({3}); | |
}} | |
"@ | |
write-host "Clearing useless attributes and comments..." -nonewline | |
cat .\Client.cs |% { | |
if ($_ -notmatch " \[System\.ServiceModel\.ServiceKnown" -and ` | |
$_ -notmatch " \[System\.ServiceModel\.FaultContractAttribute" -and ` | |
$_ -notmatch " \[System\.Xml\.Serialization\.XmlIncludeAttribute" -and ` | |
$_ -notmatch "// CODEGEN" -and $_ -notmatch "// <remarks") | |
{ | |
$_ | |
} | |
} | Set-Content .\ClientOut.cs | |
write-host "Done" | |
write-host "Loading updated client..." -nonewline | |
$client = cat .\ClientOut.cs | |
write-host "Done" | |
$operations = New-Object System.Collections.ArrayList | |
$clientmethods = New-Object System.Collections.ArrayList | |
write-host "Loading interface contracts..." -nonewline | |
for ($i = 0; $i -lt $client.length; $i++) | |
{ | |
if ($client[$i] -match "\[System.ServiceModel.OperationContract." -and $client[$i+1] -match "\[System.ServiceModel.XmlSerial.") | |
{ | |
$temp = "" | |
$tempLength = $i + 6 | |
for ($j = $i; $j -lt $tempLength; $j++) { | |
$temp += $client[$j] + "`n" | |
} | |
$operations.add($temp) | out-null | |
} | |
elseif ($client[$i] -match "\[System\.ComponentModel\.EditorBrowsa" -and $client[$i+1] -match " AxlNetClient\.") | |
{ | |
$temp = "" | |
$tempLength = $i + 26 | |
for ($j = $i; $j -lt $tempLength; $j++) { | |
$temp += $client[$j] + "`n" | |
} | |
$clientmethods.add($temp) | out-null | |
} | |
} | |
write-host "Done" | |
write-host $operations.Count " operations loaded" | |
write-host $clientmethods.Count " client methods loaded" | |
write-host "Creating sub port clients..." -nonewline | |
$groupSize = 12 | |
$groupCount = 1 | |
for ($i = 0; $i -lt $operations.Count; $i += $groupSize){ | |
$tempLength = $i + $groupSize | |
$groupoperations = New-Object System.Collections.ArrayList | |
$groupclientmethods = New-Object System.Collections.ArrayList | |
$globalmethods = New-Object System.Collections.ArrayList | |
for ($j = $i; $j -lt $tempLength; $j++) { | |
$groupoperations.add($operations[$j]) | Out-Null | |
$cm = $clientmethods[$j] -replace "AXLPort", "AXLPort$groupCount" | |
if ($cm) { | |
$groupclientmethods.add($cm) | Out-Null | |
$cmsigs = [regex]::matches($cm, "public AxlNetClient.*|public System.*") | select -ExpandProperty value | |
$cmMethodNames = [regex]::matches($cmsigs, "(\w+)\(") |% {$_.groups[1].value} | |
$cmParams = [regex]::matches($cmsigs, "(\w+)\)") |% {$_.groups[1].value} | |
$globalmethods.Add(($cmtemplate -f $cmsigs[0], $groupCount, $cmMethodNames[0], $cmParams[0])) | Out-Null | |
$globalmethods.Add(($cmtemplate -f $cmsigs[1], $groupCount, $cmMethodNames[1], $cmParams[1])) | Out-Null | |
} | |
} | |
$template -f $groupCount, $globalmethods, $groupoperations, $groupclientmethods | Out-File "Client.AXLPortClient$groupCount.cs" | |
$groupCount++ | |
} | |
write-host "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment