ASP.NET 5添加WCF服务引用

时间:2021-01-01 16:49:15

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

在Visual Studio 2015预览版(预发行版)中,如何为WCF服务添加服务引用?

5 个解决方案

#1


41  

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.

目前,这是一个相当复杂的过程,因为工具似乎不太支持生成WCF客户端代码或自动映射配置文件。此外,正如dotnetstep指出的那样,ASP.NET团队还没有将System.ServiceModel移植到5(或者为WCF客户端提供了替代方案)。尽管如此,我们可以使用基于代码的方法来创建客户端代理并使用svcutil来生成我们的服务引用类。

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.

对于此示例,我将假设您在http:// localhost:5000 / MapService.svc本地托管服务,该服务实现IMapService合同。此外,我们将调用将包含服务代理MapClient的项目。

Your project.json should look something like:

你的project.json应该是这样的:

{
    "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.

首先,让我们在MapClient项目中创建一个文件夹Service References。

Next, open up Developer Command Prompt for VS2015 and navigate to your MapClient project directory:

接下来,打开VS2015的Developer Command Prompt并导航到MapClient项目目录:

cd "C:\Users\youraccount\Documents\Visual Studio 2015\Projects\MapClient\src\MapClient"

Make sure MapService is running and run the following command:

确保MapService正在运行并运行以下命令:

svcutil /language:cs /out:"Service References\MapServiceReference.cs" http://localhost:5000/MapService.svc

That should generate two files, output.config and MapServiceReference.cs.

这应该生成两个文件,output.config和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.

由于无法将端点和绑定配置从配置文件自动映射到当前在ASP.NET 5中的ClientBase,因此output.config对我们没有多大用处。你可以删除它。

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.

现在,您可以使用clientProxy实例访问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.

作为旁注,创建密钥可能是更好的架构:值配置文件,用于存储绑定和端点配置,并使用Microsoft.Framework.ConfigurationModel.Configuration对象填充您的ChannelFactory,以便您可以将服务配置保留在代码,但希望这个例子能让你开始。

#2


9  

There is a new Visual Studio extension which allows you to add and use service references like in previous versions. It is also compatible with the new CoreCLR, I have just tested it.

有一个新的Visual Studio扩展,允许您添加和使用以前版本中的服务引用。它也与新的CoreCLR兼容,我刚刚测试过它。

http://blogs.msdn.com/b/webdev/archive/2015/12/15/wcf-connected-service-visual-studio-extension-preview-for-asp-net-5-projects.aspx

http://blogs.msdn.com/b/webdev/archive/2015/12/15/wcf-connected-service-visual-studio-extension-preview-for-asp-net-5-projects.aspx

#3


3  

Currently there is no tooling available for this and possible reason for this System.ServiceModel which is not available in asp.netcore5.

目前没有可用的工具和可能的原因,这个System.ServiceModel在asp.netcore5中不可用。

If you decided to use ASP.net 5 the you can do following thing as of now to use WCF service ( I am using Visual Studio 2015 CTP 5 for this answer)

如果您决定使用ASP.net 5,那么您现在可以执行以下操作来使用WCF服务(我使用Visual Studio 2015 CTP 5来获得此答案)

In VS 2015 CTP 5 , it allow us to add reference of regular class library.

在VS 2015 CTP 5中,它允许我们添加常规类库的引用。

  1. Create WCF service.
  2. 创建WCF服务。
  3. Create Regular Class Library ( I choose .NET Framework 4.6)
  4. 创建常规类库(我选择.NET Framework 4.6)
  5. After that I added WCF service reference to ClassLibrary.
  6. 之后,我将WCF服务引用添加到ClassLibrary。
  7. Add ClassLibrary as a Reference to ASP.net 5 website. ( As the CoreCLR framework does not support System.Service Model so I removed that from project.json) Framework part of project.json.

    添加ClassLibrary作为ASP.net 5网站的参考。 (因为CoreCLR框架不支持System.Service模型所以我从project.json中删除了它)project.json的框架部分。

    "frameworks": {
    "aspnet50": {
        "frameworkAssemblies": {
             "System.ServiceModel": ""
         },
        "dependencies": {
             "ClassLibrary2": "1.0.0-*"
         }
       }
    },
    
  8. Now if you look at classlibrary project it contains app.config file.
  9. 现在,如果您查看classlibrary项目,它包含app.config文件。
  10. Copy that file and put it in wwwroot folder of ASP.net website project (vnext)
  11. 复制该文件并将其放在ASP.net网站项目的wwwroot文件夹中(vnext)
  12. rename it to web.config.
  13. 将其重命名为web.config。

Now run your application.

现在运行你的应用程序

#4


3  

Edit: The new extension for adding a connected service as posted in other answers still did not work for me, but I found another working configuration, though it requires you don`t use dnxcore50:

编辑:添加连接服务的新扩展程序在其他答案中发布仍然不适合我,但我找到了另一个工作配置,但它要求你不要使用dnxcore50:

  • Have a class library holding the service reference (choose a framework <= aspnet5 used one, e.g. dnx451)
  • 有一个包含服务引用的类库(选择一个框架<= aspnet5使用一个,例如dnx451)
  • Reference that one into your aspnet5 with right clicking on references (will create all the wrap stuff)
  • 通过右键单击引用将一个引用到您的aspnet5中(将创建所有包装内容)
  • Have Service model and needed serialization dll in "framework" section of project.json (dnxcore need to be removed)

    在project.json的“框架”部分中有服务模型和所需的序列化dll(需要删除dnxcore)

         "dnx451": {
           "dependencies": {
            "YourClassLibWillAppearHere": "1.0.0-*" // after you reference it
           },
           "frameworkAssemblies": {
            "System.ServiceModel": "4.0.0.0",
            "System.ServiceModel.Http": "4.0.0.0",
            "System.Runtime.Serialization": "4.0.0.0"
           }
         }   
    

You should be able to do where u need it:

你应该能够在你需要的地方做到:

using YourNameSpace.ServiceReference

Old Answer:

旧答案:

this worked for me:

这对我有用:

I followed both the instructions at the same time provided under known issues for beta4 (find in page "WCF") at this link:

我在此链接上同时遵循了针对beta4(在页面“WCF”中找到)的已知问题提供的说明:

https://github.com/aspnet/Home/releases

https://github.com/aspnet/Home/releases

so my steps where:

所以我的步骤在哪里:

  1. added service reference to another project (class library or windows 8.1 univ app as advised)
  2. 添加了对另一个项目的服务引用(类库或Windows 8.1 univ app,如建议的那样)
  3. copied the reference class to the ASP.NET 5 project
  4. 将引用类复制到ASP.NET 5项目
  5. copied the whole

    复制了整个

    <system.serviceModel>...

    ...

    from app.config to web.config

    从app.config到web.config

  6. copied all those missing dependencies list from the link above in project.json, under common dependencies, leaving alone the specific frameworks ones (trying to reference the dotnet4.6 class library as an aspnet framework dependency gave many missing types in that framework)

    在公共依赖项下,从project.json中的上面的链接中复制了所有那些缺少的依赖项列表,只留下了特定的框架(尝试引用dotnet4.6类库作为aspnet框架依赖项,在该框架中提供了许多缺少的类型)

"dependencies": { >> here << }

“依赖”:{>> here <<}

"frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  }

#5


3  

Another potential way of doing this that has worked for me is to simply add a dll project to the solution and add the service ref here as you normally would - include the dll in the MVC project and ref for the services.

另一种对我有用的潜在方法是简单地将dll项目添加到解决方案中并像往常一样添加服务引用 - 在MVC项目中包含dll并为服务引用。

You just need to copy the contents of app.config to the mvc projects own app.config - yes, app.config, not web.config.

您只需将app.config的内容复制到mvc项目自己的app.config - 是,app.config,而不是web.config。

Done

完成

#1


41  

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.

目前,这是一个相当复杂的过程,因为工具似乎不太支持生成WCF客户端代码或自动映射配置文件。此外,正如dotnetstep指出的那样,ASP.NET团队还没有将System.ServiceModel移植到5(或者为WCF客户端提供了替代方案)。尽管如此,我们可以使用基于代码的方法来创建客户端代理并使用svcutil来生成我们的服务引用类。

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.

对于此示例,我将假设您在http:// localhost:5000 / MapService.svc本地托管服务,该服务实现IMapService合同。此外,我们将调用将包含服务代理MapClient的项目。

Your project.json should look something like:

你的project.json应该是这样的:

{
    "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.

首先,让我们在MapClient项目中创建一个文件夹Service References。

Next, open up Developer Command Prompt for VS2015 and navigate to your MapClient project directory:

接下来,打开VS2015的Developer Command Prompt并导航到MapClient项目目录:

cd "C:\Users\youraccount\Documents\Visual Studio 2015\Projects\MapClient\src\MapClient"

Make sure MapService is running and run the following command:

确保MapService正在运行并运行以下命令:

svcutil /language:cs /out:"Service References\MapServiceReference.cs" http://localhost:5000/MapService.svc

That should generate two files, output.config and MapServiceReference.cs.

这应该生成两个文件,output.config和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.

由于无法将端点和绑定配置从配置文件自动映射到当前在ASP.NET 5中的ClientBase,因此output.config对我们没有多大用处。你可以删除它。

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.

现在,您可以使用clientProxy实例访问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.

作为旁注,创建密钥可能是更好的架构:值配置文件,用于存储绑定和端点配置,并使用Microsoft.Framework.ConfigurationModel.Configuration对象填充您的ChannelFactory,以便您可以将服务配置保留在代码,但希望这个例子能让你开始。

#2


9  

There is a new Visual Studio extension which allows you to add and use service references like in previous versions. It is also compatible with the new CoreCLR, I have just tested it.

有一个新的Visual Studio扩展,允许您添加和使用以前版本中的服务引用。它也与新的CoreCLR兼容,我刚刚测试过它。

http://blogs.msdn.com/b/webdev/archive/2015/12/15/wcf-connected-service-visual-studio-extension-preview-for-asp-net-5-projects.aspx

http://blogs.msdn.com/b/webdev/archive/2015/12/15/wcf-connected-service-visual-studio-extension-preview-for-asp-net-5-projects.aspx

#3


3  

Currently there is no tooling available for this and possible reason for this System.ServiceModel which is not available in asp.netcore5.

目前没有可用的工具和可能的原因,这个System.ServiceModel在asp.netcore5中不可用。

If you decided to use ASP.net 5 the you can do following thing as of now to use WCF service ( I am using Visual Studio 2015 CTP 5 for this answer)

如果您决定使用ASP.net 5,那么您现在可以执行以下操作来使用WCF服务(我使用Visual Studio 2015 CTP 5来获得此答案)

In VS 2015 CTP 5 , it allow us to add reference of regular class library.

在VS 2015 CTP 5中,它允许我们添加常规类库的引用。

  1. Create WCF service.
  2. 创建WCF服务。
  3. Create Regular Class Library ( I choose .NET Framework 4.6)
  4. 创建常规类库(我选择.NET Framework 4.6)
  5. After that I added WCF service reference to ClassLibrary.
  6. 之后,我将WCF服务引用添加到ClassLibrary。
  7. Add ClassLibrary as a Reference to ASP.net 5 website. ( As the CoreCLR framework does not support System.Service Model so I removed that from project.json) Framework part of project.json.

    添加ClassLibrary作为ASP.net 5网站的参考。 (因为CoreCLR框架不支持System.Service模型所以我从project.json中删除了它)project.json的框架部分。

    "frameworks": {
    "aspnet50": {
        "frameworkAssemblies": {
             "System.ServiceModel": ""
         },
        "dependencies": {
             "ClassLibrary2": "1.0.0-*"
         }
       }
    },
    
  8. Now if you look at classlibrary project it contains app.config file.
  9. 现在,如果您查看classlibrary项目,它包含app.config文件。
  10. Copy that file and put it in wwwroot folder of ASP.net website project (vnext)
  11. 复制该文件并将其放在ASP.net网站项目的wwwroot文件夹中(vnext)
  12. rename it to web.config.
  13. 将其重命名为web.config。

Now run your application.

现在运行你的应用程序

#4


3  

Edit: The new extension for adding a connected service as posted in other answers still did not work for me, but I found another working configuration, though it requires you don`t use dnxcore50:

编辑:添加连接服务的新扩展程序在其他答案中发布仍然不适合我,但我找到了另一个工作配置,但它要求你不要使用dnxcore50:

  • Have a class library holding the service reference (choose a framework <= aspnet5 used one, e.g. dnx451)
  • 有一个包含服务引用的类库(选择一个框架<= aspnet5使用一个,例如dnx451)
  • Reference that one into your aspnet5 with right clicking on references (will create all the wrap stuff)
  • 通过右键单击引用将一个引用到您的aspnet5中(将创建所有包装内容)
  • Have Service model and needed serialization dll in "framework" section of project.json (dnxcore need to be removed)

    在project.json的“框架”部分中有服务模型和所需的序列化dll(需要删除dnxcore)

         "dnx451": {
           "dependencies": {
            "YourClassLibWillAppearHere": "1.0.0-*" // after you reference it
           },
           "frameworkAssemblies": {
            "System.ServiceModel": "4.0.0.0",
            "System.ServiceModel.Http": "4.0.0.0",
            "System.Runtime.Serialization": "4.0.0.0"
           }
         }   
    

You should be able to do where u need it:

你应该能够在你需要的地方做到:

using YourNameSpace.ServiceReference

Old Answer:

旧答案:

this worked for me:

这对我有用:

I followed both the instructions at the same time provided under known issues for beta4 (find in page "WCF") at this link:

我在此链接上同时遵循了针对beta4(在页面“WCF”中找到)的已知问题提供的说明:

https://github.com/aspnet/Home/releases

https://github.com/aspnet/Home/releases

so my steps where:

所以我的步骤在哪里:

  1. added service reference to another project (class library or windows 8.1 univ app as advised)
  2. 添加了对另一个项目的服务引用(类库或Windows 8.1 univ app,如建议的那样)
  3. copied the reference class to the ASP.NET 5 project
  4. 将引用类复制到ASP.NET 5项目
  5. copied the whole

    复制了整个

    <system.serviceModel>...

    ...

    from app.config to web.config

    从app.config到web.config

  6. copied all those missing dependencies list from the link above in project.json, under common dependencies, leaving alone the specific frameworks ones (trying to reference the dotnet4.6 class library as an aspnet framework dependency gave many missing types in that framework)

    在公共依赖项下,从project.json中的上面的链接中复制了所有那些缺少的依赖项列表,只留下了特定的框架(尝试引用dotnet4.6类库作为aspnet框架依赖项,在该框架中提供了许多缺少的类型)

"dependencies": { >> here << }

“依赖”:{>> here <<}

"frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  }

#5


3  

Another potential way of doing this that has worked for me is to simply add a dll project to the solution and add the service ref here as you normally would - include the dll in the MVC project and ref for the services.

另一种对我有用的潜在方法是简单地将dll项目添加到解决方案中并像往常一样添加服务引用 - 在MVC项目中包含dll并为服务引用。

You just need to copy the contents of app.config to the mvc projects own app.config - yes, app.config, not web.config.

您只需将app.config的内容复制到mvc项目自己的app.config - 是,app.config,而不是web.config。

Done

完成