In Visual Studio 2015 Preview (Pre Release), how can I add a service reference for a WCF
service?
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.
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.
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.
- Create WCF service.
- Create Regular Class Library ( I choose .NET Framework 4.6)
- After that I added WCF service reference to ClassLibrary.
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-*" } } },
- Now if you look at classlibrary project it contains app.config file.
- Copy that file and put it in wwwroot folder of ASP.net website project (vnext)
- rename it to web.config.
Now run your application.
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:
- added service reference to another project (class library or windows 8.1 univ app as advised)
- copied the reference class to the ASP.NET 5 project
copied the whole
<system.serviceModel>...
from app.config to web.config
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": { }
}
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
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
© 2022 - 2024 — McMap. All rights reserved.