我遵循REST / SOAP endpoints for a WCF Service问题,我的SOAP / basicHttpbinding
方法有效,但我的RESTful webHttpbinding
会抛出404未找到的错误。
我确实在那个问题中看到有人和我说同样的事情,但是没有回复。
Webconfig
<?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();
答案 0 :(得分:1)
尝试从地址属性中删除值
<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