【问题标题】:WCF RESTful Error 404WCF RESTful 错误 404
【发布时间】:2015-12-03 07:01:56
【问题描述】:

我关注了REST / SOAP endpoints for a WCF Service 问题,我的 SOAP /basicHttpbindingmethods 有效,但我的 RESTful webHttpbinding抛出 404 not found 错误。

我确实看到有人在这个问题中问过和我一样的问题,但没有任何回应。

网络配置

 <?xml version="1.0"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add name="TruckDbWcf" connectionString="Data Source=localhost;Initial Catalog=TruckDbWcf;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <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="TrucksWcf.TruckService" behaviorConfiguration="ServiceBehavior">
        <!--Endpoint for SOAP-->
        <endpoint 
          address="SoapService" 
          contract ="TrucksWcf.ITruckService" 
          binding="basicHttpBinding">
        </endpoint>
        <!--Endpoint for REST-->
        <endpoint 
          address="JSONService" 
          contract="TrucksWcf.ITruckService" 
          binding="webHttpBinding"
          behaviorConfiguration="restPoxBehavior">
        </endpoint>
      </service>
    </services>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"/>
    </diagnostics>
  </system.serviceModel>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
        <listeners>
          <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Temp\SvcLog\Traces.svclog"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <!--Behavior for the REST endpoint for help enability-->
        <behavior name="restPoxBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept"/>
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
</configuration>

Service.svc.cs

 public List<RTrucks> GetTrucksA()
        {
            List<RTrucks> results = new List<RTrucks>();

            foreach (RTrucks truck in results)
            {
                results.Add(new RTrucks()
                {
                    Id = truck.Id,
                    ChassisManufacturer = truck.ChassisManufacturer,
                    ChassisModel = truck.ChassisModel
                });
            }
            return results;
        }

Service.cs

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getTrucksA")]
List<RTrucks> GetTrucksA();

【问题讨论】:

    标签: c# web-services wcf rest soap


    【解决方案1】:

    尝试从 address 属性中删除值

    <endpoint 
        address="" 
        contract="TrucksWcf.ITruckService" 
        binding="webHttpBinding"
        behaviorConfiguration="restPoxBehavior">
    </endpoint>
    

    然后从这里更改操作合同:

    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getTrucksA")]
    

    到这里:

     [WebGet(UriTemplate = "/getTrucksA", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    

    运行您的服务并在浏览器中打开下一页:

    http://your_host_name:your_port_number/Service.svc/getTrucksA
    

    【讨论】:

    • 是的,清除地址值非常感谢@Tequila
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多