Looking for a "Hello World WCF without SVCUtil" working solution project to download, preferably in VB.NET
Can someone point me to a sample solution that I can download? I am h开发者_如何转开发aving trouble trying to find one.
Create a class library, MyInterfaces, that houses just your interfaces:
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
Add a Web Service Library project. Set a reference to the above project that contains the above Interfaces. Add the following code to this project:
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
Add a 3rd project, a client app which will consume the Service. I'll make mine a WinForm app. Have this project also reference the Interface projrect. Add this code:
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
This is just a quick and dirty example. My goal was to get rid of the generated proxy object that you normally get when you use the Add Service Wizard. I suppose that end point should come from a config file, though I am not sure how to dynamically determine what the port will be when debugging under VS' web server.
Dim endPoint As New EndpointAddress("http://localhost:9496/WCFService4/Service.svc")
The one thing that I don't like about this is that the objects I pass, eg, a Customer object, which represent a potential Business Object, seem to require that they have a default parameterless constructor. I prefer to have constructors to ensure that my object was properly initialized wherever it may have been used.
精彩评论