【问题标题】:How to get all the URLTemplate of method names from the WCF Rest service in C#如何从 C# 中的 WCF Rest 服务中获取方法名称的所有 URLTemplate
【发布时间】:2018-09-25 19:19:42
【问题描述】:

我有一个休息服务网址如下 http://XXXXXXXX/RestServices/Project.svc

这个SVC有两种方法,

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "UserLogin")]
    ResultInfo Login(Login objLogin);

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "SaveUser")]
    ResultInfo User(User objUser);

方法名称是 Login 和 User,但 UriTemplate 名称是 UserLogin 和 SaveUser。

我想得到如下 C# 列表中的输出

UserLogin
SaveUser

在这方面需要你的帮助。

【问题讨论】:

  • 请尝试提供更多详细信息,例如您想要什么以及如何填充您的列表?
  • List str = new List();在这个字符串列表中,我需要使用这个 WCF 服务 URL 的 URLTemplate 名称

标签: c# asp.net rest web-services wcf


【解决方案1】:

您可以使用以下方法获取方法名称

在 ASPX 页面中:

<asp:ListBox ID="lstMethodName" runat="server" Visible="true" AutoPostBack="true"
        Style="height: 150px; width:300px; overflow: auto;"></asp:ListBox>

在代码后面:

string wsdlUrl = "http://XXXXXXXX/RestServices/Project.svc?wsdl"; // WCF wsdl address
                XmlTextReader myReader = new XmlTextReader(wsdlUrl);
                if (ServiceDescription.CanRead(myReader))
                {
                    ServiceDescription myDescription = ServiceDescription.Read(myReader);

                    foreach (PortType pt in myDescription.PortTypes)
                    {
                        foreach (Operation op in pt.Operations)
                        {
                            lstMethodName.Items.Add(op.Name);
                        }
                    }
                }

添加命名空间:

 using System.Web.Services.Description;
 using System.Collections.Generic;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多