寻找一个“没有SVCUtil的Hello World WCF”工作解决方案项目,最好在VB.NET中下载

问题描述

| 有人可以指出我可以下载的示例解决方案吗?我很难找到一个。     

解决方法

创建一个仅包含您的接口的类库MyInterfaces:
Imports System.Runtime.Serialization
Imports System.ServiceModel
\' NOTE: You can use the \"Rename\" command on the context menu to change the interface name \"IService\" in both code and config file together.
<ServiceContract()>
Public Interface IService

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

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

    <OperationContract()>
    Function GetCustomer() As Customer

    \' TODO: Add your service operations here

End Interface

\' Use a data contract as illustrated in the sample below to add composite types to service operations.
Public Class Customer

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

    <DataMember()>
    Public Property Age() As Integer

End Class

<DataContract()>
Public Class CompositeType

    <DataMember()>
    Public Property BoolValue() As Boolean
    <DataMember()>
    Public Property StringValue() As String

End Class
添加一个Web服务库项目。设置对包含以上接口的上述项目的引用。将以下代码添加到该项目:
Imports MyInterfaces

\' NOTE: You can use the \"Rename\" command on the context menu to change the class name \"Service\" in code,svc and config file together.
Public Class Service
    Implements IService


    Public Function GetData(ByVal value As Integer) As String Implements IService.GetData
        Return String.Format(\"You entered: {0}\",value)
    End Function

    Public Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType Implements IService.GetDataUsingDataContract
        If composite Is Nothing Then
            Throw New ArgumentNullException(\"composite\")
        End If
        If composite.BoolValue Then
            composite.StringValue &= \"Suffix\"
        End If
        Return composite
    End Function

    Public Function GetCustomer() As MyInterfaces.Customer Implements MyInterfaces.IService.GetCustomer
        Dim x As New Customer
        x.Age = 15
        x.Name = \"Chad\"
        Return x
    End Function

End Class
添加第三个项目,一个将使用该服务的客户端应用程序。我将使我成为WinForm应用程序。让该项目也引用接口项目。添加此代码:
Imports System.ServiceModel
Imports MyInterfaces

Public Class Form1

    Private Sub Button1_Click(sender As System.Object,e As System.EventArgs) Handles Button1.Click

        Dim endPoint As New EndpointAddress(\"http://localhost:9496/WCFService4/Service.svc\")
        Dim binding As New BasicHttpBinding()

        Dim proxy As IService = ChannelFactory(Of IService).CreateChannel(binding,endPoint)

        Dim getDateResult As String = proxy.GetData(3)

        Dim myCompositeType As New MyInterfaces.CompositeType
        myCompositeType.BoolValue = True
        myCompositeType.StringValue = \"ok\"

        Dim result As MyInterfaces.CompositeType = proxy.GetDataUsingDataContract(myCompositeType)
        MessageBox.Show(result.StringValue)
End Sub
这只是一个快速而肮脏的例子。我的目标是摆脱使用添加服务向导时通常会生成的代理对象。我想该端点应该来自配置文件,尽管我不确定在VS \的Web服务器上调试时如何动态确定端口是什么。
Dim endPoint As New EndpointAddress(\"http://localhost:9496/WCFService4/Service.svc\")
我对此不喜欢的一件事是,我传递的对象(例如,表示潜在业务对象的客户对象)似乎要求它们具有默认的无参数构造函数。我更喜欢使用构造函数来确保无论在何处使用我的对象都已正确初始化。