Monday, April 21, 2008

Can't compile Linq after VS 2008 migration

I migrated a VS 2005 asp.net website (not using Linq) to VS 2008. Initially, the website compiled fine, but once I tried to add Linq statements, it would not compile.

I had already done the following to prepare for Linq use:

1) Added in the web.config so System.Core (where Linq lives) would be a reference (copied several of these from a virgin VS 2008 asp.net site)

2) Modified the site Build properties and targeted .NET 3.5

3) Added "using System.Linq"

But it still would not compile...all Linq statements were not recognized (even Intellisense worked). It was like I was still using the .NET 2.0 compiler.

When I looked back at the web.config from a virgin VS 2008 asp.net site, I realized I needed to also have the section. I copied this entire section into my web.config and it now compiles fine (with the .3.5 compiler).

It would have been nice if the conversion tool from VS 2005 to 2008 would have done this for me. I have the first official release of VS 2008.

Saturday, March 29, 2008

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.

Description:
When you try to access a WCF service hosted in IIS, you get the following error:

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.

Context:
You have a website hosted in IIS and the site has multiple host headers defined for the site (e.g. www.domain.com, domain.com).

Fix:
There doesn’t appear to be a nice workable solution for this as of VS 2008 initial release on .NET 3.5. The only “hack” found out there is to define a new Factory for the service.

Step 1:

namespace Foo
{
public class CustomHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(
Type serviceType,
Uri[] baseAddresses)
{
// Specify the exact URL of your web service from the config file:
// e.g. http://www.domain.com/service/myservice.svc
Uri webServiceAddress =
new Uri(ConfigurationManager.AppSettings["ServiceUri"]);

ServiceHost webServiceHost =
new ServiceHost(serviceType, webServiceAddress);

return webServiceHost;
}
}
}

Some web articles had you also creating a new host derived from ServiceHost, but that is not needed.

Step 2:
Modify the .svc file on the site hosting the service (not the .svc in Visual Studio):

<%@ ServiceHost Service="Foo.Service1" Factory="Foo.CustomHostFactory" %>

The above .svc content may also include the language and debug specifiers.

Can't add web reference in VS 2008 due to computer name being used for schemaLocation

Description:
You can’t add a web reference using Visual Studio 2008 for a WCF service since the WSDL is using the computer name rather than the service address. If you develop using "localhost" as the service name this is NOT an issue.

Context:
You have a computer/website with more than one IP address and are developing a WCF web service on one of the extra IP addresses (Site A).

Issue:
When you use Visual Studio 2008 to add a reference to a web service on Site A, you will get an error something like:

The document at the url http://192.168.0.54:8000/abcService.svc was not
recognized as a known document type.
The error message from each known type may help you fix the problem:
- Report from 'WSDL Document' is 'The document format is not recognized (the
content type is 'text/html; charset=utf-8').'.
- Report from 'DISCO Document' is 'There was an error downloading
'http://192.168.0.54:8000/abcService.svc?disco'.'.
- The request failed with HTTP status 404: Not Found.
- Report from 'XML Schema' is 'The document format is not recognized (the
content type is 'text/html; charset=utf-8').'.

If you look manually enter the service url in a browser for the service at the IP address (e.g. 192.168.0.54), you will see in the XML WSDL that it is trying to import schemas (schemaLocation) using the computer name, not the service address (192.168.0.54).

Fix:
I fixed this by just making sure I entered the IP address as the host header in IIS. Then the IP address was used rather than the computer name for all WSDL url references.

Thursday, January 3, 2008

Microsoft SQL, stored procedures, optional parameters

Here is a good article on the pros/cons and performance considerations of using optional parameters in SQL stored procedures:

http://www.sommarskog.se/dyn-search.html

Calling Web Services from SQL 2005

Here is an article on how to call Web Services from SQL 2005. The article addresses SQL triggers, but the same approach can be used for user-defined functions or stored procedures.

http://www.codeproject.com/KB/database/SQLCLR.aspx

Monday, December 10, 2007

Powershell Cmdlet and Alias listing

Generate a list of PowerShell Cmdlets:

"Name`tSynopsis`tDescription`tFile"
ls -recurse $PSHOME *-Help.xml | foreach {
$fileName = $_.Name
$help = [xml](gc $_.fullName)
$help.helpitems.command | foreach {
write-output ([string]::format("{0}`t{1}`t{2}`t{3}",$_.details.name.trim(),$_.details.description.get_InnerText().trim(),$_.description.get_InnerText().trim().replace("`n", " "),$fileName))
}
}

Generate a list of Aliases:

"Name`tDefinition"
get-alias | sort -property Name | select Name, Definition |% {
write-output ([string]::format("{0}`t{1}", $_.Name,$_.Definition))
}

Each output is tab delimited.

Sunday, December 2, 2007

Generate .NET Guid

To generate a .NET guid in PowerShell:

Write-Host ([System.Guid]::NewGuid())

Or to generate a bunch of guids:

1..30 |% { Write-Host ([System.Guid]::NewGuid()) }

Counting # of matching lines in file

I wanted to scan a large number XML files and determine which ones had more than 20 elements of a particular type. Here was the PowerShell command-line I used to report on files that contains more than 20 "" elements:

gci *.xml |% {$fn=$_.name; gc $_ | where {$_ -match ""} |% {$count = 0}{++$count}{if ($count -gt 20) {$fn}}}

An easier, but not very good for performance if a large number of files are involved is:

gci f_*.xml | select-string "" | group filename | where {$_.count -gt 20} | select count, name

Tuesday, November 6, 2007

PowerShell to report top 10 results from log file

Ever want to sift through a log file and report on the top 10 occurances of a certain field value? Here is a PowerShell script that will do just that.

I had a log file that contained an IP address in the third field (column index 2 since arrays start at zero in PowerShell). I wanted to know what were the top 10 IPs that were logs. I could call this script like:

./ipcount.ps1 logfile.log 2

When using huge log files, don't forget if you want to redisplay, but not recompute, the results, you can "dot source" the script like:

. ./ipcount.ps1 logfile.log 2

Then $result will always hold the last set of results. Here is the script (3 lines...middle line is really long):

param($file,$index)

$result = gc $file | foreach {$hash=@{}}{$hash[$_.split(',')[[int]$index]] += 1}{$hash.getenumerator()} | sort value -desc | select -first 10

$result

Friday, November 2, 2007

URLs and System.IO.Path

Cool. I just found out that the .NET System.IO.Path static methods like GetFilename and GetDirectory work with internet URLs as well. I was wanting to get just the file name from a URL and the System.Uri class doesn't do this...but Path does. Now ain't that slick.

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...