ASP.NET 5 添加 WCF 服务引用 [英] ASP.NET 5 add WCF service reference
问题描述
在 Visual Studio 2015 预览版(预发布版)中,如何为 WCF
服务添加服务引用?
In Visual Studio 2015 Preview (Pre Release), how can I add a service reference for a WCF
service?
推荐答案
目前,这是一个相当复杂的过程,因为该工具似乎不太支持生成 WCF 客户端代码或从配置文件自动映射的方式.此外,正如 dotnetstep 指出的那样,ASP.NET 团队尚未将 System.ServiceModel
移植到 5(或为 WCF 客户端提供替代).尽管如此,我们可以使用基于代码的方法来创建客户端代理并使用 svcutil
来生成我们的服务引用类.
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.
对于本示例,我假设您在 http://localhost:5000/MapService 上本地托管服务.svc 实现了一个 IMapService
契约.此外,我们将调用将包含服务代理的项目 MapClient
.
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
.
您的 project.json
应该类似于:
{
"commands": {
"run": "run"
},
"frameworks": {
"dnx451": {
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-beta2"
},
"frameworkAssemblies": {
"System.ServiceModel": "4.0.0.0"
}
}
}
}
生成服务引用类
首先,让我们在 MapClient
项目中创建一个文件夹,Service References
.
Generate the Service Reference Classes
First, let's create a folder, Service References
, in the MapClient
project.
接下来,打开VS2015的Developer Command Prompt并导航到您的MapClient
项目目录:
Next, open up Developer Command Prompt for VS2015 and navigate to your MapClient
project directory:
cd "C:UsersyouraccountDocumentsVisual Studio 2015ProjectsMapClientsrcMapClient"
确保 MapService
正在运行并运行以下命令:
Make sure MapService
is running and run the following command:
svcutil /language:cs /out:"Service ReferencesMapServiceReference.cs" http://localhost:5000/MapService.svc
这应该会生成两个文件,output.config
和 MapServiceReference.cs
.
That should generate two files, output.config
and MapServiceReference.cs
.
由于无法自动将端点和绑定配置从配置文件映射到当前在 ASP.NET 5 中的 ClientBase
,output.config
对我们没有多大用处.你可以删除它.
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();
}
}
}
现在您可以使用 clientProxy
实例访问 IMapService
中的任何操作合约.
Now you can use the clientProxy
instance to access any Operation Contract in IMapService
.
作为旁注,创建一个 key:value 配置文件来存储您的绑定和端点配置并使用 Microsoft.Framework.ConfigurationModel.Configuration
对象来填充您的 ChannelFactory
这样您就可以将服务配置排除在代码之外,但希望这个示例可以帮助您入门.
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 添加 WCF 服务引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!