Create a web service using dynamically defined web methods

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

I want to make a web service that responds to these math functions. Let's say the first one is y = x * x. If I wanted to turn this into a web service, I could just do this:

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

      

However, I pulled the function out of the list manually and coded it into a function manually. This is 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 to invoke a call to a specific method that parses the text file at runtime as well.

How hard is the climb? I found a sample on how to generate WSDL dynamically on this link , but there is a lot more to do this, and I don't want to bark this tree if there are parts of the project that cannot be done. Does anyone have any links, guides, books or positive experiences with this kind of thing?

+2


source to share


3 answers


This question fooobar.com/questions/730962 / ... might give you an edge.

You can use the SoapExtensionReflector class here .

As I see it, you can use this class like this:

  • Create a web service containing 1 dummy method.
  • Subclass SoapExtensionReflector

    and configure it in web.config.
  • Once your subclass is called for the dummy method, read the function file and dynamically add the method to the WSDL file for each function.

As you might agree, it sounds simpler than it is, and I personally would rather not go there at all. The resulting code is likely to be a bit of a maintenance nightmare.



Good luck :)

EDIT: It might actually be easier to write a small code generator that generates C # web service code from your function file. Then let the WSDL generation match the framework used (e.g. WCF).

Obviously this kind kills all the dynamic aspect of this +, you will need to re-install after changing the functions file. But again, the "generate code - build - relocate" cycle can be easily automated with some MSBuild tasks.

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

+3


source


I believe it's possible to add a metadata exchange endpoint programmatically to WCF - you might want to see what. This will allow you to dynamically return the WSDL to potential service clients who might request your web service at runtime to determine which entry points are available. But this is definitely a bit of work - and not for the faint of heart.



+1


source


Is dynamic WSDL an absolute requirement? Having no static WSDL also means you cannot have a static (auto-generated) proxy class, which is a real PITA. Instead, you can expose function signatures as plain old data rather than WSDL metadata:

[ServiceContract]
public interface IMathFunctions
{
  [OperationContract]
  FunctionDescription[] GetFunctionList();

  [OperationContract]
  object RunFunction(string funcName, object[] args);
}

public class FunctionDescription
{
  string Name { get; set; }
  Argument[] Arguments { get; set; }
  TypeCode ReturnType { get; set; }
}

Public class Argument
{
  String Name { get; set; }
  TypeCode Type { get; set; }
}

      

You will need to use attributes [DataContract]

, and [DataMember]

in classes FunctionDescription

and Argument

when using earlier versions of the .NET 3.5 SP1.

+1


source







All Articles