- Home /
How to use System.Net with code stripping?
Setting Unity's stripping level to Strip Assemblies or Strip ByteCode from Player Settings -> Optimization causes (at least) some of the System.Net functionality fail.
To test the issue, I created a simple project that only tries to use System.Net.WebRequest.Create():
using UnityEngine;
using System.Net;
public class NetworkAccessTest : MonoBehaviour
{
void Start()
{
bool succeeded = false;
try
{
WebRequest.Create("http://www.google.com");
succeeded = true;
}
catch (System.Exception e)
{
Debug.Log(string.Format("Exception: {0}", e));
succeeded = false;
}
Debug.Log(string.Format("Web request creation succeeded: {0}", succeeded));
}
}
It works fine without stripping. But without sripping:
Exception: System.NotSupportedException: http://www.google.com/
at System.Net.WebRequest.GetCreator (System.String prefix) [0x00000] in :0
at System.Net.WebRequest.Create (System.Uri requestUri) [0x00000] in :0
at System.Net.WebRequest.Create (System.String requestUriString) [0x00000] in :0
at NetworkAccessTest.Start () [0x00000] in :0
The questions:
Is it possible to use WebRequest and other networking classes from System.Net with code stripping?
And if it's possible, how?
It's possible to list types that shouldn't be stripped: docs.unity3d.com/Documentation/Manual/iphone-playerSizeOptimization.html. Would that solve the issue? How would I conveniently find out which types to list there? I can only think of reading the Mono source code and trying to deduce the missing types that could cause the expections.
Answer by Antti · Jan 09, 2014 at 12:20 PM
Adding the following link.xml to Assets direcotry solves my little test case:
<linker>
<assembly fullname="System">
<type fullname="System.Net.HttpRequestCreator" preserve="all"/>
</assembly>
</linker>
I found the needed type by reading Mono sources for System.Net. Finding all necessary types for a large application is a pain in a neck, though, so any ideas on making that easier would be still appreciated.
The method here sounds like it would work. I haven't tried it myself, but I plan to later today.
Hi @Antti - sorry to bring up an old thread but how did you pinpoint the HttpRequestCreator as the culprit? I've been debugging our own stripping errors - currently stuck on one due to a file stream request over http - and I cannot find the type name (i.e. System.IO.StreamRequestCreator) or something similar. $$anonymous$$SDN is incredibly unhelpful for this endeavour! Your little link.xml did however solve 2 of our problems.
Your answer
Follow this Question
Related Questions
how to reference a .NET 4 targeted, native code wrapper assembly in Unity (beta)? 0 Answers
Is it possible to reference .NET references and give up cross-platform? 1 Answer
Target Platform for .NET DLLs, And Should I Use Linq? 0 Answers
Use .NET native dll calls 1 Answer
Using Microsoft surface 2.0 with Unity 3 Answers