0

MonoDroid for C# を使用して、コンセプト Android アプリのプーフを作成しています。これまでのところ、見た目はかなり良いです。ただし、WCF を使用しているときに問題が発生しました。

概念は単純です。文字列「Pong」を返す「Ping」という単一のメソッドを使用して WCF サービスを作成します。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();
}

誰かが解決策を知っているか、別のアプローチを教えてくれることを願っています。

4

1 に答える 1

2

サービスを .Net 2.0 スタイルの Web サービスとして公開できる場合、それらは Mono (つまり MonoDroid) でより適切にサポートされます。

于 2011-02-16T16:00:29.463 に答える