2

Silverlight 経由で netTcpBinding を使用して WCF を呼び出そうとすると、次のエラーが発生します。

「TCP エラー コード 10013: アクセス許可によって禁止されている方法でソケットにアクセスしようとしました。これは、サービスがクロスドメイン用に構成されていないときに、クロスドメインの方法でサービスにアクセスしようとしたことが原因である可能性があります。アクセス。サービスの所有者に連絡して、HTTP 経由でソケット クロスドメイン ポリシーを公開し、許可されたソケット ポート範囲 4502 ~ 4534 でサービスをホストする必要がある場合があります。」

私の WCF サービスは IIS7 でホストされており、以下にバインドされています。

http://localhost.myserivce.comをポート 80 に、net.tcp をポート 4502 に

http://localhost.myserivce.com/myservice.svcを参照すると表示されます (ホスト ファイルはこのドメインを localhost にポイントしています)。http://localhost.myserivce.com/clientaccesspolicy.xmlも確認できます。

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
   <cross-domain-access>
      <policy>
         <allow-from http-request-headers="*">
            <domain uri="*" />
         </allow-from>
         <grant-to>
            <socket-resource port="4502-4534" protocol="tcp" />
         </grant-to>
      </policy>
   </cross-domain-access>
</access-policy>

私は何を間違っていますか?

4

2 に答える 2

1

ポート範囲 4502 ~ 4534 で TCP 接続を確立しようとすると、Silverlight は最初にポート 943 にリクエストを送信して、クライアント アクセス ポリシー ファイルのコンテンツを取得します。http://localhost.myserivce.com のファイルは読み取られません。 /clientaccesspolicy.xml、これは HTTP 要求専用であるためです。TCP ポート 943 でリッスンするようにサーバーを構成する必要があります。要求文字列<policy-file-request/>が xml ファイルの内容と同じであることを期待して応答します。

以下のコードは基本的な実装を示しています。ポート 943 を使用してローカルの IPEndPoint に渡す必要があります。

public class SocketPolicyServer
{
    private const string m_policyRequestString = "<policy-file-request/>";
    private string m_policyResponseString;
    private TcpListener m_listener;
    bool _started = false;

    public SocketPolicyServer()
    {
        m_policyResponseString = File.ReadAllText("path/to/clientaccesspolicy.xml");
    }

    public void Start(IPEndPoint endpoint)
    {
        m_listener = new TcpListener(endpoint);
        m_listener.Start();
        _started = true;
        m_listener.BeginAcceptTcpClient(HandleClient, null);
    }

    public event EventHandler ClientConnected;
    public event EventHandler ClientDisconnected;

    private void HandleClient(IAsyncResult res)
    {
        if(_started)
        {
            try
            {
                TcpClient client = m_listener.EndAcceptTcpClient(res);
                m_listener.BeginAcceptTcpClient(HandleClient, null);
                this.ProcessClient(client);
            }
            catch(Exception ex)
            {
                Trace.TraceError("SocketPolicyServer : {0}", ex.Message);
            }
        }
    }

    public void Stop()
    {
        _started = false;
        m_listener.Stop();
    }

    public void ProcessClient(TcpClient client)
    {
        try
        {
            if(this.ClientConnected != null)
                this.ClientConnected(this, EventArgs.Empty);

            StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8);
            char[] buffer = new char[m_policyRequestString.Length];
            int read = reader.Read(buffer, 0, buffer.Length);

            if(read == buffer.Length)
            {
                string request = new string(buffer);

                if(StringComparer.InvariantCultureIgnoreCase.Compare(request, m_policyRequestString) == 0)
                {
                    StreamWriter writer = new StreamWriter(client.GetStream());
                    writer.Write(m_policyResponseString);
                    writer.Flush();
                }
            }
        }
        catch(Exception ex)
        {
            Trace.TraceError("SocketPolicyServer : {0}", ex.Message);
        }
        finally
        {
            client.GetStream().Close();
            client.Close();
            if(this.ClientDisconnected != null)
                this.ClientDisconnected(this, EventArgs.Empty);
        }
    }
}
于 2010-06-17T17:30:44.157 に答える