ASP.NET 5 add WCF service reference

35,598

Solution 1

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.

Solution 2

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

Solution 3

Currently there is no tooling available for this and possible reason for this System.ServiceModel which is not available in asp.netcore5.

If you decided to use ASP.net 5 the you can do following thing as of now to use WCF service ( I am using Visual Studio 2015 CTP 5 for this answer)

In VS 2015 CTP 5 , it allow us to add reference of regular class library.

  1. Create WCF service.
  2. Create Regular Class Library ( I choose .NET Framework 4.6)
  3. After that I added WCF service reference to ClassLibrary.
  4. Add ClassLibrary as a Reference to ASP.net 5 website. ( As the CoreCLR framework does not support System.Service Model so I removed that from project.json) Framework part of project.json.

    "frameworks": {
    "aspnet50": {
        "frameworkAssemblies": {
             "System.ServiceModel": ""
         },
        "dependencies": {
             "ClassLibrary2": "1.0.0-*"
         }
       }
    },
    
  5. Now if you look at classlibrary project it contains app.config file.
  6. Copy that file and put it in wwwroot folder of ASP.net website project (vnext)
  7. rename it to web.config.

Now run your application.

Solution 4

Edit: The new extension for adding a connected service as posted in other answers still did not work for me, but I found another working configuration, though it requires you don`t use dnxcore50:

  • Have a class library holding the service reference (choose a framework <= aspnet5 used one, e.g. dnx451)
  • Reference that one into your aspnet5 with right clicking on references (will create all the wrap stuff)
  • Have Service model and needed serialization dll in "framework" section of project.json (dnxcore need to be removed)

         "dnx451": {
           "dependencies": {
            "YourClassLibWillAppearHere": "1.0.0-*" // after you reference it
           },
           "frameworkAssemblies": {
            "System.ServiceModel": "4.0.0.0",
            "System.ServiceModel.Http": "4.0.0.0",
            "System.Runtime.Serialization": "4.0.0.0"
           }
         }   
    

You should be able to do where u need it:

using YourNameSpace.ServiceReference

Old Answer:

this worked for me:

I followed both the instructions at the same time provided under known issues for beta4 (find in page "WCF") at this link:

https://github.com/aspnet/Home/releases

so my steps where:

  1. added service reference to another project (class library or windows 8.1 univ app as advised)
  2. copied the reference class to the ASP.NET 5 project
  3. copied the whole

    <system.serviceModel>...

    from app.config to web.config

  4. copied all those missing dependencies list from the link above in project.json, under common dependencies, leaving alone the specific frameworks ones (trying to reference the dotnet4.6 class library as an aspnet framework dependency gave many missing types in that framework)

"dependencies": { >> here << }

"frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  }

Solution 5

Another potential way of doing this that has worked for me is to simply add a dll project to the solution and add the service ref here as you normally would - include the dll in the MVC project and ref for the services.

You just need to copy the contents of app.config to the mvc projects own app.config - yes, app.config, not web.config.

Done

Share:
35,598
heymega
Author by

heymega

Updated on July 24, 2021

Comments

  • heymega
    heymega almost 3 years

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