【问题标题】:Can't run WCF service application in Visual Studio 2015无法在 Visual Studio 2015 中运行 WCF 服务应用程序
【发布时间】:2017-01-28 02:12:08
【问题描述】:

我正在尝试创建我的第一个 WCF 服务应用程序,但无法从 Visual Studio 2015 运行它。

这是我点击运行时遇到的错误...

我正在关注一个教程,我认为他们跳过了几个步骤,但这是我添加到 web.config 中的内容

<services>
  <service name="OnPatrolRest.Service1">
    <endpoint address="http://localhost:51964/service1"
          binding="webHttpBinding"
          contract="OnPatrolRest.IService1"/>
  </service>
</services>

这是界面

    <ServiceContract()>
Public Interface IService1

    <OperationContract()>
    Function GetData(ByVal value As Integer) As String

    <OperationContract()>
    Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType

    <OperationContract()>
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/getPerson?id={id}")>
    Function getPerson(ByVal id As String) As Person

End Interface

对于类文件...

Public Function getPerson(ByVal id As String) As Person Implements IService1.getPerson
        Dim p As New Person

        p.Id = Convert.ToInt32(id)
        p.Name = "Sterling Archer"

        Return p
    End Function

这是我添加的唯一功能。我错过了什么。我对此很陌生。我看到的每个帖子都是几年前的,在 VS 2015 中不起作用。

非常感谢您的帮助!!!

【问题讨论】:

    标签: vb.net wcf visual-studio-2015


    【解决方案1】:

    这对我有用,在控制台应用程序中。不需要Web.configApp.config

    主模块

    Module Main
      Sub Main()
        With New Manager(GetType(Service), "SomeService", 8080)
          Environment.ExitCode = .StartService
        End With
      End Sub
    End Module
    

    服务经理

    Friend Class Manager
      Implements ServiceControl
    
      Public Sub New(ServiceType As Type, ServiceName as String, WcfPort As Integer)
        Me.ServiceType = ServiceType
        Me.ServiceName = ServiceName
        Me.WcfPort = WcfPort
      End Sub
    
    
    
      Public Sub StartService
        Try
          Me.OpenServiceHost()
    
        Catch ex As Exception
    
        End Try
      End Sub
    
    
    
      Public Sub StopService
        Try
          Me.CloseServiceHost()
    
        Catch ex As Exception
    
        End Try
      End Sub
    
    
    
      Public Sub OpenServiceHost()
        Me.ServiceHost.Open()
      End Sub
    
    
    
      Public Sub CloseServiceHost()
        If Me.ServiceHost.IsNotNothing Then
          If Me.ServiceHost.State <> CommunicationState.Closed Then
            Me.ServiceHost.Close()
          End If
        End If
      End Sub
    
    
    
      Private ReadOnly Property ServiceHost As ServiceHost
        Get
          If _ServiceHost Is Nothing Then
            _ServiceHost = New ServiceHost(Me.ServiceType, Me.ListenerAddress)
            _ServiceHost.AddDefaultEndpoints()
          End If
    
          Return _ServiceHost
        End Get
      End Property
      Private _ServiceHost As ServiceHost
    
    
    
      Private ReadOnly Property ListenerAddress As Uri
        Get
          Return New Uri($"http://{Environment.MachineName}:{Me.WcfPort}/{Me.ServiceName}")
        End Get
      End Property
    
    
    
      Private Property ServiceName As String
      Private Property WcfPort As Integer
    End Class
    

    服务接口

    <ServiceContract>
    Public Interface IService
      <OperationContract> Function GetFilteredResult(Workstations As List(Of String), ProcessName As String) As Result
      <OperationContract> Function GetResult(Workstations As List(Of String)) As Result
    End Interface
    

    服务实现

    Public Class Service
      Implements IService
    
      Public Function GetFilteredResult(Workstations As List(Of String), ProcessName As String) As Result Implements IService.GetFilteredResult
        '
        ' Code goes here
        '
      End Function
    
    
    
      Public Function GetResult(Workstations As List(Of String)) As Result Implements IService.GetResult
        '
        ' Code goes here
        '
      End Function
    End Class
    

    我通常将它们与 TopShelf 配对;该组合提供了一个很好的自托管 WCF 服务,包装在 Windows 服务中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      相关资源
      最近更新 更多