hey*_*ega 26 c# asp.net wcf visual-studio-2015 asp.net-core
在Visual Studio 2015预览版(预发行版)中,如何为服务添加服务引用WCF?
Car*_*all 41
目前,这是一个相当复杂的过程,因为工具似乎不太支持生成WCF客户端代码或自动映射配置文件.此外,作为dotnetstep指出,ASP.NET开发团队并没有移植System.ServiceModel到5,但(或提供给WCF客户端的替代还没有).尽管如此,我们可以使用基于代码的方法来创建客户端代理并用于svcutil生成我们的服务引用类.
对于此示例,我将假设您在http:// localhost:5000/MapService.svc本地托管服务以实现IMapService合同.此外,我们将调用将包含服务代理的项目MapClient.
你project.json应该看起来像:
{
"commands": {
"run": "run"
},
"frameworks": {
"dnx451": {
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-beta2"
},
"frameworkAssemblies": {
"System.ServiceModel": "4.0.0.0"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先,让我们Service References在MapClient项目中创建一个文件夹.
接下来,打开VS2015的Developer Command Prompt并导航到您的MapClient项目目录:
cd "C:\Users\youraccount\Documents\Visual Studio 2015\Projects\MapClient\src\MapClient"
Run Code Online (Sandbox Code Playgroud)
确保MapService正在运行并运行以下命令:
svcutil /language:cs /out:"Service References\MapServiceReference.cs" http://localhost:5000/MapService.svc
Run Code Online (Sandbox Code Playgroud)
这应该生成两个文件,output.config和MapServiceReference.cs.
由于无法将端点和绑定配置从配置文件自动映射到ClientBase当前的ASP.NET 5,因此output.config对我们没有多大用处.你可以删除它.
相反,让我们在代码中创建一个客户端代理:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用该clientProxy实例访问任何操作合同IMapService.
作为旁注,创建密钥可能是更好的架构:值配置文件,用于存储绑定和端点配置,并使用该Microsoft.Framework.ConfigurationModel.Configuration对象填充您的,ChannelFactory以便您可以将服务配置保留在代码之外,但希望此示例将获得你开始了
有一个新的Visual Studio扩展,允许您添加和使用以前版本中的服务引用.它也与新的CoreCLR兼容,我刚刚测试过它.