如何在wcf中使用路由表调用端点?

时间:2017-04-14 07:30:38

标签: c# wcf asp.net-routing

我创建了一个演示Web应用程序,其中我正在尝试使用不包含svc文件的wcf服务。基本上我正在尝试将路由添加到 routetable ,就像我们在asp.net mvc中添加一样。

我从这里开始参考:Reference

代码:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PersonService : IPersonService
    {
        [WebInvoke(UriTemplate = "", Method = "GET")]
        public void GetPerson()
        {
            string k = "a";
        }
    }

 [ServiceContract]
    public interface IPersonService
    {
        [OperationContract]
        void GetPerson();

    }

 public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("foo", new WebServiceHostFactory(), typeof(PersonService)));
        }
    }

Web.config:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

当我尝试从浏览器调用我的GetPerson方法时,我得到未找到EndPoint enter image description here

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

好的,我设法通过在Uritemplate中提供我的方法名称来解决它,如下所示:

[WebInvoke(UriTemplate = "GetPerson", Method = "GET")]
        public void GetPerson()
        {
            string k = "a";
        }