平台:Xamarin Studio 4
目标移动设备:Android
我有一个使用basicHttpBinding调用WCF服务的Android应用程序,我一直在使用Xamarin Studio 4,它在调试模式下运行得很好。为了简化这一过程,我要调用WCF的“Hello World”功能。没有输入参数,只有字符串输出。
在调试模式下,我收到“Hello World”响应。当我将应用程序构建切换到“Release”,并再次运行该应用程序时,我收到以下错误消息:
System.ServiceModel.EndpointNoFoundException:发生了系统异常。 ---> System.Net.WebException:错误:ConnectFailure(没有到主机的路由)---> System.Net.Sockets.SocketExcpetion:在文件名未知的System.Net.Sockets.Socket.Connect(System.Net.EndPoint remoteEP)[0x00000]中没有主机路由:0
调用WCF的代码是:
BasicHttpBinding binding = CreateBasicHttp ();
BTSMobileWcfClient _client = new BTSMobileWcfClient (binding, endPoint);
_client.SayHelloCompleted += ClientOnSayHelloCompleted;
_client.SayHelloAsync();
private static BasicHttpBinding CreateBasicHttp()
{
BasicHttpBinding binding = new BasicHttpBinding
{
Name = "basicHttpBinding",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
TimeSpan timeout = new TimeSpan(0, 0, 30);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
return binding;
}
private void ClientOnSayHelloCompleted(object sender, SayHelloCompletedEventArgs sayHelloCompletedEventArgs)
{
string msg = null;
if (sayHelloCompletedEventArgs.Error != null)
{
msg = sayHelloCompletedEventArgs.Error.ToString();
}
else if (sayHelloCompletedEventArgs.Cancelled)
{
msg = "Request was cancelled.";
}
else
{
msg = sayHelloCompletedEventArgs.Result.ToString();
}
RunOnUiThread(() =>{
var lblSignInError = FindViewById<TextView> (Resource.Id.lblSignInError);
lblSignInError.Text = msg;
});
}
BTSMobileWcfClient是一个.cs文件,它是使用工具SLsvcUtil.exe针对Web服务的.svc文件创建的.cs文件。我不确定这与它有什么关系,但想要记录以防万一。
有没有人在“调试模式”中运行良好但在“发布模式”下失败之前有任何建议或看过这个?
谢谢!
答案 0 :(得分:7)
我在项目的发布编译中遇到了同样的问题。在游荡一段时间之后,我在ProjectOptions-&gt; AndroidApplication-&gt;所需权限(在Xamarin工作室中)设置了Internet权限。它看起来对我有用
答案 1 :(得分:1)
对于Visual Studio,在android.manifest中添加下一行:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
答案 2 :(得分:0)
将以下内容添加到System.ServiceModel.xml。
<?xml version="1.0" encoding="utf-8" ?>
<linker>
<assembly fullname="System.ServiceModel">
<type fullname="System.ServiceModel.Channels.ChannelFactoryBase`1">
<method name="CreateChannel" />
</type>
</assembly>
</linker>
确保链接器设置仅设置为SDK程序集。