Can't run WCF service application in Visual Studio 2015

人走茶凉 提交于 2021-01-27 18:51:06

问题


I'm trying to create my first WCF service application, but I can't get it to run from Visual Studio 2015.

This is the error I get when I click run...

I was following a tutorial and I think they skipped a few steps, but here's what I added to the web.config

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

and here's the Interface

    <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

and for the class file...

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

That's the only function I added. What am I missing. I'm very new to this. Every post I've seen is from years ago and doesn't work in VS 2015.

Thank you very much for any help!!!


回答1:


This works for me, in a Console app. No Web.config or App.config necessary.

Main module

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

Service manager

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

Service Interface

<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

Service implementation

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

I usually pair these with TopShelf; the combination provides a nice self-hosted WCF Service wrapped in a Windows Service.



来源:https://stackoverflow.com/questions/41905712/cant-run-wcf-service-application-in-visual-studio-2015

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!