.debug .NET and BizTalk Rants

31Jan/120

WCF REST Services and HTTP Redirects

We have an existing RESTful method that does a redirect (HTTP 302) for a specific condition.  Our proxy services complained that the redirect abruptly closes the connection which wrecks havoc on their proxy servers...he has the curl command to prove it, and it returns it like this:

curl(18): transfer closed with outstanding read data remaining

The funny thing is, when I run the REST HTTP service under Visual Studio debugger using the built-in web server, i don't get this error!  it only appears when I deploy it on an IIS server.

Here's the original code:

if (...)
{
  HttpContext.Current.Response.Redirect(new_url);
}

I changed it to:

if (...)
{
  HttpContext.Current.Response.Rediect(new_url, false);
}

The problem now is that status code is being set to HTTP 200!  To get around this (after a lot of trial and error),

I added this code:

WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;

BAM!  Fixed.  No more curl errors!

Filed under: .NET, IIS No Comments
28Dec/110

Automate FTP in PowerShell

Sometimes solutions can just be so simple...why complicate it?

 

Filed under: PowerShell No Comments
17Dec/110

IIS 7.x Dynamic Compression

I'm not a big fan of Scott Hanselman.  I personally think the guy has a very high opinion of himself and you really need to weed out a lot of the BS and unfunny stuff he's saying to get the meat of what he's talking about.

Having said all of that, this blog post of his is the ONLY useful source out there about enabling IIS 7 Dynamic Compression.

Filed under: .NET, IIS No Comments
9Dec/110

Web Deploy 2.0 and Remote IIS

This seems simple but believe it or not it took me a long while to find the answer..

If you're using Web Deploy 2.0 and you try to deploy your web application to a remote server by using https://remote_server:8172/msdeploy.axd you may encounter an error message that is along the lines of the destination is unreachable, or the required process "The Web Management Service" is started.

If you encounter this, try to check the services on the REMOTE SERVER if Web Management Service exists and is running.  Chances are, it's missing.  If it's missing, you need to launch the Web Platform Installer and install "IIS: Management Service" then go to Services and make sure it's running.

Update: If you're still having problems, like getting a 404 or 403 errors on deploying the app to a remote IIS (i.e., using TFS), there's something else you need to do :-| : check out this thread from stack overflow and read the response from simbolo.

Filed under: .NET, TFS No Comments
26Oct/110

Upgrading from Silverlight 2.x to 4.x

Forget about installing and reinstalling the Silverlight SDK Tools, forget about the tools that just crashes Visual Studio for no apparent reason, this is probably one of the things that frustrates the hell out of SilverLight development: upgrading from older to newer version.

I am not a SilverLight expert--far from it.  I don't intend to learn this, and the only reason I'm going through the motions is because some system that I happen to have inherited uses SilverLight.  SilverLight is probably one of the top 5 things I hate that came out of the Microsoft pile of trainwreck (WiX being the #1).

Anyway, enough with the rant.

I upgraded the UI from SilverLight 2.x to 4.x and one of the things that Microsoft got rid of is the asp:SilverLight tag to plop in your SL app on a page; it's recommended that you use the object.  The issue with this is that the silverlight object is now not accessible from code-behind...so in SL 2.x where it's easy to pass a dynamic initParams string with:

mySLApp.InitParameters = "O HAI LOL";

...now with SL 4, this becomes a nuisance.  Fortunately with this blog posting, I have found an elegant way to pass initialization parameters...make sure you read the comment from Jelgab.

Filed under: SilverLight No Comments
22Sep/110

Visual Studio 2010 Build Template Editing

When I learned that you can finally use WF to customize builds in TFS, I got excited.  Thank goodness I don't need to re-learn MSBuild to customize my builds!!  But the more I

  • Whoever designed the WF Editing UI needs to die.  Right now.  Holy kitten nipples!  And I thought BizTalk Orchestration editing was atrocious.
  • No inline C# code support.  It's like Chuck Norris delivered a roundhouse kick to my face.  BizTalk Orchestration has this since BizTalk 2004.  Over 7 years ago.  Why do I need to write a separate DLL to use custom code?  Now, this ain't an option here because...I have to deploy the DLL on the build machine to make it work!  Microsoft, why you do this?
  • Debugging.  No, not even going there.  I'm just gonna cry right here.
I ended up using InvokeProcess with PowerShell for what I'm doing, which is really clunky at best.
This is probably going to be all moot when Windows 8 comes out as we're all gonna be using Eclipese to write HTML5 "apps". LA DEE DA.  :-/
30Aug/110

Removing XML Namespaces with LINQ

Generally speaking, removing namespaces in an XML is never a good thing...if you need to remove them, you're doing it wrong!

Unfortunately, I happen to need to fix some code that strips the namespace because it had a glaring flaw:

private static XElement RemoveNamespace(XElement e)
{
  return new XElement(e.Name.LocalName,
    e.HasElements ? e.Elements().Select(n => RemoveNamespace(n)) : (object) e.Value,
    e.HasAttributes ? e.Attributes().Select(a => new XAttribute(a.Name,a.Value)) : null);
}

See it? Don't feel too bad if you don't; I didn't see it, either, until I ran the code through the debugger.

The problem is it can't handle mixed mode content like this:

<p>I have an <a href="http://mylol.com">LOLz.</a> Thanxbai!</p>

It becomes:

<a href="http://mylol.com">LOLz.</a></p>

It's funny that in stackoverflow there's this exact same question and a number of answers...most of them wrong. One used XSLT (using the term "obligatory") which is correct but i think is a bit overkill. One used RegEx and failed. And most of them has this same flaw I presented here.

I chimed in:

public static XElement RemoveAllNamespaces(XElement e)
{
 return new XElement(e.Name.LocalName,
    (from n in e.Nodes()
      select ((n is XElement) ? RemoveAllNamespaces(n as XElement) : n)),
    (e.HasAttributes) ? (from a in e.Attributes() select a) : null);
}

Note that I traverse through Nodes() instead of just Elements()...which will go through each text() nodes...and it will have this nice bonus:

  • I don't need to set the XElement.Value explicitly (traversing through text() Nodes does that for me)
  • I can copy comment nodes
Filed under: .NET, LINQ No Comments
10Aug/110

Creating Batch Files with PowerShell Redirect FAIL

To dynamically create a batch file using PowerShell, one would think that the easiest way would be something like this:

"echo hello world" >hello.cmd

Guess what? The resulting batch file doesn't work...because redirection in Powershell uses UNICODE for encoding. The only way to do this is using the out-file cmdlet specifying the correct encoding.

"echo hello world" | out-file -Encoding ascii ".\hello.cmd"

1Aug/110

PowerShell + GMail SMTP

I'm writing a script that automates some testing process (what that testing process is all about is topic for another day), and one of the things I wanted to do is to have the script send out an email. Unfortunately, when I was writing the script, it never occurred to me that I'll be deploying this script on an EC2 instance which will not have access to my workplace's SMTP server which is sitting behind a firewall.

I tried to set up SMTP service on the server to no avail...I'm sure I'm doing something wrong even though I followed the instructions step-by-step (and I turned off the firewall, too). :-/

So what I did is to use GMail's SMTP server instead. Unfortunately, I couldn't use the PowerShell 2.0's new send-mailmessage command in a straightforward manner because of two things:

- GMail SMTP requires authentication
- GMail SMTP listens on a non-standard port

I know you can specify the credentials using send-mailmessage but I was too lazy to look it up. So I ended up using the SmtpClient .NET object:

$sc = new-object Net.Mail.SmtpClient("smtp.gmail.com", 587);
$sc.EnableSsl = $true;
$cred = New-Object System.Net.NetworkCredential("your-user-name-here-lol@gmail.com", "your-2step-auth-code-password-here");
$sc.Credentials = $cred;

$emsg = new-Object System.Net.Mail.MailMessage($sender, $recipient, $subject, $body);

if ($attachmentpath -ne "")
{
$attachment = new-Object System.Net.Mail.Attachment($attachmentpath)
$emsg.Attachments.Add($attachment)
}

$sc.Send($emsg);

There are a couple of things you need to do:

- Enable SSL
- Use 587 as port
- If you are using Google's 2-step verification feature, you need to create a password for this script; otherwise, just use your GMail password.

The rest of the parameters/variables should be self-explanatory.

Filed under: .NET, PowerShell No Comments
26Jul/110

Visual Studio 2010 + SilverLight CRASH LOL

If you find yourself not being able to load your Silverlight projects in Visual Studio 2010 after in spite of being anal about having your system up-to-date (talk about irony), check out this thread.

To make the long story short: unregister Windows.System.dll from the GAC. Yes, I know that doesn't sit with me, either; that DLL looks like a system DLL that's supposed to be in the GAC.

Anyways, in Win7 and Windows2K8, we all know how fantastically annoyingly non-easy it is to remove DLLs in the GAC (read: you can't just delete the DLL from C:\Windows\Assembly); open a Visual Studio command prompt in Admin mode and type:

gacutil -u System.Windows