Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

c# - ASP.NET 5 add WCF service reference

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...