1

I am trying to use the WCF in my Unity project, therefore I need to reference dll System.ServiceModel. Also, these are some more info on my environment:

  • Unity 2018.1.5f1 Personal (64bit)
  • Windows 10 (64bit)

Using msc.rsp

Following the Unity doc on loading external assemblies, I have created the msc.rsp file inside my Assets/ directory:

-r:System.ServiceModel.dll

In my Assets/ folder I also have a C# file which needs the WCF:

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; }
    }
}

There is another file where interface ICommunicationService is defined, still part of the assets inside Assets/. Did not report here as it is not that useful.

Still compile errors

Basically, nothing happens in Unity: I still get errors at compile time where clearly the assembly was not loaded at all.

Errors in Unity

Note I have also re-imported all by right-clicking on the Assets pane in Unity and clicking on Reimport All to make sure the C# project gets regenerated. I can see it is regenerated, but still nothing changes: same issues.

What am I doing wrong?

Andry
  • 16,172
  • 27
  • 138
  • 246
  • have you tried adding a reference to the assembly? check here: https://stackoverflow.com/a/5647493/1287916 – smn.tino Jun 24 '18 at 12:56
  • Shouldn't Unity take care of adding that reference? If I do that, next time I make a change and Unity regenerates the C# project, my manual changes will be overriden. – Andry Jun 24 '18 at 12:59
  • Directly changing the C# project I think is wrong because it is a generated file. If something happens I lose my changes – Andry Jun 24 '18 at 13:00
  • I have tried what you suggested, Visual Studio has no more issues and I can compile, but back to Unity, the errors are still there. When I refresh in Unity, the project gets regenerated and my changes are gone and the errors are back in Visual Studio too. I don't think that can be a valid approach :( – Andry Jun 24 '18 at 13:03

3 Answers3

0

I tried setting up a similar project where i had a mcs.rsp file with -r:System.ServiceModel.dll inside while i had a .cs reference the ServiceModel namespace, and i got the same issue.

After changing the project Scripting Runtime Version to .NET 4.0 Equivalent, and Api Compability Level to .NET 4.x it started working.

If part of the API seems to be missing, it might not be included with .NET Standard 2.0. The Project may need to use the .NET 4.x Api Compatibility Level instead.

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

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
  • Ok... I have tried to find that option in unity... where do I change the `Scripting Runtime Version`? and where to I change the `API Compatibility Level`? Cannot find them sorry – Andry Jun 24 '18 at 20:55
  • 1
    Ok found it under `Player` settings :) – Andry Jun 24 '18 at 20:59
  • 1
    By any chance, do you know why `System.ServiceModel.Web.dll` is not being loaded? I have added it to the `mcs.rsp` file and reimported everything, but the generated project does not have that dll :( – Andry Jun 24 '18 at 21:38
0

msc.rsp do include System.ServiceModel.dll by adding content

-r:System.ServiceModel.dll

for Unity Editor

However, there's a bug that dll reference won't be included in csproj : https://issuetracker.unity3d.com/issues/httpclient-namespace-is-not-recognized-in-vs-with-net-4-dot-x

You can try to fix the problem by alter the csproj file by yourself : https://learn.microsoft.com/en-us/visualstudio/cross-platform/customize-project-files-created-by-vstu?view=vs-2017

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

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

Xray
  • 1
  • 1
0

For anyone currently needing to add references to certain system assemblies to Unity in recent versions (as of 2021), the correct way to do so is add a text file at Assets\csc.rsp and put -r:System.ServiceModel.dll in it, or whatever assembly you want. You can also add #defines in this file, too. For example: -define:STAGING

Jason Hughes
  • 2,612
  • 1
  • 10
  • 10