Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind a custom WSDL to an existing WCF Service

I've created a web service using WCF. I cannot figure out how to set the constraints and the restrictions in the WSDL/XSD through the data contract.

What if I improve the XSD2 schema with the constraints and then bind this custom schema to the existing service? If so, how can I make the service expose the improved WSDL? Otherwise, is there any other way to set the metadata for a WCF service?

like image 572
Alberto De Caro Avatar asked Jun 19 '12 10:06

Alberto De Caro


3 Answers

Please, do not confuse XSD and WSDL - this is totally different things

According your question - try to use RiaServices. It's allow you write such code as

public class Meeting
{
    [Key]
    public int MeetingId { get; set; }

    [Required]
    public DateTime Start { get; set; }

    [Required]
    public DateTime End { get; set; }

    [Required]
    [StringLength(80, MinimumLength = 5)]
    public string Title { get; set; }

    public string Details { get; set; }

    [Required]
    [RegularExpression(@"\d{1,3}/\d{4}",
    ErrorMessage = "{0} must be in the format of 'Building/Room'")]
    public string Location { get; set; }

    [Range(2, 100)]
    [Display(Name = "Minimum Attendees")]
    public int MinimumAttendees { get; set; }

    [Range(2, 100)]
    [Display(Name = "Maximum Attendees")]
    public int MaximumAttendees { get; set; }
}

As I know, the only way to improve your WSDL with constrains and restrictions is mark your DataContract classes with restrict attributes (fix me if it's not true)

like image 162
Andriy Zakharko Avatar answered Nov 12 '22 07:11

Andriy Zakharko


This seems to be a common problem. The service metadata describes the data contract. That is, the structure of the exchanged data, without any validation information.

I have been solving this problem by implementing a validation layer on top of the service layer. It goes as follow:

In addition to a WSDL, the service author and consumer also agree on a refined XSD that describes all the validation details in addition to the mere structure of the data contracts.

Each party (xml) serializes and validates the data contracts against the refined XSD.

Sample "pseudocode" for a service method that validates the request agains an XSD.

public string MyServiceMethod(MyDataType m){
    string s = XmlSerialize(m);
    if( XSDValidate(s) ){
        return ProcessRequest(m);
    }else{
        return BuildErrorResponse("The request is not compliant with the contract");
    }
}

The service consumer can also implement a similar logic to validate the request data before sending it to the server.

like image 1
xtrem Avatar answered Nov 12 '22 07:11

xtrem


You basically have two ways of creating a webservice:

  1. Code first. You create a class and mark it as a datacontract class plus some more attributes. When you compile this the WSDL of your web service will be generated from your class. This is a quick method and often gives you good enough control over the WSDL.

  2. Schema first. You create the WSDL by hand and use a tool (e.g. WSCF.Blue or ) to generate the datacontract class from your WSDL. This will give you complete control over your WSDL schema, but in my experience it is more work to create the WSDL than a datacontract class.

like image 1
GTG Avatar answered Nov 12 '22 06:11

GTG