ASP.NET 5 add WCF service reference
Asked Answered
B

6

28

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

Begun answered 9/2, 2015 at 15:53 Comment(2)
I think you're right :( I've updated my answer to include the version of VS.Begun
Good news! WCF is now being made available on .NET Core and is accessible under ASP.NET 5. dotnetfoundation.org/blog/wcf-is-open-source Now..when that tooling will be made available in Visual Studio...unknown.Abatement
S
44

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.

Spastic answered 10/2, 2015 at 19:47 Comment(3)
I have a bunch of those "Severity Code Description Project File Line Error CS0234 The type or namespace name 'ServiceContractAttribute' does not exist in the namespace 'System.ServiceModel' (are you missing an assembly reference?) MyProject.Web.DNX 4.5.1 C:\dev\MyProjectLibrary\MyProject.Web\Service References\EditUsersReference.cs 397", Any Idea why ?Carmellacarmelle
I'm not sure how much help I can be since I am not currently on m$ stack. Can you compile and run outside of Visual Studio @Lomithrani?Spastic
You could try this approach. I'm just testing it for a new project. blogs.msdn.microsoft.com/webdev/2016/05/25/…Elegist
A
9

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

Aman answered 21/12, 2015 at 11:54 Comment(1)
Thank you for your contribution.Begun
A
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.

Aletaaletha answered 10/2, 2015 at 3:37 Comment(1)
I can only reference a .net 5 library is it normal? I've read that now wcf was ported to .net 5 but how can I reference it in my mvc 6 project ?Carmellacarmelle
B
3

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": { }
  }
Bookseller answered 28/8, 2015 at 14:19 Comment(0)
T
3

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

Taster answered 4/6, 2016 at 8:31 Comment(2)
Nightmare wasn't it!Taster
Yep :). I am porting a project to ASP.Core but with .Net Framework 4.6.1 and the extension tool is not available if it's not .Net Framework CorePaulinapauline
B
2

If you have encountered with System.ServiceModel after WCF service is added with .NET 5. You can delete the Assemblies System.Servicemodel and add following packages with version 4.4.4 . This worked for me.

System.ServiceModel.Duplex
System.ServiceModel.Http
System.ServiceModel.NetTcp
System.ServiceModel.Security
Bernt answered 23/7, 2021 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.