Dan Rigsby - Coding Up Style

.Net, C#, & Wcf Development

Archive for January, 2008

Wcf Binding Comparision List and Supported Features (Reference)

Posted by Dan Rigsby on 19th January 2008

I have been working pretty heavily with Wcf over the last couple of weeks and really dived into understanding the ABC’s (Addressing, Binding and Contracts) of Wcf. If you dont already know, Addressing and Contracts are very straight forward. An Address is the uri string and the contract is the name of the interface that the endpoint implements. Binding is a quite a bit more difficult to understand and get right. There are a number of different binding, configuration settings, and behaviors to wrap your arms around. I put together a little “Cheatsheet” reference to help me understand each binding and when to use each. This data is primarily taken from http://msdn2.microsoft.com/en-us/library/ms731092.aspx with quite a bit of additions.

Name (Config Name) [Schemas] Transport Message Encoding Message Version Interop Security Session Tx Flow Duplex
BasicHttpBinding
(basicHttpBinding)
[http, https]
HTTP/HTTPS

Text

SOAP 1.1

Basic Profile 1.1 None, Transport, Message, Mixed N
A binding that is suitable for communicating with WS-Basic Profile conformant Web services like ASP.NET Web services (ASMX)-based services.
WSHttpBinding
(wsHttpBinding,
webHttpBinding)
[http, https]
HTTP/HTTPS Text, MTOM

SOAP 1.2, WS-A 1.0

WS None, Transport, Message, Mixed None, Transport, Reliable Session No, Yes (WS-AT) N
A secure and interoperable binding that is suitable for non-duplex service contracts.
WS2007HttpBinding
(ws2007HttpBinding)
[http, https]
HTTP/HTTPS Text, MTOM SOAP 1.2, WS-A 1.0 WS-Security, WS-Trust, WS-SC, WS-SP None, Transport, Message, Mixed None, Transport, Reliable Session No, Yes (WS-AT) N
WSDualHttpBinding
(wsDualHttpBinding)
[http]
HTTP Text, MTOM

SOAP 1.2, WS-A 1.0

WS None, Message Reliable Session No, Yes (WS-AT) Y
A secure and interoperable binding that is suitable for duplex service contracts or communication through SOAP intermediaries.
WSFederationHttpBinding
(wsFederationHttpBinding)
[http, https]
HTTP/HTTPS Text, MTOM

SOAP 1.2, WS-A 1.0

WS-Federation None, Message, Mixed None, Reliable Session No, Yes (WS-AT) N
A secure and interoperable binding that supports the WS-Federation protocol, enabling organizations that are in a federation to efficiently authenticate and authorize users.
WS2007FederationHttpBinding
(ws2007FederationHttpBinding)
[http, https]
HTTP/HTTPS Text, MTOM

SOAP 1.2, WS-A 1.0

WS-Federation None, Message, Mixed None, Reliable Session No, Yes (WS-AT) N
A secure and interoperable binding that derives from WS2007HttpBinding and supports federated security.
NetTcpBinding
(netTcpBinding)
[net.tcp]
TCP Binary

SOAP 1.2

.Net None, Transport, Message, Mixed Transport, Reliable Session No, Yes (OleTx) Y
A secure and optimized binding suitable for cross-machine communication between WCF applications.
NetPeerTcpBinding
(netPeerTcpBinding)
[net.p2p]
P2P Binary

SOAP 1.2

Peer None, Transport, Message, Mixed Y
A binding that enables secure, multi-machine communication.
NetNamedPipeBinding
(netNamedPipeBinding)
[net.pipe]
Named Pipe (IPC) Binary

SOAP 1.2

.Net None, Transport None, Transport No, Yes (OleTx) Y
A secure, reliable, optimized binding that is suitable for on-machine communication between WCF applications.
NetMsmqBinding
(netMsmqBinding)
[net.msmq]
MSMQ Binary

SOAP 1.2

.Net None, Transport, Message, Both No, Yes (OleTx) N
A queued binding that is suitable for cross-machine communication between WCF applications.
MsmqIntegrationBinding
(msmqIntegrationBinding)
MSMQ * MSMQ None, Transport No, Yes N
A binding that is suitable for cross-machine communication between a WCF application and existing MSMQ applications.

*Doesn’t use a WCF message encoding – instead it lets you choose a pre-WCF serialization format
Notes: Items in bold are the defaults for features that have multiple values. “–“ = No Support
Abbreviations: WS-SC = WS-SecureConversation, WS-SP = WS-SecurityPolicy, WS-A = WS-Addressing, WS-AT = WS-AtomicTransaction, OleTx = OleTransactions

Feature Description
Name The name of the binding.
Config Name The name of the binding used in configuration such as app.config or web.config.
Scheme The supported Uri schemes.
Transport The supported types of message transport (similar to Providers in Remoting).
Message Encoding The supported types of message encoding.
Message Version The supported message versions.
Interop Names the protocol or technology with which the binding ensures interoperation.
Security Specifies how the channel is secured:

  • None: The SOAP message is not secured and the client is not authenticated.
  • Transport: Security requirements are satisfied at the transport layer.
  • Message: Security requirements are satisfied at the message layer.
  • Mixed: This security mode is known as TransportWithMessageCredentials. It handles credentials at the message level, and integrity and confidentiality requirements are satisfied by the transport layer.
  • Both: Both message level and transport level security are used. This ability is unique to the NetMsmqBinding.
Session Specifies whether this binding supports session contracts.
Transaction Flow Specifies whether transactions are enabled and the transaction flow type in ().
Duplex Specifies whether duplex contracts are supported. Note this feature requires support for Sessions in the binding.

Posted in .Net, Wcf | 1 Comment »

IndyNDA - January 2008, "Programming in the Cloud" by Larry Clarkin

Posted by Dan Rigsby on 13th January 2008

This past Thursday (January 10, 2008) Larry Clarkin (our local Microsoft Architect Evangelist) came down for our monthly Indianapolis .Net Developers Association (IndyNDA) meeting.  His topic was “Programming in the Cloud - Tapping into Internet APIs”.  Here was the description given for the event:

Services are becoming increasingly important in software development today.  The Internet has numerous interesting APIs that you can “tap” into as part of your Internet and enterprise based applications.  In this presentation Larry Clarkin, an Architect Evangelist for Microsoft, will explore a number of these APIs and how to consumer them in your application.  We will look at some popular APIs like Virtual Earth, Twitter, Facebook and Flickr and what it takes to start using these APIs.  We will also plug into them with a variety of UX (User Experience) platforms like HTML Pages, Silverlight and WPF. 

The IndyNDA president, Brad Jones, was out of town this month.  The vice-president, Bob Walker, took over the event with the help of Dave Leininger and myself. 101 people were in attendance which is about 10% more than usual and is high considering that this wasn’t a “special engagement” nor were there “big” prizes.

Larry did a good job covering the gambit of what is possible with web APIs. I really like that more and more people at Microsoft are open to talk about other companies and competitors in a positive light.  The biggest threat to Microsoft has always been Microsoft itself.  Working with other companies, sharing ideas, and showing love to competitors is showing that they are not as close minded as they once were.  Below is a run down of the highlights of the meeting:

Upcoming Events:

  1. MSDN Event - What’s New in IIS7 and Extending ASP.NET 2.0 Application Services
  2. Thursday, February 07, 2008 1:00 PM - 5:00 PM Central (Why in Central time?)

  3. ArcReady - Software Lifecycle Management
  4. Thursday, February 21, 2008 9:00 AM - 11:45 AM Eastern

Plugs:

  1. ThirstyDeveloper - A podcast from by Larry Clarkin and Dave Bost.  They are doing a good job with this.

Topics:

  1. VirtualEarth Demo - Showed off some 3D maps of Indianapolis and covered major competitors Google and Yahoo.  There was a demo with Wpf consuming VirtualEarth data.
  2. Soap vs Rest - Microsoft is now embracing Rest. Soap is still a very important technology but is used more for internal needs.  Rest is best suited for “the cloud”, especially where security is of no consequence.
  3. Facebook - There wasn’t much talk here, but the Facebook Developer Toolkit was mentioned.  If you are interested in developing a Facebook application this is a good place to start.
  4. Twitter Demo - I was amazed to see how many people didn’t use or had never heard of twitter.  Larry spent a good chunk of time giving an introduction to what twitter was and how it could be used.  This spawned a number of smaller discussions about using twitter for other things such as a content management system and feeding other applications.
  5. Silvertwitter - This is a twitter reader written by Dave Bost in Silverlight.  He hasn’t posted the source or the application yet, but you can see it in use on his blog.
  6. Rss - There was a small discussion about Rss and why it is the best thing to happen to the Internet.  While Atom is a superior format, Rss is well known and well adopted.
  7. Flickr - There was a demo using Blend and Wpf to create a simple flickr viewer.  FlickerNet was also mentioned.

After the meeting some of the IndyNDA officers and sponsors got together for a couple of drinks with Larry.  I love these outings as they really give us a chance to discuss things going on at Microsoft and open the doors for a lot of good questions and discussions.

(From left to right: Jon Cooper, Aaron Lerch, Paul Hacker, Larry Clarkin, Dan Rigsby, Bob Walker, and Mark McClellan.)

Posted in Community | 3 Comments »

Enterprise Library 4.0 Product Backlog Available

Posted by Dan Rigsby on 9th January 2008

It looks like the Patterns and Practices team at Microsoft has exposed the product backlog for Enterprise Library 4.0 out on codeplex.  The major new item is the Dependency Injection Application Block (DIAB) which is geared up as a more complete alternative to Spring.Net, StructureMap, Castle Windsor, and other dependency injection frameworks.  The nice thing about this is that the base implementation of DIAB can be swapped out with one of the other engines to allow a lot more flexibility. It will  support for containers and both declarative and imperative configuration.  Jeremy Miller gives a much better introduction to DIAB than I ever could. It will be interesting to watch the progress they make with this and what will happen to ObjectBuilder which is already a “lightweight” dependency injection framework that is used in Enterprise Library and the Composite UI Application Block (CAB). We use EntLib 3.1 and CAB extensively, so we have a vested interest in this.

Here are a few other items in the list that I am interested in:

  1. Provide extensibility points for Caching Block to allow alternate caching implementations (including distributed cache providers) to be plugged in. The objective here is NOT to replace the ASP.NET caching but to provide more choices.
  2. Improve performance of the Logging Block by optimizing text formatter (lazy formatting in case of the local logger, and eager formatting in case of the remote logger).
  3. Provide more descriptive error messages.
  4. LAB: allow finer-grained local filters in the config tool.
  5. Hands-on Labs: Validation Block, Policy Injection Block, Dependency Injection Block.
  6. Allow to run multiple rulesets in Validation block.

Posted in .Net, Design Patterns | No Comments »

How to Drop All Database Objects Beginning with a Prefix

Posted by Dan Rigsby on 8th January 2008

We prefix all of our database objects with a namespace scope.  Objects begin with "ININ_" followed by an application prefix.  So all database objects relating to to an application called "ABC" will begin with the prefix "ININ_ABC_".  This namespacing really helps out when multiple applications share the same database.

Advantages of this approach include:

  1. Identifying what application an object belongs to
  2. Finding all objects for an application in the sysobject tables

But, this post isn’t about how to name database objects…

To test our database scripts, we normally drop a database and recreate through our build process. However, when multiple applications share the same database, we need to only drop the database objects relating to that application. 

The stored procedure below takes in an application prefix and drops all database objects whose name starts with that prefix.  This script will work for any prefix you might use to segregate your objects. 

This was written for SQL Server 2005, but it could be easily modified to work with older versions.

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N‘DropApplication’) AND OBJECTPROPERTY(id, N‘IsProcedure’) = 1)
    DROP PROCEDURE [dbo].[DropApplication]
GO

CREATE PROCEDURE [dbo].[DropApplication]
(
    @ApplicationPrefix varchar(64)
)
/************************************************************/
– DropApplication

– Date Created: 8/19/2007
– Created By:     Dan Rigsby (www.danrigsby.com)

– Purpose:         Deletes all of the sysobjects in the database
–                 for an application based on sysobject name prefix

– Example:         To delete all sysobjects for an application called
–                 ISupport whose object are prefixed with ‘ININ_ABC’ call: 
–                 EXEC DropApplication ‘ININ_ABC’
/************************************************************/

AS
BEGIN
    DECLARE @Statement AS varchar(4000)
    DECLARE @ParentID AS int
    DECLARE @ReferenceID AS int
    DECLARE @ParentName AS varchar(256)
    DECLARE @ObjectName AS varchar(256)
    DECLARE @ObjectType AS varchar(5)

    – Step 1: Delete procedures
    DECLARE cr1 CURSOR FOR
    (
        SELECT [name]
        FROM sysobjects
        WHERE [name] LIKE @ApplicationPrefix + ‘%’ AND [type] in (N‘P’, N‘PC’, N‘X’, N‘RF’)
    )

    OPEN cr1
    FETCH FROM cr1 INTO @ParentName
    WHILE (@@fetch_status = 0)
    BEGIN
        EXEC (‘DROP PROCEDURE ‘ + @ParentName)
        FETCH NEXT FROM cr1 INTO @ParentName
    END
    CLOSE cr1
    DEALLOCATE cr1

    – Step 2: Delete functions
    DECLARE cr1 CURSOR FOR
    (
        SELECT [name]
        FROM sysobjects
        WHERE [name] LIKE @ApplicationPrefix + ‘%’ AND [type] in (N‘IF’, N‘FN’, N‘TF’)
    )

    OPEN cr1
    FETCH FROM cr1 INTO @ParentName
    WHILE (@@fetch_status = 0)
    BEGIN
        EXEC (‘DROP FUNCTION ‘ + @ParentName)
        FETCH NEXT FROM cr1 INTO @ParentName
    END
    CLOSE cr1
    DEALLOCATE cr1

    – Step 3: Delete views
    DECLARE cr1 CURSOR FOR
    (
        SELECT [name]
        FROM sysobjects
        WHERE [name] LIKE @ApplicationPrefix + ‘%’ AND [type] in (N‘V’)
    )

    OPEN cr1
    FETCH FROM cr1 INTO @ParentName
    WHILE (@@fetch_status = 0)
    BEGIN
        EXEC (‘DROP VIEW ‘ + @ParentName)
        FETCH NEXT FROM cr1 INTO @ParentName
    END
    CLOSE cr1
    DEALLOCATE cr1

    – Step 4: Delete FKs
    DECLARE cr1 CURSOR FOR
    (
        SELECT [id], [name]
        FROM sysobjects
        WHERE [name] LIKE @ApplicationPrefix + ‘%’ AND [type] in (N‘U’)
    )

    OPEN cr1
    FETCH FROM cr1 INTO @ParentID, @ParentName
    WHILE (@@fetch_status = 0)
    BEGIN
        DECLARE cr2 CURSOR FOR
        (
            SELECT [name]
            FROM sysobjects
            WHERE [parent_obj] = @ParentID AND [type] IN (N‘F’)
        )

        OPEN cr2
        FETCH FROM cr2 INTO @ObjectName
        WHILE (@@fetch_status = 0)
        BEGIN
            EXEC (‘ ALTER TABLE ‘ + @ParentName +
                ‘ DROP CONSTRAINT ‘ + @ObjectName)

            FETCH NEXT FROM cr2 INTO @ObjectName
        END
        CLOSE cr2
        DEALLOCATE cr2

        FETCH NEXT FROM cr1 INTO @ParentID, @ParentName
    END
    CLOSE cr1
    DEALLOCATE cr1

    – Step 5: Delete any FK constraints in other tables that reference our tables
    DECLARE cr1 CURSOR FOR
    (
        SELECT [id]
        FROM sysobjects
        WHERE [name] LIKE @ApplicationPrefix + ‘%’ AND [type] in (N‘U’)
    )

    OPEN cr1
    FETCH FROM cr1 INTO @ReferenceID
    WHILE (@@fetch_status = 0)
    BEGIN
        DECLARE cr2 CURSOR FOR
        (
            SELECT [name], [type], [parent_object_id]
            FROM sys.foreign_keys
            WHERE [referenced_object_id] = @ReferenceID
        )

        OPEN cr2
        FETCH FROM cr2 INTO @ObjectName, @ObjectType, @ParentID
        WHILE (@@fetch_status = 0)
        BEGIN
            – Get parent name
            SELECT @ParentName = [name]
            FROM sysobjects
            WHERE [id] = @ParentID

            SELECT @Statement =
                CASE (@ObjectType)
                    WHEN ‘F’ THEN
                        ‘ ALTER TABLE ‘ + @ParentName +
                        ‘ DROP CONSTRAINT ‘ + @ObjectName
                END
            EXEC (@Statement)

            FETCH NEXT FROM cr2 INTO @ObjectName, @ObjectType, @ParentID
        END
        CLOSE cr2
        DEALLOCATE cr2

        FETCH NEXT FROM cr1 INTO @ReferenceID
    END
    CLOSE cr1
    DEALLOCATE cr1

    – Step 6: Delete tables, triggers, FKs, and constraints
    DECLARE cr1 CURSOR FOR
    (
        SELECT [id], [name]
        FROM sysobjects
        WHERE [name] LIKE @ApplicationPrefix + ‘%’ AND [type] in (N‘U’)
    )

    OPEN cr1
    FETCH FROM cr1 INTO @ParentID, @ParentName
    WHILE (@@fetch_status = 0)
    BEGIN
        DECLARE cr2 CURSOR FOR
        (
            SELECT [name], [type]
            FROM sysobjects
            WHERE [parent_obj] = @ParentID
        )

        OPEN cr2
        FETCH FROM cr2 INTO @ObjectName, @ObjectType
        WHILE (@@fetch_status = 0)
        BEGIN
            SELECT @Statement =
                CASE (@ObjectType)
                    WHEN ‘TR’ THEN
                        ‘ DROP TRIGGER ‘ + @ObjectName
                    ELSE
                        ‘ ALTER TABLE ‘ + @ParentName +
                        ‘ DROP CONSTRAINT ‘ + @ObjectName
                END
            EXEC (@Statement)

            FETCH NEXT FROM cr2 INTO @ObjectName, @ObjectType
        END
        CLOSE cr2
        DEALLOCATE cr2

        EXEC (‘ DROP TABLE ‘ + @ParentName)

        FETCH NEXT FROM cr1 INTO @ParentID, @ParentName
    END
    CLOSE cr1
    DEALLOCATE cr1

    – Step 7: Drop users
    DECLARE cr1 CURSOR FOR
    (
        SELECT [name]
        FROM sys.database_principals
        WHERE [name] LIKE @ApplicationPrefix + ‘%’
            AND [type] IN (N‘S’)
    )

    OPEN cr1
    FETCH FROM cr1 INTO @ObjectName
    WHILE (@@fetch_status = 0)
    BEGIN
        EXEC (‘DROP USER ‘ + @ObjectName)
        FETCH NEXT FROM cr1 INTO @ObjectName
    END
    CLOSE cr1
    DEALLOCATE cr1

    – Step 8: Drop roles
    DECLARE cr1 CURSOR FOR
    (
        SELECT [principal_id], [name]
        FROM sys.database_principals
        WHERE [name] LIKE @ApplicationPrefix + ‘%’
            AND [type] in (N‘R’)
    )

    OPEN cr1
    FETCH FROM cr1 INTO @ParentID, @ParentName
    WHILE (@@fetch_status = 0)
    BEGIN
        DECLARE cr2 CURSOR FOR
        (
            SELECT [name]
            FROM sys.database_role_members DRM
                INNER JOIN sys.database_principals DP
                    ON DRM.[member_principal_id] = DP.[principal_id]
            WHERE [role_principal_id] = @ParentID
        )

        OPEN cr2
        FETCH FROM cr2 INTO @ObjectName
        WHILE (@@fetch_status = 0)
        BEGIN
            EXEC
            (‘
                EXEC dbo.sp_droprolemember ‘ + @ParentName + ‘, ‘ + @ObjectName + ‘
            ‘)
            FETCH NEXT FROM cr2 INTO @ObjectName
        END
        CLOSE cr2
        DEALLOCATE cr2

        EXEC (‘DROP ROLE ‘ + @ParentName)

        FETCH NEXT FROM cr1 INTO @ParentID, @ParentName
    END
    CLOSE cr1
    DEALLOCATE cr1
END
GO

Database objects are dropped in the following order:

  1. Procedures
  2. Functions
  3. Views
  4. Foreign Keys
  5. Foreign Key constraints in other tables
  6. Triggers and Constraints
  7. Tables
  8. Users
  9. Roles

Posted in Database, MSSQL | No Comments »

Preserving line breaks in xml while transforming to html with xslt

Posted by Dan Rigsby on 3rd January 2008

When using xslt to transform xml into html, sometimes it may be desired to keep the line breaks in the text of the xml and convert them into <br/> tags.  For instance, we pipe the output of build and database scripts to xml.  When rendering into html, we want to keep the line breaks or the text looks garbled.  There are many other situations where you might want to preserve line breaks, but bare with me.

Below I have some text copied from an execution of msbuild.  This text is stored directly into a <text> node in the xml:

<text>Microsoft (R) Build Engine Version 3.5.21022.8
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 1/2/2008 6:34:50 PM.
Done Building Project "D:\builds\ININ.BCF.sln" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)</text>

A simple xslt to convert this into html might look like this:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/TR/xhtml1/strict">
    <xsl:output method="html"/>

    <xsl:template match="/">
        <xsl:value-of select="/text"/>
    </xsl:template>
</xsl:stylesheet>

The problem with the xslt is that the line breaks aren’t preserved and as a result, the text looks garbled:

Microsoft (R) Build Engine Version 3.5.21022.8 [Microsoft .NET Framework, Version 2.0.50727.1433] Copyright (C) Microsoft Corporation 2007. All rights reserved. Build started 1/2/2008 6:34:50 PM. Done Building Project "D:\builds\ININ.BCF.sln" (default targets). Build succeeded. 0 Warning(s) 0 Error(s)

Here is the same xslt with the inclusion of a PreserveLineBreaks template:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/TR/xhtml1/strict">
    <xsl:output method="html"/>

    <xsl:template match="/">
        <xsl:call-template name="PreserveLineBreaks">
            <xsl:with-param name="text" select="/text"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="PreserveLineBreaks">
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="contains($text,’&#xA;’)">
                <xsl:value-of select="substring-before($text,’&#xA;’)"/>
                <br/>
                <xsl:call-template name="PreserveLineBreaks">
                    <xsl:with-param name="text">
                        <xsl:value-of select="substring-after($text,’&#xA;’)"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

The template preserves line breaks by writing out the text before the line break, adding a <br/> tag, then recursively calling the template again to look for addition line breaks in the remaining text.  This process continues until every line break is essentially replaced with a <br/>.

Microsoft (R) Build Engine Version 3.5.21022.8

[Microsoft .NET Framework, Version 2.0.50727.1433]

Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 1/2/2008 6:34:50 PM.

Done Building Project "D:\builds\ININ.BCF.sln" (default targets).

Build succeeded.

0 Warning(s)

0 Error(s)

This template could even be modified to replace other characters such as tabs depending on your needs.

Posted in xslt | 3 Comments »