【问题标题】:WCF and using WEBGET to test service in browserWCF 和使用 WEBGET 在浏览器中测试服务
【发布时间】:2015-11-16 22:24:20
【问题描述】:

您好,我对使用 WCF(半天)非常陌生,我创建了一个可以通过 localhost 看到的服务

http://localhost:[port]/Service1.svc? - 没有 404!

但是,在我想通过应用程序连接到此服务之前,我想查看该服务是否确实返回了预期的内容。 (只要确保数据库连接正常等)

我的 IService 上有这个(不幸的是它在 VB.Net 中,但我知道 C#..)

<OperationContract>
<WebGet(UriTemplate:="/method/{parameter}")>
Function getData(ByVal parameter As Integer) As String

这当然会触发(或应该)service1.svc 类中的 getData 方法。

所以启动网络服务,我尝试这样做......

http://localhost:61094/Service1.svc?method/1

http://localhost:61094/Service1.svc/method/1

然而什么都没有。 (也不调试代码)

环顾四周,这可能与我没有接触过的网络配置文件有关。

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <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"/>
  </system.webServer>

</configuration>

我错过了什么?

谢谢

【问题讨论】:

    标签: c# vb.net web-services wcf webget


    【解决方案1】:

    您可以尝试设置明确的服务定义:

    <?xml version="1.0" encoding="utf-8"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
        <system.web>
            <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
        </system.web>
        <system.serviceModel>
            <behaviors>
                <serviceBehaviors>
                    <behavior>
                        <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
                        <serviceMetadata httpGetEnabled="true"/>
                        <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                        <serviceDebug includeExceptionDetailInFaults="false"/>
                    </behavior>
                </serviceBehaviors>
                <endpointBehaviors>
                    <behavior name="WebBehavior">
                        <webHttp/>
                    </behavior>
                </endpointBehaviors>
            </behaviors>
            <services>
                <service name="WebApplication1.Service1">
                    <endpoint address="" binding="webHttpBinding" contract="WebApplication1.IService" behaviorConfiguration="WebBehavior" />
                </service>
            </services>
            <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
        </system.serviceModel>
        <system.webServer>
            <modules runAllManagedModulesForAllRequests="true"/>
            <directoryBrowse enabled="true"/>
        </system.webServer>    
    </configuration>
    

    然后签订合同:

    Imports System.ServiceModel
    Imports System.ServiceModel.Web
    
    <ServiceContract()>
    Public Interface IService
        <OperationContract>
        <WebGet(UriTemplate:="/method/{parameter}")>
        Function getData(ByVal parameter As String) As String
    End Interface
    

    和一个实现:

    Public Class Service1
        Implements IService
    
        Public Function getData(ByVal parameter As String) As String Implements IService.getData
            Return "Foo bar"
        End Function
    End Class
    

    现在导航到/Service1.svc/method/123 将调用正确的方法。

    【讨论】:

    • 谢谢,完美的工作,快速的问题,因为我不想真正发布一个新问题,我做这个服务是为了证明一些功能可以工作,网络服务实际上是由第三方开发的,但是,它将成为一个 SOAP Web 服务,所以基本上为了使它成为一个 SOAP,我将端点绑定从 webHttpBinding 更改为 basicHttpBinding,现在这是否使它成为一个 SOAP 服务?
    • 是的,从webHttpBinding 更改为basicHttpBinding 使其成为SOAP 服务。
    猜你喜欢
    • 2011-06-20
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 2013-03-26
    相关资源
    最近更新 更多