It’s easy to test a service if you create it in a Windows Communication Foundation (WCF) Service Library — just press F5 to bring up Visual Studio’s Test Client. Testing isn’t as easy if you create your service in an ASP.NET project — you have to write your own test client. However, deploying a WCF service is much easier when you create it in an ASP.NET service (just deploy the ASP.NET project). Deploying isn’t as easy if you create your service in a WCF Service Library, because you have to write your own host. But you can get the best of both worlds: develop and test in a WCF Service Library project while hosting your application in an ASP.NET project.
Start by creating a solution containing the WCF Service Library project that will hold your WCF service. When you’ve finished building and testing your service using the Test Client and are ready to host it in a Web site, add the ASP.NET project that will host your service to the solution. Give your ASP.NET project a reference to your WCF Service Library project. Then add a WCF Service to your ASP.NET project (if you want, to reduce the number of names you’re managing, you can give the WCF Service in the ASP.NET project the same name as the service class in your WCF Service Library).
The next step is to get rid of what you don’t need in your ASP.NET project. First, delete both of the C# or Visual Basic code files that were added with the service’s svc file (i.e., the interface and the code behind file). Now double-click on the svc file to open it and, in the ServiceHost directive, delete the CodeBehind=”…” attribute. Then open the project’s Web.config file and delete the system.serviceModel element along with everything inside of it.
Now you can add what you need to your ASP.NET project. Open the app.config file in your WCF Service Library project and copy its system.serviceModel element (and everything in it). Then switch to your ASP.NET project’s Web.config file, and paste the copied system.serviceModel element into the Web.config file where you deleted the old system.serviceModel element.
The last step is to change the svc file in your ASP.NET project to use your service class. In the svc file’s ServiceHost directive, change the name in the Service attribute to the namespace and class name for your service in your WCF Service Library (i.e., if your WCF Service Library is called NorthwindManagement and your service class is called Customers, your Service attribute would be Service=”NorthwindManagement.Customers”).
And you’re done: the service hosted in your ASP.NET project is now using the service you built in your Service Library. You can continue to test your Service Library project just by making the Service Library your solution’s start project and pressing F5; you can deploy your service by deploying the ASP.NET site.