简体   繁体   English

WCF并使用WEBGET在浏览器中测试服务

[英]WCF and using WEBGET to test service in browser

Hi I am very new to using WCF (half a day old) and I have created a service which I can see via localhost 嗨,我是第一次使用WCF(半天),并且创建了一个服务,可以通过localhost查看

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

However, before I want to connect to this service via an application, I want to see if the service does actually return what is meant to. 但是,在我想通过应用程序连接到该服务之前,我想看看该服务是否确实返回了预期的结果。 (Just making sure the database connection is OK etc) (只需确保数据库连接正常,等等)

I have this on my IService (unfortunately it is in VB.Net, but I do know C#..) 我的IService上有此文件(不幸的是,它在VB.Net中,但是我确实知道C#..)

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

And this of course fires (or should) the method getData in the service1.svc class. 当然,这会触发(或应该触发)service1.svc类中的getData方法。

So firing up the webservice, I tried to do this... 因此启动了网络服务,我尝试执行此操作...

http://localhost:61094/Service1.svc?method/1 HTTP://本地主机:61094 / Service1.svc方法/ 1

and

http://localhost:61094/Service1.svc/method/1 HTTP://本地主机:61094 / Service1.svc /方法/ 1

However nothing. 但是什么都没有。 (doesnt debug on the code either) (也不对代码进行调试)

Looking around, It could be do with my web config file which I haven't touched. 环顾四周,这可能与我未触及的Web配置文件有关。

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

What is it that I am missing? 我想念的是什么?

Thanks 谢谢

You could try setting up an explicit service definition: 您可以尝试设置一个明确的服务定义:

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

and then have a contract: 然后签订合同:

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

and an implementation: 和一个实现:

Public Class Service1
    Implements IService

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

Now navigating to /Service1.svc/method/123 will invoke the proper method. 现在导航到/Service1.svc/method/123将调用适当的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM