Simplify WCF Development by Hosting a WCF Service Library in an ASP.NET Project

Develop and test in a WCF Service Library project while hosting your application in an ASP.NET project.

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.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Mastering Blazor Authentication and Authorization

    At the Visual Studio Live! @ Microsoft HQ developer conference set for August, Rockford Lhotka will explain the ins and outs of authentication across Blazor Server, WebAssembly, and .NET MAUI Hybrid apps, and show how to use identity and claims to customize application behavior through fine-grained authorization.

  • Linear Support Vector Regression from Scratch Using C# with Evolutionary Training

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the linear support vector regression (linear SVR) technique, where the goal is to predict a single numeric value. A linear SVR model uses an unusual error/loss function and cannot be trained using standard simple techniques, and so evolutionary optimization training is used.

  • Low-Code Report Says AI Will Enhance, Not Replace DIY Dev Tools

    Along with replacing software developers and possibly killing humanity, advanced AI is seen by many as a death knell for the do-it-yourself, low-code/no-code tooling industry, but a new report belies that notion.

  • Vibe Coding with Latest Visual Studio Preview

    Microsoft's latest Visual Studio preview facilitates "vibe coding," where developers mainly use GitHub Copilot AI to do all the programming in accordance with spoken or typed instructions.

  • Steve Sanderson Previews AI App Dev: Small Models, Agents and a Blazor Voice Assistant

    Blazor creator Steve Sanderson presented a keynote at the recent NDC London 2025 conference where he previewed the future of .NET application development with smaller AI models and autonomous agents, along with showcasing a new Blazor voice assistant project demonstrating cutting-edge functionality.

Subscribe on YouTube