首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Silverlight WCF netTcpBinding问题

Silverlight WCF netTcpBinding问题
EN

Stack Overflow用户
提问于 2010-05-20 22:34:02
回答 2查看 2.8K关注 0票数 2

尝试通过Silverlight调用带有netTcpBinding的WCF时,出现错误:

"TCP错误代码10013:试图以访问权限禁止的方式访问套接字。这可能是由于试图以跨域方式访问服务,而服务未配置为跨域访问。您可能需要与服务的所有者联系,以通过HTTP公开套接字跨域策略,并在允许的套接字端口范围4502-4534中承载服务。“

我的WCF服务托管在IIS7中,绑定到:

端口80上的http://localhost.myserivce.com和端口4502上的net.tcp

如果我浏览到它,我可以看到http://localhost.myserivce.com/myservice.svc (我的主机文件将此域指向本地主机)。我还可以看到http://localhost.myserivce.com/clientaccesspolicy.xml

代码语言:javascript
复制
<?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>

我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-06-18 01:30:44

如果您尝试在端口范围4502-4534中建立TCP连接,Silverlight将首先在端口943上发布一个请求,以检索客户端访问策略文件内容-它不会读取http://localhost.myserivce.com/clientaccesspolicy.xml上的文件,因为这仅用于HTTP请求。您需要将服务器配置为侦听TCP端口943,期望请求字符串等于<policy-file-request/>,并使用xml文件内容进行回复。

下面的代码显示了一个基本的实现,您需要使用端口943传递给它一个本地IPEndPoint:

代码语言:javascript
复制
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);
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2011-03-01 05:08:29

从SL4 RTM开始,这种情况发生了变化。SL4 net.tcp客户端不会在TCP943上查找客户端访问策略,而是使用旧方法,即使用the服务器的根目录,即端口80。奇怪的是,这在任何地方都没有很好的文档记录,但我可以确认这就是发布时的行为。

归根结底,对于SL4 RTM,您描述的方法应该有效,而这里的另一个答案不起作用。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2874712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档