我正在使用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();
}
我希望有人知道解决方案,或者可能会给我一个不同的方法。
答案 0 :(得分:2)
如果您可以将服务公开为.Net 2.0样式的Web服务,Mono(以及MonoDroid)可以更好地支持这些服务。