C# – Defining a Web Service Endpoint via Configuration instead of Code

asp.netcweb services

I'm interested in creating a web service endpoint in C#/.NET that is fully configurable. To keep it simple assume there is no GET/query/retrieval requirement. I just need some guidance on how to get started here.

The goal is to have a a core solution built, tested, and deployed that can then be configured for new objects thus avoiding a requirement to modify, build, and deploy the entire solution anytime an object is added/removed/modified. This configuration would have to support versioning given the environment it will operate in.

Assume we have a configuration file such as the following:

{
  "V1": {
    "Person": [
      {
        "Name": "FirstName",
        "Required": true,
        "Type": "string",
        "MaxLength": 25
      },
      {
        "Name": "Lastname",
        "Required": true,
        "Type": "string",
        "MaxLength": 25
      },
      {
        "Name": "Title",
        "Required": false,
        "Type": "string",
        "MaxLength": 50
      }
    ]
  },
  "V2": {
    "Person": [
      {
        "Name": "FirstName",
        "Required": true,
        "Type": "string",
        "MaxLength": 25
      },
      {
        "Name": "Lastname",
        "Required": true,
        "Type": "string",
        "MaxLength": 25
      },
      {
        "Name": "Title",
        "Required": false,
        "Type": "string",
        "MaxLength": 50
      },
      {
        "Name": "DateOfBirth",
        "Required": false,
        "Type": "DateTime"
      }
    ]
  }
}

With this configuration I want an application that will expose an endpoint for a caller to send an object and validate the object against the type rules. It should also expose a WSDL.

To be clear, I do not want to write a simple single generic endpoint that can take an array of key-value attributes and then validate – I want to generate a contract.

In essence, I'm trying to have a contract web service while not having to create POCOs. This will allow senders to more easily mock the endpoint for their development work.

For the curious, the incoming data will be sent to a messaging bus to be processed by other services. Thus, the endpoint needs to do the evaluation of type/length/etc but I don't want to create/update the POCOs and controllers along with all the testing and headaches of an enterprise deployment simply because some object has a new field.

Best Answer

The traditional way to do this is to define the types in a XSD file and define the service contract in a WSDL file that references the XSD files. You're doing pretty much the same thing in a bespoke JSON format, which is fine if you want to do the work, but you won't find there are a lot of tools out there to help you.

One problem you will quickly discover is that, while it is fairly trivial to define an interface from configuration or markup, that doesn't get you very far, because you will still need code to handle service calls. Since you say you don't want to work with tag/value pairs, that means your code will need to be strongly typed, meaning that a generalized solution is not possible.

I suppose you could build something that will dynamically generate code to translate from the strongly-typed service message to a general format c# data structure. Which sounds an awful lot like the tag/value sort of solution you were avoiding. So you're not really solving a problem, just moving it, albeit you will move it from the public interface to private code.