简体   繁体   中英

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

http://localhost:[port]/Service1.svc ? - no 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#..)

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

So firing up the webservice, I tried to do this...

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

and

http://localhost:61094/Service1.svc/method/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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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