无法在 Visual Studio 2015 中运行 WCF 服务应用程序 [英] Can't run WCF service application in Visual Studio 2015
问题描述
我正在尝试创建我的第一个 WCF 服务应用程序,但我无法让它从 Visual Studio 2015 运行.
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...
我正在学习教程,我认为他们跳过了几个步骤,但这是我添加到 web.config 的内容
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>
这是界面
<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 中不起作用.
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!!!
推荐答案
这对我有用,在控制台应用程序中.不需要 Web.config
或 App.config
.
This works for me, in a Console app. No Web.config
or App.config
necessary.
主模块
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 服务中.
I usually pair these with TopShelf; the combination provides a nice self-hosted WCF Service wrapped in a Windows Service.
这篇关于无法在 Visual Studio 2015 中运行 WCF 服务应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!