【问题标题】:404 not found on WCF REST service在 WCF REST 服务上找不到 404
【发布时间】:2019-08-29 05:27:03
【问题描述】:

我已经在 WCF 服务上配置了 REST。我有一个像这样工作的其他项目,并将所有配置和内容复制到我的项目中。但是当我运行它时,找不到rest方法和/web页面。

Web.config:

> <appSettings>
>     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />   </appSettings>   <system.web>
>     <compilation debug="true" targetFramework="4.5" />
>     <httpRuntime targetFramework="4.5"/>   </system.web>   <system.serviceModel>
>     <services>
>       <service name="WcfRestTest.Service1">
>         <clear />
>         <endpoint behaviorConfiguration="RFEndPointBehavior" binding="webHttpBinding"
>           contract="WcfRestTest.IService1" listenUriMode="Explicit">
>         </endpoint>
>       </service>
>     </services>
>     <behaviors>
>       <endpointBehaviors>
>         <behavior name="RFEndPointBehavior"  >
>           <webHttp helpEnabled="true"/>
>         </behavior>
>       </endpointBehaviors>
>     </behaviors>
>     <protocolMapping>
>       <add binding="basicHttpsBinding" scheme="https" />
>     </protocolMapping>
>     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />   </system.serviceModel>  
> <system.webServer>
>     <modules runAllManagedModulesForAllRequests="true"/>
>     <directoryBrowse enabled="true"/>   

服务类:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    public List<Student> GetStudentDetails()
    {
        List<Student> stuList = new List<Student>();
        stuList.Add(new Student { ID = "1", Name = "Test" });
        return stuList;
    }
}

服务合同:

[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "Students", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        List<Student> GetStudentDetails();
    }

[DataContract]
public class Student
{
    [DataMember]
    public string ID { get; set; }
    [DataMember]
    public string Name { get; set; }
}

【问题讨论】:

    标签: .net rest wcf http-status-code-404


    【解决方案1】:

    我处理了你的代码段,发现它运行良好。我认为您访问的地址有问题。
    对于托管在 IIS 中的 WCF Rest 服务,我们应该使用 IIS 基地址和 UriTemplate 属性,

    [WebGet(UriTemplate = "Students", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    

    例如,下面是 IIS 中的站点绑定。

    服务基址为http://MyIP:11000/service1.svc,操作应使用http://MyIP:11000/service1.svc/Students进行路由。
    必须注意的另一件事是服务通常具有相对地址。假设有以下定义。

    <endpoint address="myservice" binding="webHttpBinding" contract="WcfService3.IService1" listenUriMode="Explicit"></endpoint>
    

    也应该考虑进去。
    http://MyIP:11000/service1.svc/myservice/Students
    如果有什么我可以帮忙的,请随时告诉我。

    更新

    <system.serviceModel>
    <services>
      <service name="WcfRestTest.Service1">
        <clear />
        <endpoint behaviorConfiguration="RFEndPointBehavior" binding="webHttpBinding"
          contract="WcfRestTest.IService1" listenUriMode="Explicit">
        </endpoint>
      <endpoint address="" binding="webHttpBinding" contract="WcfRestTest.IService1" behaviorConfiguration="RFEndPointBehavior" bindingConfiguration="mybinding"></endpoint>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="mybinding">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="RFEndPointBehavior"  >
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    

    【讨论】:

    • 再次您好,感谢您的回答,我在visual stuido(iis express)上试了一下,我还没有发布IIS。相对地址也不起作用。您为此示例 Abraham 创建了哪种项目类型?
    • 我在VS2017中使用WCF服务应用项目模板。 IIS express 提供的基地址是什么?
    • 如果项目模板是 wcf 服务应用程序没有问题,它可以工作。但其他 web 项目类型我可以提供 wcf 服务(我还有另一个项目),但我无法访问其他服务方法
    • 您可以发布完整的项目类型和测试过程,我将尝试重现该问题。在我这边,当我使用 Web 应用程序模板并添加 WCF 服务项(启用 Ajax)时,它也可以正常工作。有一点需要提一下,这个场景下是没有服务接口的。
    • 嗨,亚伯拉罕,这里是示例项目。感谢帮助。 1drv.ms/u/s!Ag9IW3o-pNntoagml6n1zIYypyvv5g?e=DsNLm3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    相关资源
    最近更新 更多