繁体   English   中英

无法在 Unity 中引用 System.ServiceModel

[英]Cannot reference System.ServiceModel in Unity

我试图在我的 Unity 项目中使用WCF ,因此我需要引用 dll System.ServiceModel 此外,这些是有关我的环境的更多信息:

  • Unity 2018.1.5f1 个人版(64 位)
  • 视窗 10(64 位)

使用 msc.rsp

按照有关加载外部程序集Unity 文档,我在Assets/目录中创建了msc.rsp文件:

-r:System.ServiceModel.dll

在我的Assets/文件夹中,我还有一个需要 WCF 的 C# 文件:

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

public class CommunicationEndpoint {
    private const short Port = 8000;
    private ServiceHost host;

    public void Start() {
        this.EnsureInitilized();
        host.Open();
    }

    public void Stop() {
        if (this.host != null) return;
        host.Close();
    }

    private void EnsureInitilized() {
        if (this.host != null) return;

        this.host = new ServiceHost(typeof(CommunicationService));
        host.AddServiceEndpoint(typeof(ICommunicationService), new BasicHttpBinding(), Address);

        host.Open();
    }

    private string Address {
        get { return "http://localhost:" + Port; }
    }
}

还有另一个定义了ICommunicationService接口的文件,它仍然是Assets/资产的一部分。 没有在这里报告,因为它不是那么有用。

还是编译错误

基本上,在 Unity 中什么也没有发生:我仍然在编译时遇到错误,显然程序集根本没有加载。

Unity 中的错误

注意我还通过在 Unity 中右键单击“ Assets窗格并单击“ Reimport AllReimport All ,以确保重新生成 C# 项目。 我可以看到它已重新生成,但仍然没有任何变化:相同的问题。

我究竟做错了什么?

我尝试建立一个类似的项目,其中有一个带有-r:System.ServiceModel.dllmcs.rsp文件,而我有一个.cs引用ServiceModel命名空间的文件,并且遇到了同样的问题。

将项目Scripting Runtime Version更改为.NET 4.0 Equivalent ,并将Api Compability Level更改为.NET 4.x它开始工作。

如果API的一部分似乎丢失了,则.NET Standard 2.0中可能未包含它。 该项目可能需要使用.NET 4.x Api兼容性级别。

https://docs.unity3d.com/Manual/dotnetProfileAssemblies.html

msc.rsp 包括System.ServiceModel.dll通过添加内容

-r:System.ServiceModel.dll

用于Unity编辑器

但是,存在一个错误,DLL引用将不会包含在csproj中: https ://issuetracker.unity3d.com/issues/httpclient-namespace-is-not-recognized-in-vs-with-net-4-dot- X

您可以尝试通过自己更改csproj文件来解决此问题: https ://docs.microsoft.com/zh-cn/visualstudio/cross-platform/customize-project-files-created-by-vstu?view = vs -2017

参考: https : //forum.unity.com/threads/using-dynamic-keyword-not-working.490600/

https://forum.unity.com/threads/httpclient.460748/

对于当前需要在最新版本(截至 2021 年)中向 Unity 添加对某些系统程序集的引用的任何人,正确的方法是在Assets\\csc.rsp添加一个文本文件并将-r:System.ServiceModel.dll放入它,或任何你想要的程序集。 您也可以在此文件中添加 #defines。 例如: -define:STAGING

暂无
暂无

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

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