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 a开发者_如何转开发lso 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?
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:
- Create a web service containing 1 dummy method.
- Subclass the
SoapExtensionReflector
and configure it in web.config. - 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...
I believe it's possible to add a metadata exchange endpoint programmatically in WCF - you may want to look into that. That would allow you to dynamically return WSDL to potential service clients who could query your webservice at runtime to determine which entry points are available. But it's definetely a bit of work - and not for the faint of heart.
Is a dynamic WSDL an absolute requirement? Not having a static WSDL also means you can't have a static (auto-generated) proxy class, which is a real PITA. Instead, you could expose the function signatures as plain old data, rather than as 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 the [DataContract]
and [DataMember]
attributes on the FunctionDescription
and Argument
classes when using a version of .NET earlier than 3.5 SP1.
精彩评论