动态读取Web服务方法
有没有办法使用程序动态读取 Web 服务方法?我有一个 Windows 窗体应用程序,应该能够读取方法列表并显示它们。我已向我的项目添加了服务引用,但需要帮助来读取 Web 方法或操作 (WCF) 的列表。
答案:
这是一段代码,以防万一有人正在寻找它。
MethodInfo[] methods = typeof(MyClass).GetMethods(BindingFlags.Public | BindingFlags.Instance);
if (methods != null && methods.Length > 0)
{
foreach (MethodInfo m in methods)
{
foreach (object o in m.GetCustomAttributes(false))
{
// To identify the method
if (o.GetType().Name.Equals("SoapDocumentMethodAttribute"))
{
// Get Name using m.Name
}
}
}
}
Is there a way to read the web service methods dynamically using a program? I have a windows forms app that should be able to read the list of methods and display them. I have added a service reference to my project but need help to read the list of web methods or operations (WCF).
Answer:
Here is the piece of code just in case anyone is looking for it.
MethodInfo[] methods = typeof(MyClass).GetMethods(BindingFlags.Public | BindingFlags.Instance);
if (methods != null && methods.Length > 0)
{
foreach (MethodInfo m in methods)
{
foreach (object o in m.GetCustomAttributes(false))
{
// To identify the method
if (o.GetType().Name.Equals("SoapDocumentMethodAttribute"))
{
// Get Name using m.Name
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者,如果您需要即时阅读服务的方法,您可能会对本文感兴趣,因为它说明了如何从 WSDL 创建 WCF 代理。
http://blogs .msdn.com/b/vipulmodi/archive/2008/10/16/dynamic-proxy-and-memory-footprint.aspx
然后您可以使用反射(按照迈克的建议)来读取服务方法列表由服务暴露。
Alternatively, if you need to read the methods of a service on-the-fly, this article may be of interest to you, as it illustrates how to create a WCF proxy from WSDL.
http://blogs.msdn.com/b/vipulmodi/archive/2008/10/16/dynamic-proxy-and-memory-footprint.aspx
Then you can use reflection (as per Mike's suggestion) to read the list of service methods exposed by the service.
在您的客户端,由于您已经拥有 Web 服务的 Web 引用类型,因此您只需 使用反射列出代理客户端类中的所有方法。
On your client side, since you already have a web reference type for the web service, you can just use reflection to list all the methods in the proxy client class.