具有多个参数的 WCF Restful API

Posted

技术标签:

【中文标题】具有多个参数的 WCF Restful API【英文标题】:WCF Restful API with multiple parameter 【发布时间】:2017-05-06 21:19:35 【问题描述】:

我正在努力在 VB.Net 中创建 WCF RESTful API,到目前为止,我正在使用一个参数从 MSSQL 数据库获取数据并保存数据。 目前我正在尝试创建一个具有多个参数(电子邮件和密码)的服务合同来验证用户登录。这不断抛出以下错误,我只是不知道如何解决这个问题:

错误:无法从 localhost:12345/SMPCService.svc 获取元数据 如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。有关启用元数据发布的帮助,请参阅位于 go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange 错误 URI: localhost:12345/SMPCService.svc 元数据包含无法解析的引用的 MSDN 文档:localhost :12345/SMPCService.svc'。无法激活请求的服务“localhost:12345/SMPCService.svc”。有关详细信息,请参阅服务器的诊断跟踪日志。HTTP GET 错误 URI:localhost:12345/SMPCService.svc 下载“/localhost:12345/SMPCService.svc”时出错。请求失败并显示错误消息:--

这是我的服务合同:

<OperationContract()>
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="ValidateLogin/e/p")>
Function ValidateLogin(ByVal sEmailAddress As String, ByVal sPassword As String) As List(Of CheckLogin)

这是被调用的 ValidateLogin 函数:

Public Function ValidateLogin(ByVal sEmailAddress As String, ByVal sPassword As String) As List(Of CheckLogin) Implements ISMPCService.ValidateLogin
    Dim result As List(Of CheckLogin) = New List(Of CheckLogin)
    Dim uAction = New CheckLogin
    Dim pwd As String = ""
    Try
        ' Dimension Local Variables
        Dim uRecSnap As ADODB.Recordset

        ' Check For Open Connection
        If uDBase Is Nothing Then
            OpenConnection()
            bConnection = True
        End If

        ' Run Stored Procedure - Load Timesheet Record
        uCommand = New ADODB.Command
        With uCommand
            .ActiveConnection = uDBase
            .CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
            .CommandTimeout = 0
            .Parameters.Append(.CreateParameter("@EmailAddress", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 30, sEmailAddress))
            .CommandText = "API_WebUser_ValidateLogin"
            uRecSnap = .Execute
        End With

        ' Populate List
        Do Until uRecSnap.EOF
            pwd = If(IsDBNull(uRecSnap("UserPassword").Value), "", uRecSnap("UserPassword").Value)
            uRecSnap.MoveNext()
        Loop
        uRecSnap = Nothing

        If pwd <> "" Then
            If pwd.Substring(0, 4) = "$2y$" Then
                Mid(pwd, 3, 1) = "a"
            End If

            If BCrypt.Net.BCrypt.Verify(SHA512Hash(sPassword), pwd) Then
                uAction.WasSuccessful = "OK"
                uAction.StatusDescription = "User credentials match"
            Else
                uAction.WasSuccessful = "FAIL"
                uAction.StatusDescription = "Failed to authorize user"
            End If

        Else
            uAction.WasSuccessful = "FAIL"
            uAction.StatusDescription = "Failed to authorize user"
        End If

        result.Add(uAction)
    Catch ex As Exception
        ' Catch Error
        If Err.Number <> 0 Then
            Console.WriteLine(ex.Message)
        End If

    Finally
        ' CleanUp

        ' Close Database Connection
        If bConnection Then CloseConnection()

    End Try

    Return result
End Function

最后,这是我的 Web.config:

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <!--Disabled custom errors to allow display of detailed errors.-->
    <customErrors mode="Off"/>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" relaxedUrlToFileSystemMapping="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <!--Specify services the application hosts. 
      Name specifies the type that provides an implementation of a service contract.
      Behavior Configuration specifies the name of one of the behaviours found in the behaviours element and governs actions such as whether the service allows impersonation.-->
      <service name="SMPCWebService.SMPCService" behaviorConfiguration="SMPCWebService.SMPCServiceBehaviour">
        <!--Define the service endpoints.-->  
        <endpoint address="../SMPCService.svc" behaviorConfiguration="webBehaviour" binding="webHttpBinding" contract="SMPCWebService.ISMPCService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
          <behavior name="SMPCWebService.SMPCServiceBehaviour">
            <!-- To avoid disclosing metadata information, set the values 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="true" />
        </behavior>
      </serviceBehaviors>
      <!--Define the endpoint behaviour.-->
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <!-- Allowing Cross-Origin Resource Sharing (CORS) - The httpProtocol settings allow web services to be called from external domains using javascript-->
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true"/>
    <httpErrors errorMode="Detailed" />
    <validation validateIntegratedModeConfiguration="false"/>
    <!--
        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>

我查看了许多帖子以找到有关此问题的提示,但我没有得到任何结果。

我看过的帖子:

Why do I get this WCF error when 'GET'ing?

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.`

How to send email address as parameter in wcf method

http://jeffbarnes.net/blog/post/2006/10/16/metadata-exchange-endpoint.aspx

【问题讨论】:

尝试将&lt;endpoint address="../SMPCService.svc" 更改为&lt;endpoint address="SMPCService.svc"。此外,RESFul WCF 服务不应该发布任何元数据(尽管您可以看到带有 host:post/some.svc?wsdl 的 wsdl) @AmitKumarGhosh 我已经更改了端点,但是我仍然收到完全相同的错误消息。我觉得奇怪的是我的其他两个服务合同如何工作得很好,但是这个有两个参数的合同却导致了问题。 尝试删除 WebMessageBodyStyle.Wrapped,因为它希望请求被包装,但实际上您并没有这样做。 @AmitKumarGhosh 我之前已经尝试过这个并且没有任何变化。应该指定 GET 中的两个参数的方式有什么问题吗?我已经在我的 HelloWorld 项目中测试了这个函数,我用它来测试不同的设施,它返回了正确的值,但是它导致了 WCF 服务的问题。 【参考方案1】:

在尝试了不同的方法之后,我终于解决了我出错的地方。

函数的参数名称需要与 UriTemplate 中指定的参数命名相同。

原文:

<OperationContract()>
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="ValidateLogin/e/p")>
Function ValidateLogin(ByVal sEmailAddress As String, ByVal sPassword As String) As List(Of CheckLogin)

修复:

<OperationContract()>
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="ValidateLogin/e/p")>
Function ValidateLogin(ByVal e As String, ByVal p As String) As List(Of CheckLogin)

【讨论】:

以上是关于具有多个参数的 WCF Restful API的主要内容,如果未能解决你的问题,请参考以下文章

如何设计具有多个输入参数的 RESTful URL

RESTful 服务:WCF 与 ASP.NET MVC

具有多个参数值的 Laravel RESTful API

带有参数的 Wcf Restful 服务发布用户名

Android 向 WCF RESTful WS 发布请求(JSON 参数)

WCF RESTful 服务:如何在 POST 请求中发送长字符串参数?