无法从Xamarin Android发布版本Ping外部IP

wtlkbnrh  于 2023-09-28  发布在  Android
关注(0)|答案(1)|浏览(106)

我正在使用InetAddress从Xamarin.Android应用程序ping外部IP地址。它在调试模式下工作,但在释放模式下失败。

private bool Ping(string ip)
{
     InetAddress inetAddress = InetAddress.GetByName(ip);
     return inetAddress.IsReachable(1000); // 1 second timeout
}

如果您使用相同的IP,将在DEBUG模式下返回true,但在RELEASE模式下返回false

项目文件配置

<PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProductVersion>8.0.30703</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>
        <ProjectGuid>{6A6B026F-9CFE-4692-8D72-C41E577C0718}</ProjectGuid>
        <ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
        <OutputType>Library</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>XamarinDemo</RootNamespace>
        <AssemblyName>XamarinDemo</AssemblyName>
        <FileAlignment>512</FileAlignment>
        <AndroidApplication>True</AndroidApplication>
        <AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
        <AndroidResgenClass>Resource</AndroidResgenClass>
        <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
        <AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
        <TargetFrameworkVersion>v10.0</TargetFrameworkVersion>
        <AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
        <MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
        <MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
        <AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>True</DebugSymbols>
        <DebugType>portable</DebugType>
        <Optimize>False</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <AndroidUseSharedRuntime>True</AndroidUseSharedRuntime>
        <AndroidLinkMode>None</AndroidLinkMode>
        <EmbedAssembliesIntoApk>False</EmbedAssembliesIntoApk>
        <AndroidSupportedAbis>armeabi-v7a;x86;arm64-v8a;x86_64</AndroidSupportedAbis>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugSymbols>True</DebugSymbols>
        <DebugType>pdbonly</DebugType>
        <Optimize>True</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <AndroidManagedSymbols>true</AndroidManagedSymbols>
        <AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
        <AndroidLinkMode>None</AndroidLinkMode>
        <EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
    </PropertyGroup>

释放模式配置是否错误或缺少任何权限?或者在xamarin android的发布模式下有没有其他方法可以ping ip?
谢谢你,谢谢

fv2wmkja

fv2wmkja1#

Optimize的值从true更改为false可以解决此问题。我相信这个设置会导致InetAddress类中的代码不能正确运行。

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugSymbols>True</DebugSymbols>
        <DebugType>pdbonly</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <AndroidManagedSymbols>true</AndroidManagedSymbols>
        <AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
        <AndroidLinkMode>None</AndroidLinkMode>
        <EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
</PropertyGroup>

相关问题