2009-04-03 19 views
6

私は、「時折接続されたビジネスライン(LOB)アプリケーション」として使用される.NET Compact Framework 3.5プログラムを用意しています。オンラインWebサービスが見える場合は、データアクセスに使用されますが、ネットワーク接続が失われた場合は、ローカルキャッシュが使用されます。Windows Mobileでネットワーク状態を管理する最善の方法

すべての接続オプションと状態の変更を処理する最善の方法は何ですか?

  • OpenNetCFのConnectionManagerクラス?
  • Microsoft.WindowsBile.State.SystemState?
  • APIコール?

どのようにWiFi、Cradle、GPRSの違いを理解し、利用可能な最良の方法を使用するのですか?

誰もがこれに関するガイダンスを持っていますか?

答えて

2

は、私はちょうど私がこのように呼び出すことができます単純な共有クラスの作成:次に

If MyConnectionClass.IsConnected then 
    'Do connected stuff 
Else 
    'Do local save 
End If 

私の実際のビジネスクラスのすべてを/機能は、UIコードから、この意地の悪さを隠すためにこれを使用することができます。また、あなたがバックグラウンドスレッドを使用して接続状態をポーリングし、その後ときの状態メインアプリのスレッドに戻ってイベントを発生することが推奨され

Public ReadOnly Property IsConnected As Boolean 
    Get 
    Try 

     Dim HostName As String = Dns.GetHostName() 
     Dim thisHost As IPHostEntry = Dns.GetHostByName(HostName) 
     Dim thisIpAddr As String = thisHost.AddressList(0).ToString 

     return (thisIpAddr <> Net.IPAddress.Parse("127.0.0.1").ToString()) 

    Catch ex As Exception 
     Return False 
    End Try 
    End Get 
End Property 

MyConnectionClass'接続されているプロパティは、このような何かを持っているでしょう変更。ここでは、詳細な過去記事です:

Testing for and responding to network connections in the .NET Compact Framework

EDIT:GPRS支援のための今

、:

あなたがWeb要求またはWebサービスを使用している場合は、フレームワークが処理しますあなたのための接続。あなたがれるtcpClientまたはUDPClientに深く潜水している場合は、そのような接続マネージャAPIの自分自身をしてそれを処理する必要があります。

public class GPRSConnection 
{ 
    const int S_OK = 0; 
    const uint CONNMGR_PARAM_GUIDDESTNET = 0x1; 
    const uint CONNMGR_FLAG_PROXY_HTTP = 0x1; 
    const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000; 
    const uint INFINITE = 0xffffffff; 
    const uint CONNMGR_STATUS_CONNECTED = 0x10; 
    static Hashtable ht = new Hashtable(); 

    static GPRSConnection() 
    { 
     ManualResetEvent mre = new ManualResetEvent(false); 
     mre.Handle = ConnMgrApiReadyEvent(); 
     mre.WaitOne(); 
     CloseHandle(mre.Handle); 
    } 

    ~GPRSConnection() 
    { 
     ReleaseAll(); 
    } 

    public static bool Setup(Uri url) 
    { 
     return Setup(url.ToString()); 
    } 

    public static bool Setup(string urlStr) 
    { 
     ConnectionInfo ci = new ConnectionInfo(); 
     IntPtr phConnection = IntPtr.Zero; 
     uint status = 0; 

     if (ht[urlStr] != null) 
      return true; 

     if (ConnMgrMapURL(urlStr, ref ci.guidDestNet, IntPtr.Zero) != S_OK) 
      return false; 

     ci.cbSize = (uint) Marshal.SizeOf(ci); 
     ci.dwParams = CONNMGR_PARAM_GUIDDESTNET; 
     ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP; 
     ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE; 
     ci.bExclusive = 0; 
     ci.bDisabled = 0; 
     ci.hWnd = IntPtr.Zero; 
     ci.uMsg = 0; 
     ci.lParam = 0; 

     if (ConnMgrEstablishConnectionSync(ref ci, ref phConnection, INFINITE, ref status) != S_OK && 
      status != CONNMGR_STATUS_CONNECTED) 
      return false; 

     ht[urlStr] = phConnection; 
     return true; 
    } 

    public static bool Release(Uri url) 
    { 
     return Release(url.ToString()); 
    } 

    public static bool Release(string urlStr) 
    { 
     return Release(urlStr, true); 
    } 

    private static bool Release(string urlStr, bool removeNode) 
    { 
     bool res = true; 
     IntPtr ph = IntPtr.Zero; 
     if (ht[urlStr] == null) 
      return true; 
     ph = (IntPtr)ht[urlStr]; 
     if (ConnMgrReleaseConnection(ph, 1) != S_OK) 
      res = false; 
     CloseHandle(ph); 
     if (removeNode) 
      ht.Remove(urlStr); 
     return res; 
    } 

    public static void ReleaseAll() 
    { 
     foreach(DictionaryEntry de in ht) 
     { 
      Release((string)de.Key, false); 
     } 
     ht.Clear(); 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct ConnectionInfo 
    { 
     public uint cbSize; 
     public uint dwParams; 
     public uint dwFlags; 
     public uint dwPriority; 
     public int bExclusive; 
     public int bDisabled; 
     public Guid guidDestNet; 
     public IntPtr hWnd; 
     public uint uMsg; 
     public uint lParam; 
     public uint ulMaxCost; 
     public uint ulMinRcvBw; 
     public uint ulMaxConnLatency; 
    } 

    [DllImport("cellcore.dll")] 
    private static extern int ConnMgrMapURL(string pwszURL, ref Guid pguid, IntPtr pdwIndex); 

    [DllImport("cellcore.dll")] 
    private static extern int ConnMgrEstablishConnectionSync(ref ConnectionInfo ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus); 

    [DllImport("cellcore.dll")] 
    private static extern IntPtr ConnMgrApiReadyEvent(); 

    [DllImport("cellcore.dll")] 
    private static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache); 

    [DllImport("coredll.dll")] 
    private static extern int CloseHandle(IntPtr hObject); 
} 

そして、それを使用するには、この操作を行います。これは、アンソニーからだった

public void DoTcpConnection() 
    { 
     string url = "www.msn.com"; 
     bool res = GPRSConnection.Setup("http://" + url + "/"); 
     if (res) 
     { 
      TcpClient tc = new TcpClient(url, 80); 
      NetworkStream ns = tc.GetStream(); 
      byte[] buf = new byte[100]; 
      ns.Write(buf, 0, 100); 
      tc.Client.Shutdown(SocketShutdown.Both); 
      ns.Close(); 
      tc.Close(); 
      MessageBox.Show("Wrote 100 bytes"); 
     } 
     else 
     { 
      MessageBox.Show("Connection establishment failed"); 
     } 
    } 

をここではウォンのブログ:

Anthony Wong

そして、あなたは唯一の下位レベルのTCPやUDPのもののためにこれを必要と覚えておいてください。 HTTPRequestsはこれを必要としません。

+0

接続しようとするにはどうすればいいですか(GPRSがあればそれを起動します)?投稿されたメソッド(およびリンク)は、接続がオンまたはオフのWiFiのように見えるだけです。 – JasonRShaver

1

SystemStateクラスをMicrosoft.WindowsMo​​bile.Status名前空間で使用するとどうなりますか?状態が変わると、システムの現在の状態を監視して通知を受け取ることができます。いくつかのコードについてはpostを参照してください。

SystemStateは、接続のステータスについてのみです。 ConnectionManagerを使用して特定の接続を使用できます。私はこれを読むことをお勧めしますarticle。 .NET Compact Framework 3.5を使用している場合、マネージAPIが含まれています。 OpenNetCF ConnectionManagerを使用することもできます。

+0

SystemStateを使用して接続しようとするにはどうすればいいですか(GPRSが利用可能であれば開始します)。 – JasonRShaver

1

私はモバイルアプリを書いて、ネットワークに関わっていることさえ知りません。十分に有効な検証データをローカルに保持し、接続中にクリアするローカルキューにトランザクションを書き込みます。キューリーダーには、接続されていないときに再試行するタイマーが含まれています。キューメッセージは双方向であるため、ローカルリフレッシュも提供できます。基本メッセージキューパターン。

これは非常に移植性の高い基本ソケットオープン/クローズ/リード/ライト/ ioctlロジックを使用して、ネットワーク接続を最も単純な方法で扱うことを可能にします。あなたの接続は何の重要な時間も持続する必要はありません。 (IMHOを解決していないMSアーキテクチャのバリエーションすべてに同期するために何が必要か想像するのは嫌です。)

+0

私はあなたが書いたものに多く同意する、私はWiFiのないときにプロトコルとしてSMSを使用するに切り替えるモバイルアプリを持っていた。これは、CE用のCLSAに組み込まれた 'Message Queue'ベースのxferシステムのために簡単でした。私はまだそれを使用することができますが、そのプロジェクトは、電話オプションを落とした=( – JasonRShaver

1

私はMicrosoft.WindowsMo​​bile.Stateを見つけました信頼できないネットワーク接続の.SystemStateレポート。これは6.0以前でした。私は網羅的なテストをしなかったが、そこに接続がないと言うと放棄された。