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

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

Subscribe on YouTube