1

ローカルでホストされている WCF サービスと、それと通信する Silverlight 5 アプリがあります。既定では、Silverlight は、WCF サービスを呼び出すときに、HTTP 経由でクロス ドメイン ポリシー ファイルを取得しようとします。ポリシー ファイルが代わりに net.tcp ポート 943 で提供されるように、これを変更する必要があります。

ポート 943 経由でポリシー ファイルを提供するローカル tcp リスナーをセットアップしました。この手法に従って、アプリケーションの有効期間ごとに 1 回しか取得されないため、tcp 経由でポリシー ファイルを取得するためにダミーのソケット接続を作成しました。TCPサーバーは期待どおりにヒットしており、SocketErrorプロパティ値を取得していますSuccess(ただし、リスナーを開始した後に初めてTCPサーバーにヒットすると、常にアクセスが拒否されることに注意してください)。

私が知る限り、Silverlightアプリケーションがまだ接続できないためポリシーファイルが無効であるか、上記の手法がSilverlight 5では機能しません.

私が知りたいのは、私がやっていることは可能であり、それを正しく行っているかどうか、そうでなければ、ポリシーファイルを tcp 経由で正常にダウンロードし、HTTP 経由で取得する必要をなくす代替手段があるかどうかです。

ありがとう

4

1 に答える 1

1

WPF で Silverlight をホストし、http リスナーで WCF を使用する方法について、長い記事を書きました。

Silverlight 4 アプリケーションを WPF 4 アプリケーションでホストするにはどうすればよいですか?

質問に直接答えるわけではありませんが、http バージョンのポリシー ファイルを作成する方法を示しています。

ポート 943 経由でポリシー リスナーを提供するものも書きましたが、ソースを投稿した場所が見つからないため、引き続き掘り下げます。私が覚えている限りでは、Silverlight はポリシー ファイルをカスケード検索します。ポート 80 で接続できない場合は、ポート 943 を検索します。

これがどこかで役立つことを願っています。

OK、これは私が net.TCP トランスポート用に持っていたポリシーリスナーです。つまり、HTTP ベースではありません。遅れてすみません。それは今、他の誰かに役立つかもしれません。

私は、HTTP から TCP にカスケードすると言った MS のことを探していましたが、できません。

いずれにしても、net.TCP サービスを使用して呼び出し、そのリスナーが必要な場合は、次のコードが役に立ちます。

#region "Policy Listener"

// This is a simple policy listener
// that provides the cross domain policy file for silverlight applications
// this provides them with a network access policy
public class SocketPolicyListener
{

    private TcpListener listener = null;
    private TcpClient Client = null;
    byte[] Data;
    private NetworkStream netStream = null;

    private string listenaddress = "";

    // This could be read from a file on the disk, but for now, this gives the silverlight application
    // the ability to access any domain, and all the silverlight ports 4502-4534
    string policyfile = "<?xml version='1.0' encoding='utf-8'?><access-policy><cross-domain-access><policy><allow-from><domain uri='*' /></allow-from><grant-to><socket-resource port='4502-4534' protocol='tcp' /></grant-to></policy></cross-domain-access></access-policy>";

    // the request that we're expecting from the client
    private string _policyRequestString = "<policy-file-request/>";

    // Listen for our clients to connect
    public void Listen(string ListenIPAddress)
    {
        listenaddress = ListenIPAddress;
        if (listener == null)
        {
            listener = new TcpListener(IPAddress.Parse(ListenIPAddress), 943);

            // Try and stop our clients from lingering, keeping the socket open:
            LingerOption lo = new LingerOption(true, 1);
            listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,lo);
        }

        listener.Start();

        WaitForClientConnect();
    }

    private void WaitForClientConnect()
    {
        listener.BeginAcceptTcpClient(new AsyncCallback(OnClientConnected), listener);
    }

    public void StopPolicyListener()
    {
        if (Client.Connected)
        {
            // Should never reach this point, as clients
            // are closed if they request the policy
            // only clients that open the connection and
            // do not submit a policy request will remain unclosed
            Client.Close();
        }

        listener.Stop();
    }

    public void RestartPolicyListener()
    {
        listener.Start();
    }

    // When a client connects:
    private void OnClientConnected(IAsyncResult ar)
    {
        if (ar.IsCompleted)
        {
            // Get the listener that handles the client request.
            TcpListener listener = (TcpListener)ar.AsyncState;

            // End the operation and display the received data on 
            // the console.
            Client = listener.EndAcceptTcpClient(ar);

            // Try and stop our clients from lingering, keeping the socket open:
            LingerOption lo = new LingerOption(true, 1);
            Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lo);

            // Set our receive callback     
            Data = new byte[1024];
            netStream = Client.GetStream();
            netStream.BeginRead(Data, 0, 1024, ReceiveMessage, null);
        }

        WaitForClientConnect();
    }

    // Read from clients.
    public void ReceiveMessage(IAsyncResult ar)
    {
        int bufferLength;
        try
        {
            bufferLength = Client.GetStream().EndRead(ar);

            // Receive the message from client side.
            string messageReceived = Encoding.ASCII.GetString(Data, 0, bufferLength);

            if (messageReceived == _policyRequestString)
            {
                // Send our policy file, as it's been requested
                SendMessage(policyfile);

                // Have to close the connection or the
                // silverlight client will wait around.
                Client.Close();
            }
            else
            {
                // Continue reading from client. 
                Client.GetStream().BeginRead(Data, 0, Data.Length, ReceiveMessage, null);
            }
        }
        catch (Exception ex)
        {
            throw new Exception(Client.Client.RemoteEndPoint.ToString() + " is disconnected.");
        }
    }

    // Send the message.
    public void SendMessage(string message)
    {
        try
        {
            byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(message);
            //Client.Client.Send(bytesToSend,SocketFlags.None);
            Client.GetStream().Write(bytesToSend,0, bytesToSend.Length);
            Client.GetStream().Flush();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
#endregion
于 2013-04-03T09:39:44.967 に答える