Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 5 add WCF service reference

In Visual Studio 2015 Preview (Pre Release), how can I add a service reference for a WCF service?

like image 257
heymega Avatar asked Feb 09 '15 15:02

heymega


People also ask

How do I add a service reference in .NET 5?

(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.

Is WCF supported in .NET 5?

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.


2 Answers

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.

Solution Prerequisites

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"             }         }     } } 

Generate the Service Reference Classes

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.

Create a code-based client proxy

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.

like image 120
Carrie Kendall Avatar answered Sep 21 '22 04:09

Carrie Kendall


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

like image 26
wodzu Avatar answered Sep 22 '22 04:09

wodzu