posts - 76, comments - 26, trackbacks - 0

WCF Contract Constraint Woes

At my day job, we're in the process of moving a sizable list of ASMX+WSE services to Fx3 and WCF (this is the first time I'm looking at WCF beyond playing with the sample).  We're applying several lessons learned in the past and taking time to think through how we're implementing our service contracts.  One of the great-great things about WCF is developers now define their contracts in code, and use tools to export the XSD Schema.

namespace Brownsberger
{
    [ServiceContract()]
    public interface IProductService
    {
        [OperationContract]
        CreateProductResponse CreateProduct(CreateProductRequest request);
    }

    [MessageContract]
    public class CreateProductRequest { };

    [MessageContract]
    public class CreateProductResponse { };

    [DataContract]
    public class Product
    {
        [DataMember]
        public int Id;
        [DataMember]
        public string Name;
        [DataMember]
        public int CategoryId;
    }
}

Then using the svcutil.exe command line tool I can export this code to XSD Schema and WSDL.

What we'd really want to have rich attributes we can decorate our contracts with - something very similar to the metadata that drives Avanade's ACA.NET Validation Framework:

    [DataContract]
    public class Product
    {
        [DataMember]
        [GreaterThan(0)]
        public int Id;

        [DataMember]
        [LongerThan(0)]
        public string Name;

        [DataMember]
        [GreaterThan(0)]
        public int CategoryId;
    }


There doesn't appear to be any of this in the WCF box, though there are hooks in the ServiceModel to roll all of this myself - as well as just about anything else (impressive extensibility model all the way around).  I did find a sample on netfx3.com that implements this contraints scheme, which is the bulk of what we want to do in code, but of course svcutil.exe doesn't know how to manifest all that metadata into XSD Schema constraints.  WCF gives me all the hooks to override svcutil.exe's behavior and it appear to have the ability to inject whatever I want into the XSD Schema it spits out - awesome.

My guess is alot of people looking to rollout WCF are thinking about this with regard to their contracts.  I don't blame the WCF team for not having this functionality in v1 because frankly, doing so would have been an enormous amount of work.  This smells like something that will be handled internally by WCF in coming versions. 

I'll probably end up hand rolling this, but I'd be curious to hear advice on how to go about doing this.  If there's something that already exists or something in the box that I'm not see, I'd love to know about it.

Print | posted on Wednesday, February 07, 2007 12:00 AM | Filed Under [ microsoft .net development ]

Feedback

Gravatar

# re: WCF Contract Constraint Woes

have you had to use the WCF services with ssl?
9/1/2009 6:20 AM | Ramler
Gravatar

# re: WCF Contract Constraint Woes

Yep - here's a overview of how to do it:

msdn.microsoft.com/en-us/library/ms734679.aspx
9/1/2009 6:27 AM | kellyb

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 6 and type the answer here:

Powered by: