我正在使用 MonoDroid for C# 创建一个概念性的 Android 应用程序。到目前为止,它看起来还不错。然而,我在使用 WCF 时遇到了障碍。
这个概念很简单,使用一个名为“Ping”的方法创建一个 WCF 服务,该方法返回一个字符串“Pong”。编写了一个 WPF 应用程序,该应用程序运行良好。但是当尝试使用该服务时,android 应用程序给了我一个奇怪的错误。
首先是我得到的错误
System.TypeLoadException: Could not load type '__clientproxy_IService1' from assembly 'dummy, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.MonoType.GetMethodImpl (System.String name, BindingFlags bindingAttr, System.Reflection.Binder binder, CallingConventions callConvention, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) [0x00000] in <filename unknown>:0
at System.Type.GetMethod (System.String name, BindingFlags bindingAttr, System.Reflection.Binder binder, CallingConventions callConvention, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) [0x00000] in <filename unknown>:0
at System.Type.GetMethod (System.String name, System.Type[] types) [0x00000] in <filename unknown>:0
at Mono.CodeGeneration.CodeMethod.UpdateMethodBase (System.Type type) [0x00000] in <filename unknown>:0
at Mono.CodeGeneration.CodeClass.CreateType () [0x00000] in <filename unknown>:0
at System.ServiceModel.ProxyGeneratorBase.CreateProxyTypeOperations (System.Type crtype, Mono.CodeGeneration.CodeClass c, System.ServiceModel.Description.ContractDescription cd) [0x00000] in <filename unknown>:0
at System.ServiceModel.ClientProxyGenerator.CreateProxyType (System.Type requestedType, System.ServiceModel.Description.ContractDescription cd, Boolean duplex) [0x00000] in <filename unknown>:0
at System.ServiceModel.ChannelFactory`1[HelloMonoDroid.IService1].CreateChannel (System.ServiceModel.EndpointAddress address, System.Uri via) [0x00000] in <filename unknown>:0
at System.ServiceModel.ChannelFactory`1[HelloMonoDroid.IService1].CreateChannel (System.ServiceModel.EndpointAddress address) [0x00000] in <filename unknown>:0
at System.ServiceModel.ChannelFactory`1[HelloMonoDroid.IService1].CreateChannel () [0x00000] in <filename unknown>:0
at System.ServiceModel.ClientBase`1[HelloMonoDroid.IService1].CreateChannel () [0x00000] in <filename unknown>:0
at System.ServiceModel.ClientBase`1[HelloMonoDroid.IService1].get_InnerChannel () [0x00000] in <filename unknown>:0
at System.ServiceModel.ClientBase`1[HelloMonoDroid.IService1].get_Channel () [0x00000] in <filename unknown>:0
at HelloMonoDroid.ClientTest.BeginPing (System.AsyncCallback callback, System.Object asyncState) [0x00000] in <filename unknown>:0
at HelloMonoDroid.Activity1.button_Click (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0
这是服务器端的接口
[ServiceContract]
public interface IService1
{
[OperationContract]
string Ping();
}
这是服务器端类
public class Service1 : IService1
{
public string Ping()
{
return "Pong";
}
}
服务器端运行良好,因为我的测试 WPF 应用程序运行良好。
Android 客户端接口使用异步模式,但在使用对服务的直接同步调用时会出现同样的错误。
[ServiceContract]
public interface IService1
{
[OperationContract]
string Ping();
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginPing(AsyncCallback callback, object asyncState);
string EndPing(IAsyncResult result);
}
这是客户端“代理”类
class ClientTest : ClientBase<IService1>, IService1
{
public ClientTest(Binding binding, EndpointAddress address)
: base(binding, address)
{
}
public string Ping()
{
return Channel.Ping();
}
public IAsyncResult BeginPing(AsyncCallback callback, object asyncState)
{
return Channel.BeginPing(callback, asyncState);
}
public string EndPing(IAsyncResult result)
{
return Channel.EndPing(result);
}
}
这是执行调用的代码。
void CallServer(object sender, EventArgs e)
{
var myBinding = new BasicHttpBinding();
var myEndpointAddress = new EndpointAddress("http://mycomputername:8732/Android/");
_proxy = new ClientTest(myBinding, myEndpointAddress);
_proxy.BeginPing(OnCompletion, null);
}
void OnCompletion(IAsyncResult result)
{
string str = _proxy.EndPing(result);
textbox.Text = "Result is: " + str;
result.AsyncWaitHandle.Close();
}
我希望有人会知道一个解决方案,或者可以给我一个不同的方法。