0

This is basically an aftermath question after this question I posted yesterday. Long story short, I had troubles referencing System.ServiceModel in my Unity application and I got that fixed by having this mcs.rsp file:

-r:System.ServiceModel.dll

And by having Scripting Runtime Version and Api Compability Level settings aligned to .NET 4.x. However, now I am facing another issue.

Unity project builds fine!

So, let me first point out that my project runs fine without compile errors. So on that regard it is just fine. The problem I am facing is an annoyance that I am getting in Visual Studio and impacts my development experience.

Errors in Visual Studio

Basically when I open the C# project in Visual Studio, I get type errors like this:

enter image description here

Even though assembly System.ServiceModel is now correctly loaded in the project, Visual Studio shows me errors complaining about the fact that those highlighted types are not available.

The type or namespace "ServiceHost" could not be found (are you missing a using directive or an assembly reference?)

Why? This is extremely annoying when developing, I cannot possibly carry out a whole development with fake errors showing up like this :(

A few more info...

If I inspect generated project file Assembly-CSharp.csproj, I can see this:

<Reference Include="System.ServiceModel.Duplex">
    <HintPath>C:/Program Files/Unity/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/Facades/System.ServiceModel.Duplex.dll</HintPath>
</Reference>
<Reference Include="System.ServiceModel.Http">
   <HintPath>C:/Program Files/Unity/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/Facades/System.ServiceModel.Http.dll</HintPath>
</Reference>
<Reference Include="System.ServiceModel.NetTcp">
    <HintPath>C:/Program Files/Unity/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/Facades/System.ServiceModel.NetTcp.dll</HintPath>
</Reference>
<Reference Include="System.ServiceModel.Primitives">
    <HintPath>C:/Program Files/Unity/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/Facades/System.ServiceModel.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.ServiceModel.Security">
    <HintPath>C:/Program Files/Unity/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/Facades/System.ServiceModel.Security.dll</HintPath>
</Reference>

So it makes me wonder:

  • Seems like Unity is pointing to the mono generated dlls of the required dlls. However where is System.ServiceModel.dll?
  • I can see there is System.ServiceModel.dll in the folder containing Facades, why isn't that used?
Andry
  • 16,172
  • 27
  • 138
  • 246
  • Mono has “limited” WCF support if that is what you are doing with System.ServiceModel https://www.mono-project.com/docs/about-mono/compatibility/ –  Jun 25 '18 at 06:27
  • @User453465436754: Yes I see :( But I wonder how developers achieve online gamig in Unity. How do they operate the communication between remote players? – Andry Jun 25 '18 at 07:37
  • Games use very efficient network lean; speedy and often proprietary (nothing wrong with that) protocols as opposed to anything SOAP; REST; or Protobuf have to offer, hence no reliance on `System.ServiceModel.dll` –  Jun 25 '18 at 12:59
  • @User453465436754: I see. Just out of curiosity, can you link a reference about how to build this connectivity in a game with Unity? I am just curious to see if I can maybe look at that practice to solve my problem. – Andry Jun 25 '18 at 20:22
  • 1
    Best bet is to have a look over at [gamedev](http://gamedev.net). Generally you will find peeps use IP Datagrams for it's broadcast and faster aspects –  Jun 26 '18 at 01:23
  • 1
    For this specific error, checkout https://stackoverflow.com/questions/24040904/cannot-find-namespace-servicemodel-with-unity –  Jun 26 '18 at 03:15

1 Answers1

1

You get the error because you are not using the proper dll. In order to use anything from the System.ServiceModel and System.ServiceModel.Description namespace, you must reference the proper mono dll that comes with Unity.

First, change the Scripting Runtime Version and Api Compatibility Level to 4.x or .NET 4.6 Equivalent (Options depends on Unity version).

Go to to <UnityInstallationDirectory>\Editor\Data\MonoBleedingEdge\lib\mono\4.5 or for example, C:\Program Files\Unity\Editor\Data\MonoBleedingEdge\lib\mono\4.5 on my computer.

Once in this directory, copy System.ServiceModel.dll to your <Project>\Assets path and you should be able to use ServiceHost and ServiceEndpoint API.

You may still get a run-time error or exception since this is mono but this should fix your current compile-time error. Don't forget to undo what you've already done in your question before doing what's in this answer.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    Quoting from [Doc](https://docs.unity3d.com/Manual/dotnetProfileAssemblies.html): "You should reference class library assemblies as described in the example above. Don’t copy them into the Project directory." – Andry Jun 25 '18 at 20:10
  • I wanted to edit the comment at the beginning because I am just angry that this is not working but StackOverflow does not let change comments after 5 minutes. I seriously owe you an apology for those first words. I wanted to change them into: "This is not the solution I was looking for unfortunately". Please accept my apologies for being rude! Seriously. Again, I am just frustrated because I've been spending the whole day on this. I am very very sorry. – Andry Jun 25 '18 at 20:43
  • That being said, what I was trying to say, sorry out of anger, is that this is probably not the best practice here. I am not looking for a hack unfortunately but for a best practice. I just think it is very strange that Unity does not reference the proper dlls when knowing that a certain set of assemblies is not supported in the current profile. The `System.ServiceModel` dll is available also in different versions, like in version 3.5. f the one in 4.x is not completely supported, they could generate the project to reference the supported one. Unless no WCF is supported at all, then that's it – Andry Jun 25 '18 at 20:46