In Visual Studio 2015 Preview (Pre Release), how can I add a service reference for a WCF
service?
(For a . NET Core or . NET Standard project, this option is available when you right-click on the Dependencies node of the project in Solution Explorer and choose Manage Connected Services.) On the Connected Services page, select Add Service Reference.
NET 5 support calling WCF services, but won't offer server-side support for hosting WCF. There are two recommended paths for modernizing WCF apps: gRPC is built on modern technologies and has emerged as the most popular choice across the developer community for RPC apps.
Currently, this is a fairly involved process as the tooling does not seem to support much in the way of generating WCF client code or automatically map from config files. Also, as dotnetstep has pointed out, the ASP.NET team has not ported System.ServiceModel
to 5 yet (or provided an alternative for WCF clients yet). Nonetheless, we can use a code-based approach to create a client proxy and use svcutil
to generate our service reference classes.
For this example, I will assume you are locally hosting a service at http://localhost:5000/MapService.svc that implements an IMapService
contract. Also, we will call the project that is going to contain the service proxy MapClient
.
Your project.json
should look something like:
{ "commands": { "run": "run" }, "frameworks": { "dnx451": { "dependencies": { "Microsoft.AspNet.Mvc": "6.0.0-beta2" }, "frameworkAssemblies": { "System.ServiceModel": "4.0.0.0" } } } }
First, let's create a folder, Service References
, in the MapClient
project.
Next, open up Developer Command Prompt for VS2015 and navigate to your MapClient
project directory:
cd "C:\Users\youraccount\Documents\Visual Studio 2015\Projects\MapClient\src\MapClient"
Make sure MapService
is running and run the following command:
svcutil /language:cs /out:"Service References\MapServiceReference.cs" http://localhost:5000/MapService.svc
That should generate two files, output.config
and MapServiceReference.cs
.
Since there is no way to automagically map endpoint and binding configuration from a config file to your ClientBase
currently in ASP.NET 5, the output.config
isn't of much use to us. You can remove it.
Instead, let's create a client proxy in the code:
using System.ServiceModel; namespace TestWCFReference { public class Program { public void Main(string[] args) { var endpointUrl = "http://localhost:5000/MapService.svc"; BasicHttpBinding binding = new BasicHttpBinding(); EndpointAddress endpoint = new EndpointAddress(endpointUrl); ChannelFactory<IMapService> channelFactory = new ChannelFactory<IMapService>(binding, endpoint); IMapService clientProxy = channelFactory.CreateChannel(); var map = clientProxy.GetMap(); channelFactory.Close(); } } }
Now you can use the clientProxy
instance to access any Operation Contract in IMapService
.
As a sidenote, it would probably be better architecture to create a key:value config file that stores your binding and endpoint configuration and use the Microsoft.Framework.ConfigurationModel.Configuration
object to populate your ChannelFactory
so you can keep your service configuration out of your code, but hopefully this example will get you started.
There is a new Visual Studio extension which allows you to add and use service references like in previous versions. It is also compatible with the new CoreCLR, I have just tested it.
http://blogs.msdn.com/b/webdev/archive/2015/12/15/wcf-connected-service-visual-studio-extension-preview-for-asp-net-5-projects.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With