Featured image of post gRPC COmpared with WCF and DCOM

gRPC COmpared with WCF and DCOM

Code Examples in Python, C# and GO

🕰️ A Little Backstory (because context matters)

DCOM (Distributed Component Object Model)

  • Created by Microsoft in the 1990s (think: grunge music and dial-up internet).
  • Built to enable communication between software components across machines.
  • More info here.

WCF (Windows Communication Foundation)

  • Introduced with .NET Framework 3.0 in 2006.
  • Made for building service-oriented applications.
  • More here.

gRPC (Google Remote Procedure Call)

  • Launched by Google in 2015.
  • Uses HTTP/2, Protobuf, and it’s blazing fast.
  • More here.

🚀 Head-to-Head Comparison

FeatureDCOMWCFgRPC
ProtocolCOM/DCOMSOAP, HTTP, TCP, MSMQHTTP/2
SerializationBinaryXML, Binary, JSONProtobuf
PlatformWindows-onlyWindows-centricCross-platform
StreamingNopeLimitedFully supported
PerformanceSlowishDecentLightning fast ⚡️

🛠️ Code Examples

1. Python: Protobuf Definition

1
2
3
4
syntax = "proto3";
message Greeting {
  string message = 1;
}

2. Python: gRPC Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import grpc
from concurrent import futures
import greeting_pb2
import greeting_pb2_grpc

class Greeter(greeting_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        return greeting_pb2.Greeting(message=f"Hello {request.name}!")

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    greeting_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()

serve()

3. Python: gRPC Client

1
2
3
4
5
6
7
8
import grpc
import greeting_pb2
import greeting_pb2_grpc

channel = grpc.insecure_channel('localhost:50051')
stub = greeting_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(greeting_pb2.HelloRequest(name='Pythonista'))
print(response.message)

4. C#: WCF Service

1
2
3
4
5
6
7
8
9
[ServiceContract]
public interface IGreetingService {
    [OperationContract]
    string SayHello(string name);
}

public class GreetingService : IGreetingService {
    public string SayHello(string name) => $"Hello {name}!";
}

5. C#: WCF Client

1
2
3
var factory = new ChannelFactory<IGreetingService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/GreetingService"));
var client = factory.CreateChannel();
Console.WriteLine(client.SayHello("C# Wizard"));

6. Go: gRPC Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"net"
)
type server struct{}
func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
	return &pb.HelloReply{Message: "Hello " + req.Name}, nil
}
func main() {
	lis, _ := net.Listen("tcp", ":50051")
	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	s.Serve(lis)
}

7. Go: gRPC Client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"time"
)
func main() {
	conn, _ := grpc.Dial("localhost:50051", grpc.WithInsecure())
	defer conn.Close()
	c := pb.NewGreeterClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	resp, _ := c.SayHello(ctx, &pb.HelloRequest{Name: "Go Guru"})
	fmt.Println(resp.Message)
}

8. Python: DCOM Setup (Simplified)

1
2
3
4
import win32com.client
app = win32com.client.Dispatch("Excel.Application")
app.Visible = True
print("DCOM Excel instance launched! ON ANOTHER COMPUTER")

9. C#: Simple DCOM Client

1
2
3
4
Type excelType = Type.GetTypeFromProgID("Excel.Application");
dynamic excel = Activator.CreateInstance(excelType);
excel.Visible = true;
Console.WriteLine("DCOM Excel instance launched!  ON ANOTHER COMPUTER");

10. Go: Basic HTTP Call (WCF-style)

1
2
3
4
5
6
7
resp, err := http.Get("http://localhost:8000/GreetingService?name=GoGuru")
if err != nil {
	log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))

🔑 Key Ideas

ConceptExplanation
gRPCModern RPC framework with HTTP/2 and Protobuf
WCFWindows-centric, versatile communication framework
DCOMLegacy component communication from the 90s
StreamingFully supported in gRPC
Cross-platformOnly gRPC supports true cross-platform calls

📚 References

💡 Pro tip: If you hear weird terms like “IDL” or “marshaling,” just nod knowingly and Google them later. 😉