. . Hong Kong Business Software Company
Expert in web-based solutions
Software House in Hong Kong

Microsoft Certified Solution Developer

 

Chief Architect's Blog on software development

16 Apr 2008
Windows Communication Foundation - C# and VB.Net Code Comparison

Windows Communication Foundation (WCF) is a new SOA technology aiming at unifying earlier distributed technology such as ASP.NET Web Service, .Net Remoting, Enterprise Services. There are quite a number of good books published in 2007 and 2008. Unfortunately, most of them are written with reference to C# code.

In this blog, I attempt to list some code comparisons for the two languages. Please note that I am not attempting to write a C# to VB.Net conversion program for WCF. I am just hoping to give some hints to VB developers to start off learning WCF. To start off, I strongly recommend you to buy a good book on WCF (very likely, it is in C# because a general VB.Net book containing a small chapter in WCF is not good enough).

As C# and VB.Net are both .Net language, their implementation methods are very similar.  If you cannot translate the WCF code, you can refer to the example in the following table to see if you can find some hints.  For other C# to VB.Net translation, you can easily find it from the web.

C# Example VB.Net Equivalent Example
ServiceModel Metadata Utility svcutil /o:serviceproxy.cs /config:app.config http://localhost:8000/Test
svcutil /l:vb /o:serviceproxy.vb /config:app.config http://localhost:8000/Test
Add Reference using System.ServiceModel;
using System.Runtime.Serialization;
using System.IdentityModel
Imports System.ServiceModel
Imports System.Runtime.Serialization
Imports System.IdentityModel
Service Contract [ServiceContract(Name="ServiceAContract", Namespace="http://www.biswaretech.com/test")]
public interface IServiceA
{
  [OperationContract]
  string MyTest();
}
<ServiceContract(Name:="ServiceAContract", _ Namespace:= "http://www.biswaretech.com/test")> _
Public Interface IServiceA

  <OperationContract()> _
  Function MyTest() As String

End Interface
Data Contract [DataContract( Namespace= "http://schemas.biswaretech.com/test")]
public class MyItem
{
  [DataMember]
  private string m_MyTest();
 
  public string MyTest
  {
     get {return m_MyTest;}
     set {m_MyTest = value;}
}
<DataContract( Namespace:= _ "http://schemas.biswaretech.com/test")> _
Public Class MyItem

  <DataMember()> _
  Private m_MyTest() As String

  Public Property MyTest() As String
    Get
       Return m_MyTest
    End Get
    Set (ByVal Value As String)
      m_MyTest = Value
    End Set
  End Property

End Class
Service Class public class TestService : IServiceA
{
  public string MyTest()
   {
      return "Hi";
   }
}
Pulbic Class TestService
  Implements IServiceA

  Public Function MyTest () As String _
    Implements  IServiceA.MyTest
      Return "Hi"
  End Function

End Class
Self Hosting code using (ServiceHost host =
new ServiceHost(typeof(Test.ServiceA),
new Uri("http://localhost:8000/Test")))
{
  host.AddServiceEndpoint(typeof(Test.IServiceA"),
  new BasicHttpBinding(), "TestService");
  host.Open();
  Console.Writeline("Press <Enter> to terminate");
  Console.Readline();
}
Using host As New ServiceHost(GetType _(Test.ServiceA), New Uri("http://localhost:8000/Test"))
  
host.AddServiceEndpoint(GetType(Test.IServiceA"), _
   New BasicHttpBinding(), "TestService")
   host.Open()
   Console.Writeline("Press <Enter> to terminate")
   Console.Readline()

End Using
Declarative
role-based authorization
[PrincipalPermission
(SecurityAction.Demand, Role="Administrators")]
public string OperationForAdminOnly()
{
   // code restricted to users with admin role
}
<PriincipalPermission( SecurityAction.Demand, _
Role:="Administrators")> _
Public Function OperationForAdminOnly() As String
   'code restricted to users with admin role
End Function
  
Channel Factory code ChannelFactory<IServiceA> factoryA = new ChannelFactory<IServiceA>("");
proxyA=factoryA.CreateChannel();
Dim factoryA As New ChannelFactory (Of IServiceA)
porxyA=factoryA.CreateChannel
Svc file Page Directive <% @ServiceHost Service="Test.TestService" %>
<% @ServiceHost Language=VB Service="Test.TestService" %>
Web.Config and App.Config Case Sensitive Case Sensitive (same as C#)