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

时间:2015-08-23 11:48:53

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

您好我非常擅长使用WCF(半天)并创建了一个我可以通过localhost看到的服务

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

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

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

int count = int.Parse(txtListCount.Text); //convert text in the textbox to number
List<List<int>> myLists = new List<List<int>>(); //container for your lists
for(int i = 0; i < count; i++)
    {
        myLists.Add(new List<int>()); //create lists dynamically
    }
//myLists contains all your lists

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

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

  

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

  

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

然而没什么。 (不对代码进行调试)

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

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

我错过了什么?

由于

1 个答案:

答案 0 :(得分:2)

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

<?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将调用正确的方法。