【问题标题】:REST GET wcf url not working and 404 errorREST GET wcf url 不起作用和 404 错误
【发布时间】:2014-07-29 19:30:48
【问题描述】:

我正在尝试进行休息服务并对其进行测试。以下是生成的 wsdl 文件,但我无法使用 GetData 方法。

我来自 http://localhost:21611/Service1.svc?wsdl 的 WSDL 文件是一个有效的 url。

网络配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
        <listeners>
          <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData= "c:\Traces.svclog" />
        </listeners>
      </source>
    </sources>  
   </system.diagnostics>

  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />

  </system.web>
  <system.serviceModel>
    <diagnostics>
       <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
                    maxMessagesToLog="3000" maxSizeOfMessageToLog="2000"/>
    </diagnostics>
    <bindings />
    <services>
      <service behaviorConfiguration="MyServiceBehavior" name="test.Service1">
        <endpoint address="Service1.svc" behaviorConfiguration="myRestBehavior" binding="webHttpBinding" name="epname" contract="test.IService1" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:21611" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="myRestBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpGetBinding="webHttpBinding" httpGetBindingConfiguration="" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <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" />
        <handlers>
            <remove name="svc-Integrated-4.0" />
            <add name="svc-Integrated-4.0" path="*.svc" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
  </system.webServer>
</configuration>

WCF 服务文件:

<ServiceContract()>
Public Interface IService1

    <OperationContract()>
    <WebGet>
    Function GetData() As String


End Interface

Public Class Service1
    Implements IService1

    Public Function GetData() As String Implements IService1.GetData
        Return "Welcome"
    End Function
End Class

我尝试了类似的网址,但没有得到任何结果,请您指导我。

http://localhost:21611/Service1.svc?wsdl
http://localhost:21611/Service1.svc?wsdl/GetData
http://localhost:21611/Service1.svc?wsdl/GetData/
http://localhost:21611/Service1.svc/GetData
http://localhost:21611/Service1.svc/GetData/

另外,我在尝试时收到 404 错误:http://localhost:21611/Service1.svc/GetData

【问题讨论】:

  • REST 服务不使用 WSDL。您可能需要在 WebGet 属性中指定 URI。
  • 也厌倦了 UriTemplate:='GetData' 但仍然没有工作

标签: vb.net wcf rest wsdl


【解决方案1】:

我猜你是想通过指定太多东西来使你的 web.config 复杂化。看下面的web.config

<?xml version="1.0"?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="All" propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
    <system.web>
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
      <service name="SampleRestService.Service1" behaviorConfiguration="serviceWebConfiguration">
        <endpoint address="myService" binding="webHttpBinding" bindingName="defaultRest" behaviorConfiguration="web" contract="SampleRestService.IService1" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="defaultRest"  maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
        <behavior name="json">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="serviceWebConfiguration">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我的地址属性设置为“myService”。所以当我使用上面的配置并运行项目时:

localhost:52164/Service1.svc:显示默认 WCF 服务页面

localhost:52164/Service1.svc?wsdl:向我展示托管的 WCF 服务的 wsdl

locahost:52164/Service1.svc/myService/getdata : 在浏览器中使用时调用我的方法 GetData (RESTfully) 并返回浏览器中显示的 XML 响应,如下所示

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Welcome</string>

注意:

  1. 链接包含端口号 52164,因为我在从 visualstudio 运行项目时使用该端口,因此如果您将端口号替换为您正在使用的 21611,则应该没有任何区别。

  2. 我的项目命名空间是 SampleRestService,在使用上述 web.config 时需要使用“test”替换它

  3. 希望您的项目中有一个 .svc 文件?

【讨论】:

  • 从字面上看,一件小事弄乱了我整个 3-4 周的时间并感到沮丧。不同之处在于,根据我的配置文件,url 应该像“localhost:21611/Service1.svc/Service1.svc/getdata”而不是“localhost:21611/Service1.svc/getdata”。从你的信息我知道了。这很简单,但很难找到信息。如有错误请指正。
  • 是的,你是对的。使用 RESTful 服务的最佳方面是有一个友好的 URL,它们清楚地告诉操作行为。
  • 感谢您的时间和支持。
【解决方案2】:

我认为你没有在 web.config 中指定 基地址

<host>
      <baseAddresses>
        <add baseAddress="http://localhost:21611" />
      </baseAddresses>
    </host>

如果你不想这样做,那么地址应该是完全限定的

<endpoint address="http://localhost:21611/Service1.svc."  behaviorConfiguration="myRestBehavior" binding="webHttpBinding"
          name="epname" contract="test.IService1" />

在端点中定义地址前两部分(协议+根地址)是强制性的,而第三部分是可选的。您只指定了可选部分。

【讨论】:

  • 我按照上面说的添加了基地址,结果还是一样,不行。我还从 codeproject 下载了示例代码,甚至它也有同样的错误。这让我从 1 周开始就被吓到了。 :(
  • @surpavan 如果您在 iis 中托管服务,则 IIS 将确定您的寻址方案,您可以完全删除 baseAddess 部分并删除端点定义中的地址部分。删除那些,确保它会工作......(iis将在动态端口上运行)
【解决方案3】:

如果您还没有,我建议您在服务上启用 WCF 跟踪,以便您更好地了解服务启动、端点定义和任何错误。

下图显示了在两个端点上侦听且没有错误的启动序列:

供参考:WCF 跟踪为故障监控和分析提供诊断数据。您可以使用跟踪而不是调试器来了解应用程序在启动时的行为方式,包括正在侦听的接口端点和任何错误。

http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

【讨论】:

  • "" 我为侦听器添加了该文件名,但该文件是也没有创建:(。一个简单的 REST 让我在过去 3 周内变得焦躁不安,即使从 codeproject 下载的代码也会产生相同的输出。请帮忙。
  • 为了让跟踪工作,您需要: 1.) 通过 - \ \ \
  • 是的,我已经这样做了,更新了有问题的 webconfig 文件,仍然没有创建日志文件:(
  • 您可能需要确保正确安装并注册了 IIS、ASP.NET、WCF 和 WCF 激活组件。 msdn.microsoft.com/en-us/library/aa751792(v=vs.110).aspx
  • 这不是 stackoverflow 的工作方式,但需要帮助,如果您愿意,请通过此链接下载项目,并让我知道我在 3 周以上面临的问题会很有帮助。链接:onedrive.live.com/…
猜你喜欢
  • 2017-11-22
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 2015-02-20
  • 1970-01-01
  • 2012-01-20
相关资源
最近更新 更多