ASP.NET 5 Add Wcf Service Reference

ASP.NET 5 add WCF service reference

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.

ASP.NET 5 add WCF service reference

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.

Add WCF service to ASP.NET Core MVC application

That is currently not possible. The WCF app has to be a separate app.

.net core Update Service Reference missing

And as for the answer, posted by a MS person on a post in their forum:

The behavior you’ve observed that “Microsoft WCF Web Service Reference
Provider” isn’t there on 16.10.4 is a change by-design. Start from
16.11, you are supposed to add a service reference via right clicking solution and then choose “Add” -> “Connected Service”, then in the
left panel click “Service References (OpenAPI, gRPC, WCF Web
Service)”, in the popped up list select “WCF Web Service” and go
though the wizards to add a service reference.

To update an existing service, open the “Service References (OpenAPI,
gRPC, WCF Web Service)” panel, click “…” beside the existing service
which you want to update and then select “Edit” in the dropped down
menu item, complete the popped up wizards to update that service.



Related Topics



Leave a reply



Submit