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:UsersyouraccountDocumentsVisual Studio 2015ProjectsMapClientsrcMapClient"
Make sure MapService
is running and run the following command:
svcutil /language:cs /out:"Service ReferencesMapServiceReference.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.