Search code examples
c#soapisapi

writing a web service with dynamically determined web methods


Let's say I have a text file of basic mathematical functions.

I want to make a web service that answers these mathematical functions. Say the first one is y=x*x. If I wanted to turn this into a web service, I could simply do this:

[WebMethod]
public int A(int x)
{
  return x*x;
}

However, I've extracted the function from the list by hand and coded it into a function by hand. That's not what I want to do. I want the wsdl for the service to be generated at call time directly from the text file, and I want the web method calls to the service to go to a specific method that also parses the text file at run time.

How much heavy lifting is this? I've found a sample on how to generate WSDLs dynamically at this link, but there's a lot more to do beyond that and I don't want to bark up this tree if there are parts of the project that arn't feasible. Does anyone have any links, guides, books, or positive experiences trying this kind of thing?


Solution

  • This related StackOverflow question post might give you a lead.

    The tip here is to use the SoapExtensionReflector class.

    As I see it, you might be able to use that class as follows:

    1. Create a web service containing 1 dummy method.
    2. Subclass the SoapExtensionReflector and configure it in web.config.
    3. As soon as your subclass is called for the dummy method, read the file with functions and dynamically add a method to the WSDL file for each function.

    As you might agree, this sounds easier than it is, and I would personally prefer not to go there at all. The resulting code will probably be a bit of a maintenance nightmare.

    Good luck :)

    EDIT: it might actually be easier to write a little code generator, which generates the C# web service code from your file with functions. Then, let the WSDL generation be up to the framework you are using (e.g. WCF).

    Obviously, this kind of kills the whole dynamic aspect of it + you would need to redeploy after ever change in the functions file. But then again, the cycle of 'generate code - build - redeploy' could easily be automated with some MSBuild tasks.

    I guess the usefulness of such a solution depends on how often your file with functions changes...