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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube