寻找一个;Hello World WCF不带SVCUtil“;工作方案项目下载,最好在VB.NET中
有人能告诉我一个可以下载的示例解决方案吗?我很难找到一个。创建一个类库MyInterfaces,它只包含您的接口:寻找一个;Hello World WCF不带SVCUtil“;工作方案项目下载,最好在VB.NET中,vb.net,wcf,Vb.net,Wcf,有人能告诉我一个可以下载的示例解决方案吗?我很难找到一个。创建一个类库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 fi
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
添加第三个项目,即将使用该服务的客户端应用程序。我会做一个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")
我不喜欢的一点是,我传递的对象,例如,代表潜在业务对象的客户对象,似乎要求它们具有默认的无参数构造函数。我更喜欢使用构造函数来确保我的对象在任何可能被使用的地方都被正确初始化。你是认真的吗?只需使用“添加服务引用”。这里有大量的入门教程-只需遵循其中一个并构建自己的教程即可。@John:Add Service Reference使用svcuti.exe。说真的。不,它没有,这就是我建议它的原因。添加服务引用实际上与svcutil.exe的代码几乎完全相同,但它不调用svcutil.exe。
Dim endPoint As New EndpointAddress("http://localhost:9496/WCFService4/Service.svc")