开发者

Dynamic WSDL Implementation in C#

I am relatively new to web services in .Net, so I apologize if this is a newbie sort of question.

I have found numerous resources on how to implement web services in a .Net project, but all of them seem to involve static definition of the exposed methods. While this m开发者_开发知识库ay be suitable for some applications, it does not fit the bill for what I'm needing.

What I'm wondering is, is there a way to dynamically implement the methods exposed in a WSDL, such as the way the PHP SoapClient does it?


The WSDL really is there just to help define the method names and params, and to mask all the HTTP headers for you, the developer.

Sounds like you're wanting to supply the method name and params at runtime. Perhaps a way around this is to completely forget about the WSDL altogether. Make your own HTTP calls.

  • store your expected methods and SOAP endpoint URLs in .config. At runtime, find and loop through the web method's param list, and supply the values.

  • use the HttpWebRequest and add the headers as required. Here's an article doing the same thing - Invoking Web Service dynamically using HttpWebRequest.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
      "http://foo.com/Some.asmx"); //build string from .config instead.

//here Register is the name of the method. take yours from config as well if needed
req.Headers.Add("SOAPAction", "\"http://tempuri.org/Register\"");

req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";

See the article Invoking Web Service dynamically using HttpWebRequest for full details.

The thing about .NET's web service wrapper classes in general, is that they're trying to help enforce the server's contracts with the client. Obviously you're trying to avoid changing the statically typed clients, as presented by the .NET wrapper classes. Hopefully it won't be too onerous for you to maintain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜