REST GET wcf url无法正常工作,404错误

时间:2014-07-29 19:30:49

标签: vb.net wcf rest wsdl

我正在尝试提供休息服务并进行测试。以下是生成的wsdl文件,但我无法使GetData方法正常工作。

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

WebConfig:

<?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

3 个答案:

答案 0 :(得分: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文件?

答案 1 :(得分:0)

我认为你没有在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" />

在端点中定义第一个两个部分(协议+根地址)的地址是必需的,而第三个部分是可选的。您只指定了可选部分。

答案 2 :(得分:0)

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

下图显示了一个启动序列,它侦听两个没有错误的端点:

enter image description here

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

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