C# WCF RESTful服务未在/xml/string uri上拾取-404错误
我在一个MVC应用程序中有一个WCF服务,它并没有像我预期的那样工作 我的IService.cs代码如下:C# WCF RESTful服务未在/xml/string uri上拾取-404错误,c#,xml,wcf,rest,C#,Xml,Wcf,Rest,我在一个MVC应用程序中有一个WCF服务,它并没有像我预期的那样工作 我的IService.cs代码如下: [ServiceContract] public interface IService { [OperationContract] [WebInvoke(Method = "GET", // Set output to XML and the text display to wrap ResponseFormat = WebMessageF
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET",
// Set output to XML and the text display to wrap
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{name}")]
string XMLData(string name);
[OperationContract]
[WebInvoke(Method = "GET",
// Set output to JSON and the text display to wrap
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{name}")]
string JSONData(string name);
}
我的Service.svc代码是:
public class Service : IService
{
#region IService Members
public string XMLData(string name)
{
return "Hello " + name;
}
public string JSONData(string name)
{
return "Hello " + name;
}
#endregion
}
当我转到localhost/Service.svc时,我得到了我期望的页面。然而,当我转到localhost/Service.svc/xml/Tim(或/json/)时,我得到一个404-resource-not-found错误
我很肯定我的Web.config文件也配置正确,但如果有人认为这是问题所在,我可以发布(只是有点太多MVC的东西了)
你知道是什么引起的吗?谢谢
编辑:这是我的完整Web.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= (LocalDb)\v11.0;Initial Catalog=aspnet-FinalProjectGuitar-20151110211739;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-FinalProjectGuitar-20151110211739.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="WebxmlHttp">
<security mode="None"/>
</binding>
<binding name="WebjsonHttp">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="ServiceBehavior" name="FinalProjectGuitar.Service">
<endpoint address="xml" behaviorConfiguration="xmlBehavior" binding="webHttpBinding"
bindingConfiguration="WebxmlHttp" name="webXMLHttpBinding" contract="FinalProjectGuitar.IService" />
<endpoint address="json" behaviorConfiguration="jsonBehavior" binding="webHttpBinding"
bindingConfiguration="WebjsonHttp" name="webJSONHttpBinding" contract="FinalProjectGuitar.IService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp defaultOutgoingResponseFormat="Json"/>
</behavior>
<behavior name="xmlBehavior">
<webHttp defaultOutgoingResponseFormat="Xml"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
我将与您分享我的配置,因为它背后的服务已经连续运行了两年,没有出现任何问题,返回pox、json和soap。我打赌这是服务端点配置中的一些东西 顺便说一句:我认为您不应该在uri模板(json/xml)中指定端点行为。这对于参数匹配类型的东西来说更重要,imo
[OperationContract()]
[WebGet(UriTemplate = "GetData/{aParam1}/{aParam2}")]
MyResponse GetData(string aParam1, string aParam2);
web.config
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="WebxmlHttp">
<security mode="None"/>
</binding>
<binding name="WebjsonHttp">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="SecureServiceBehavior" name="XXXX.XXXX.Services.Reporting.ServiceImplementation.XXXX">
<clear />
<endpoint address="xml" behaviorConfiguration="xmlBehavior" binding="webHttpBinding"
bindingConfiguration="WebxmlHttp" name="webXMLHttpBinding" contract="XXXX.XXXX.Services.Reporting.ServiceContracts.XXXX" />
<endpoint address="json" behaviorConfiguration="jsonBehavior"
binding="webHttpBinding" bindingConfiguration="WebjsonHttp"
name="webJSONHttpBinding" contract="XXXX.XXXX.Services.Reporting.ServiceContracts.IXXXXX" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp defaultOutgoingResponseFormat="Json"/>
</behavior>
<behavior name="xmlBehavior">
<webHttp defaultOutgoingResponseFormat="Xml"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="SecureServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483647"/>
<serviceTimeouts transactionTimeout="00:03:00"/>
<serviceThrottling maxConcurrentCalls="10" maxConcurrentSessions="10" maxConcurrentInstances="10"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
你能转到localhost/Service.svc吗?wsdl
?是的,我能。我可以在这里看到XMLData和JSONData操作,嗯,我添加了一些代码。我没有xml和json的单独行为,但这并没有解决它。仍然收到相同的错误。如果仍然是404,则这是服务端点问题,或者由于路由原因找不到web服务方法签名。您是否使用DataContracts进行响应?另外,您是否正在将预期参数添加到url?--localhost/Service.svc/xml/Tim/valuefornameparam我没有使用数据契约。我不太确定这些是什么,但我在谷歌搜索时看到过它们。这是我第一次与Web服务交互,所以对我来说有点新鲜。奇怪的是,我在一个WCF Web服务应用程序中使用了完全相同的代码,它工作得很好。然后,我尝试使用相同的代码,在我的MVC项目中添加一个WCF Web服务,但它不起作用。您正在指定一个{name}参数,是否将其作为url的一部分传递?是的,我想是的。这就是localhost/Service.svc/xml/Tim中的“Tim”