[英]Cannot reference System.ServiceModel in Unity
我试图在我的 Unity 项目中使用WCF ,因此我需要引用 dll System.ServiceModel
。 此外,这些是有关我的环境的更多信息:
按照有关加载外部程序集的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 中右键单击“ Assets
窗格并单击“ Reimport All
来Reimport All
,以确保重新生成 C# 项目。 我可以看到它已重新生成,但仍然没有任何变化:相同的问题。
我究竟做错了什么?
我尝试建立一个类似的项目,其中有一个带有-r:System.ServiceModel.dll
的mcs.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/
对于当前需要在最新版本(截至 2021 年)中向 Unity 添加对某些系统程序集的引用的任何人,正确的方法是在Assets\\csc.rsp
添加一个文本文件并将-r:System.ServiceModel.dll
放入它,或任何你想要的程序集。 您也可以在此文件中添加 #defines。 例如: -define:STAGING
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.