简体   繁体   English

如何在 .NET Standard 项目中“添加服务引用”

[英]How to “Add Service Reference” in .NET Standard project

I would like to do "Add Service Reference" in .NET Standard project.(Visual Studio 2017)我想在 .NET Standard 项目中执行“添加服务引用”。(Visual Studio 2017)

I installed "System.ServiceModel.Http" and "System.ServiceModel.Security" on NuGet in order to make WCF access possible.我在 NuGet 上安装了“System.ServiceModel.Http”和“System.ServiceModel.Security”以使 WCF 访问成为可能。

However, there is no "Add Service Reference" menu item in the .NET Standard project.但是,.NET Standard 项目中没有“添加服务引用”菜单项。 How do I add a service reference?如何添加服务引用?

It exists in the .NET Framework project but it does not exist in the .NET Standard project, so it is in trouble.它存在于.NET Framework 项目中,但不存在于.NET Standard 项目中,因此它有问题。

I landed here hoping to solve a slightly different problem... but to maybe answer your question;我来到这里希望解决一个稍微不同的问题......但也许可以回答你的问题;

I had to update VS2017 to the latest version (I'm now on 15.5.2), then;我必须将 VS2017 更新到最新版本(我现在是 15.5.2),然后; Right-Click the project >> Add >> Connected Service, then click "Microsoft WCF Web Service Reference Provider".右键单击项目>>添加>>连接的服务,然后单击“Microsoft WCF Web Service Reference Provider”。 The provided dialog is very similar to that of the Framework "Add Service Reference" option.提供的对话框与框架“添加服务引用”选项的对话框非常相似。

It's the same "Add" menu you would use if you were to add a new class etc...如果您要添加新课程等,它与您将使用的“添加”菜单相同......

This was added in verion 15.5.这是在 15.5 版中添加的。 See WCF on github for more info.有关更多信息,请参阅github 上的 WCF

Visual Studio 2017 Community v15.9.7 Visual Studio 2017 社区 v15.9.7

Solution Explorer -> Right click Dependencies -> Add Connected Service解决方案资源管理器 -> 右键单击依赖项-> 添加连接的服务

ScreenShot:截屏:

These solutions didn't really work for me.这些解决方案对我来说并不真正有效。 I was using this with Unity 2019.1.10f and Visual Studio 2017. I found what you need to do is add the dll's that relate to WCF to your Unity project and then generate the service client proxy and bring that over to your scripts.我在 Unity 2019.1.10f 和 Visual Studio 2017 中使用它。我发现您需要做的是将与 WCF 相关的 dll 添加到您的 Unity 项目,然后生成服务客户端代理并将其带到您的脚本中。 Step by Step below.下面一步一步来。

  1. Create a new Unity 3D project, or open yours, then create a new folder under Assets called Plugins.创建一个新的 Unity 3D 项目,或打开您的项目,然后在 Assets 下创建一个名为 Plugins 的新文件夹。
  2. Navigate to the installation folder of Unity (eg C:\\Program Files\\Unity\\Hub\\Editor\\2019.1.10f1).导航到Unity的安装文件夹(例如C:\\Program Files\\Unity\\Hub\\Editor\\2019.1.10f1)。
  3. From the installation folder, navigate to the the Editor\\Data\\Mono\\lib\\mono\\2.0, in this folder you should find System.ServiceModel.dll, you need to copy this file into the Plugins folder created in step 1.从安装文件夹,导航到Editor\\Data\\Mono\\lib\\mono\\2.0,在这个文件夹中你应该找到System.ServiceModel.dll,你需要把这个文件复制到步骤1中创建的Plugins文件夹中。
  4. Now generate the the service client proxy, you can do this in a few ways, one option is to use svcutil, for example run the command below in a VS command prompt to generate the client proxy class.现在生成服务客户端代理,您可以通过几种方式来完成,一种选择是使用 svcutil,例如在 VS 命令提示符下运行以下命令来生成客户端代理类。

    svcutil -out:c:\\temp\\ClientProxy.cs https://[YourWebServiceDomain]/[Service].svc

  5. Copy the ClientProxy.cs file above into your project wherever you like under assets.将上面的 ClientProxy.cs 文件复制到您的项目中您喜欢的资产下。

  6. Now add a new Monobehaviour script, something like WebClient.cs into your project.现在添加一个新的 Monobehaviour 脚本,比如 WebClient.cs 到你的项目中。 You'll need to attach this to some game object in your scene for the script below to run.您需要将它附加到场景中的某个游戏对象上,以便运行下面的脚本。
  7. Open up the WebClient.cs and add your code to connect to the new proxy service, example below.打开 WebClient.cs 并添加您的代码以连接到新的代理服务,示例如下。
using UnityEngine;
using System.ServiceModel;
using YourClientProxyNamespace;

public class WebClient : MonoBehavior
{
  void Start()
  {
    using (ProxyClient client = new ProxyClient(
        new BasicHttpBinding(BasicHttpSecurityMode.Transport),
        new EndpointAddress("https://YourWebServiceDomain/Service.svc")))
    {
      var response = client.DesiredMethod();

      // Do whatever with the response
    }    
  }
}

实际上你可以右键单击,转到“添加连接服务”,然后单击“Microsoft WCF Web 服务引用提供程序”,它应该与“添加服务”相同。

This exercise is based on Use the WCF Web Service Reference Provider Tool本练习基于使用 WCF Web 服务引用提供程序工具

Environment:环境:

I have consumed asmx web service, but the documentation said: its the same whit WCF service.我使用了 asmx Web 服务,但文档说:它与 WCF 服务相同。 I think it apply to all SOAP service.我认为它适用于所有 SOAP 服务。

  1. Add service reference (using WCF Web Service Reference Provider Tool).添加服务引用(使用 WCF Web 服务引用提供程序工具)。
  2. Add package references:添加包引用:

1) Steeps to add service reference: 1) 添加服务引用的步骤:

Project> Add connected Service项目> 添加连接的服务

添加连接的服务

Add WCF service Reference to Project.将 WCF 服务引用添加到项目。

添加 WCF 参考

发现

命名

包裹

您可以通过将您的服务拖入 bin 目录来手动添加它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM