Shahid
Shahid

Reputation: 1005

how to keep DRY when moving from ASMX Web Services to WCF

Using .NET Framework 3.5 C#, I am in the process of moving from SOAP-based Web Services to JSON-based WCF Services. All my web services derive from a common ServiceBase class with common functionality like session validation, client authentication, database connection, logging etc. and in all my web service method I call a common method on the base class using a single line of code to perform the common tasks

base.InitCallThread()

Q1: What is the best way to do the same or similar in WCF in order to avoid having to write hundreds of lines of repetitive code?

Q2: If I was to support the same interfaces with both SOAP-based Web Services and JSON-based WCF services in parallel, what would be the best way to share the same common functionality? I guess it is possible to share the http session b/w the two?

Upvotes: 1

Views: 169

Answers (2)

Marek
Marek

Reputation: 1788

Q1: You can derive from the same ServiceBase class with common functionality as you had before. WCF does not limit you in this matter. It requires only implementing some interface

Q2: You can define multiple bindings through web.config (or programmatic) configuration.

Upvotes: 0

John Saunders
John Saunders

Reputation: 161773

You should refactor your base class code into a separate class. Call that separate class from the base class in your ASMX services; in the WCF service, call it from each individual service, or use one of the WCF extensibility mechanisms to call it centrally.

WCF permits the same service to be exposed on multiple endpoints with different bindings. You can expose the same WCF service with basicHttpBinding for SOAP, at the same time as exposing a webHttpBinding for the JSON.

Upvotes: 1

Related Questions