【问题标题】:Unable to add User Control to an existing WCF Service无法将用户控制添加到现有 WCF 服务
【发布时间】:2014-05-23 10:17:19
【问题描述】:

我开发了一个 WCF 服务,效果很好。但是,此服务不提供任何用户控制,我现在正在尝试添加此功能。

你可以找到 WCF 类:

Public Class WCF_ServicioWeb
    Implements IWCF_ServicioWeb

    Public Sub New()
    End Sub

    Public Function HelloWorld() As String Implements IWCF_ServicioWeb.HelloWorld
        Return My.Resources.CONST_HELLO_WORLD
    End Function

    Public Function EnviarFichero(ByVal composite As CompositeType) As Integer Implements IWCF_ServicioWeb.EnviarFichero
            'Do some ooperations.
            Return 0
    End Function

End Class

操作合同:

<ServiceContract()>
Public Interface IWCF_ServicioWeb

    <OperationContract()>
    Function HelloWorld() As String

    <OperationContract()>
    Function EnviarFichero(ByVal composite As CompositeType) As Integer

End Interface


<DataContract()>
Public Class CompositeType

    <DataMember()>
    Public Property nombreFichero() As String

    <DataMember()>
    Public Property lugarAdq() As String

    <DataMember()>
    Public Property sCadenaDeTexto() As String

End Class

现在您可以使用 web.config 文件了:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="WCF_ServicioWeb.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

在网上看了一下,发现为了引入用户控件,类:

Imports System
Imports System.ServiceModel
Imports System.IdentityModel.Tokens
Imports System.IdentityModel.Selectors
Imports System.Security.Principal

Public Class CustomUserNameValidator
    Inherits UserNamePasswordValidator
    Public Overrides Sub Validate(ByVal userName As String, ByVal password As String)
        If ((userName = "user") And (password = "password")) Then
            'passed
        Else
            Throw New FaultException("Invalid credentials")
        End If
    End Sub
End Class

但是,我收到一个错误,因为类 UserNamePasswordValidator、lib Imports System.IdentityModel.Selectors 和 lib Imports System.IdentityModel。找不到令牌。我正在使用 vb.net (VS2010) 和 NET Framework 4.0。 任何人都知道为什么会发生这种情况? 我检查了可以从 lib System.ServiceModel.Security.Tokens 而不是 System.IdentityModel.Tokens 导入令牌,但我还没有找到如何导入选择器

而且,一旦这个类可以正确地添加到项目中,我必须如何继续在 WCF 上引入用户控件?为此,我应该对 web.config 文件进行哪些修改?

提前致谢!

【问题讨论】:

    标签: .net vb.net wcf


    【解决方案1】:

    确保您已为所有类和方法定义了正确的 Contract,

    <OperationContract()>
    

    http://msdn.microsoft.com/en-us/library/bb386386.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    这是 VB.NET 中 WCF 的最佳方法,希望对您有所帮助,

    http://www.aspnettutorials.com/tutorials/advanced/introduction-to-wcf-vb/

    【讨论】:

    • 感谢您的回答。但请记住,原始 WCF 服务(没有用户创作)运行良好,因此操作合同定义明确。我要做的是添加检查用户和密码的能力。
    • 我已将合同添加到最初的问题中。请记住,发布的代码运行良好。
    猜你喜欢
    • 2012-02-15
    • 2011-06-29
    • 2011-05-17
    • 2020-08-20
    • 1970-01-01
    • 2014-04-04
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多