Migrate a WCF service and client to a gRPC with ServiceModel.Grpc
This page shows how to migrate existing WCF services and clients to gRPC with minimum effort.
The existing WCF solution
The MigrateWCFTogRpc.sln includes a simple request-response Person service. WCF service contract:
[ServiceContract]
public interface IPersonService
{
[OperationContract]
Task<Person> Get(int personId);
[OperationContract]
Task<IList<Person>> GetAll();
}
The Person type is a simple data contract class:
[DataContract]
public class Person
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string SecondName { get; set; }
}
Projects
- Contract - netstandard2.0, contains the service contract and data contract
- Service - netstandard2.0, contains the person service implementation (business logic)
- WcfServer - net462, hosts WCF endpoint “http://localhost:8000/PersonService.svc”
- WcfClient - net462, makes WCF calls to endpoint “http://localhost:8000/PersonService.svc”
Migrate to gRPC
With ServiceModel.Grpc migration is simple:
- no changes in
Contract
andService
- no .proto files
- on the server-side only hosting has to be changed
- on the client-side only WCF ChannelFactory has to be replaced by
ServiceModel.Grpc.Client.ClientFactory
Host PersonService in ASP.NET Core server
The project GrpcServer is already configured and has reference to the nuget package ServiceModel.Grpc.AspNetCore.
All required configuration to host PersonService is done in the Program.cs:
// configure service provider
PersonModule.ConfigureServices(builder.Services);
// enable ServiceModel.Grpc
builder.Services.AddServiceModelGrpc();
// host PersonService
app.MapGrpcService<PersonService>();
Migrate client
The project GrpcClient has reference to nuget package ServiceModel.Grpc.
// create ClientFactory
private static readonly IClientFactory DefaultClientFactory = new ClientFactory();
// create gRPC Channel
var channel = new Channel("localhost", 8080, ChannelCredentials.Insecure);
// create client
var proxy = DefaultClientFactory.CreateClient<IPersonService>(aspNetCoreChannel);
What is next
Migrate FaultContract exception handling to a gRPC global error handling.