ASP.NET 5添加WCF服务参考 [英] ASP.NET 5 add WCF service reference
问题描述
在Visual Studio 2015年preVIEW(pre释放),我怎么能增加对 WCF
服务的服务引用?
In Visual Studio 2015 Preview (Pre Release), how can I add a service reference for a WCF
service?
推荐答案
目前,这是一个相当复杂的过程,因为工具似乎不支持多生成WCF客户端code的方式或自动配置映射文件。此外,由于dotnetstep指出,ASP.NET开发团队还没有移植 System.ServiceModel
5还没有(或提供给WCF客户端的替代尚未)。尽管如此,我们可以使用code为基础的方法来创建一个客户端代理,并使用的 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服务://本地主机: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
项目。
接下来,打开的为VS2015开发人员命令提示符的并导航到 MapClient
项目目录:
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"
确认的MapService
正在运行,并且运行下面的命令:
Make sure MapService
is running and run the following command:
svcutil /language:cs /out:"Service References\MapServiceReference.cs" http://localhost:5000/MapService.svc
这应该生成两个文件, output.config
和 MapServiceReference.cs
。
That should generate two files, output.config
and MapServiceReference.cs
.
由于没有办法的自动地的地图端点和绑定配置从一个配置文件到您的 ClientBase
在ASP.NET 5,目前 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.
相反,让我们创建在code客户端代理:
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
.
一点题外话,它很可能是更好的架构,以创建密钥:存储你的绑定和端点配置和使用<值配置文件href=\"https://github.com/aspnet/Configuration/blob/dev/src/Microsoft.Framework.ConfigurationModel/Configuration.cs\"><$c$c>Microsoft.Framework.ConfigurationModel.Configuration$c$c>对象来填充你的的ChannelFactory
这样你就可以保持你的服务配置出你的code的,但希望这个例子将让你开始。
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屋!