【问题标题】:SilverLight Connect to NetTcp WCF Hosted inWindows ServiceSilverLight 连接到 Windows 服务中托管的 NetTcp WCF
【发布时间】:2012-12-30 12:06:46
【问题描述】:

我有一个托管在 Windows 服务中的 WCF 服务

它使用 NetTCPBinding,我可以连接,我想实现新的 Silverlight 客户端来访问服务

我已经完成了添加服务引用的常规方法,它是用 Empty "ServiceReferences.ClientConfig" 添加的

所以我查看了一些线程和主题,最后我为服务手动编写了配置

当我尝试连接时显示此异常 无法连接到 net.tcp://localhost:4502/MyService/Service。连接尝试持续了 00:00:02.2111265 的时间跨度。 TCP 错误代码 10013:尝试以访问权限禁止的方式访问套接字。这可能是由于尝试以跨域方式访问服务,而该服务未配置为跨域访问.您可能需要联系服务的所有者以通过 HTTP 公开套接字跨域策略,并将服务托管在允许的套接字端口范围 4502-4534 中。

我认为问题与 ClientAccessPolicy.xml 文件有关

搜索后人们说我需要安装 IIS7 并且可以通过它访问文件,我已经尝试过,但我无法让它工作

但是,我以前做过这个工作,但是我使用 PollinghttpBinding 没有 NetTCP,并且我创建了另一个服务合同来返回 ClientAccessPolicy 文件

我已经尝试过使用 PollinghttpBinding 做同样的事情,但我无法编写正确的服务配置

我的客户拒绝使用 IIS,所以我可以使用这种方式吗?我应该使用该服务的正确配置是什么?

这是我用于我的服务的配置

<service behaviorConfiguration="serviceBehavior" name="MyService">
              <endpoint address="net.tcp://localhost:4502/MyService/Service"       behaviorConfiguration="endpointBehavior" binding="netTcpBinding"         bindingConfiguration="netTcpServiceBinding" contract="IMyService">
                  <identity>
                      <dns value="localhost"/>
                  </identity>
              </endpoint>
              <endpoint address="net.tcp://localhost:7000/MyService/mex"     binding="mexTcpBinding" contract="IMetadataExchange"/>
          </service>

有人可以帮忙吗?

【问题讨论】:

    标签: wcf silverlight windows-services nettcpbinding clientaccesspolicy.xml


    【解决方案1】:

    Silverlight 不支持“开箱即用”的 Net.tcp 绑定。这就是配置为空的原因。但无论如何,您都可以使用它,方法是使用 customBinding 并设置您需要的属性。但是,我自己从未尝试过。

    如果这是一个跨域问题,则需要与 ClientAccessPolicy.xml 文件相关。 通常(如各种论坛上的许多地方所述),这可以通过将文件放在站点的根目录中来解决。因此,如果您的服务在“http://localhost/MyService”上运行,则应将文件放在“http://lovalhost”中。

    但是,如果没有可用的 IIS,则必须以另一种方式完成。您必须在该文件可用的 Windows 服务中手动在根上创建一个端点。 这是一个普通的 BasicHttp 绑定,您可以使用“net.tcp”或“http”。

    我以这种方式成功地做到了这一点:

    政策界面:

    using System;
    using System.IO;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    
    namespace MyPolicyService
    {
        [ServiceContract]
        public interface IPolicyRetriever
        {
            [OperationContract]
            Stream GetPolicy();
        }
    }
    

    策略类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.ServiceModel.Web;
    
    namespace MyPolicyService
    {
        public class PolicyRetrieverBase : IPolicyRetriever
        {
            public Stream StringToStream(String result)
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
                return new MemoryStream(Encoding.UTF8.GetBytes(result));
            }
    
            public Stream GetSilverlightPolicy()
            {
                string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <access-policy>
        <cross-domain-access>
            <policy>
                <allow-from http-request-headers=""*"" http-methods=""*"">
                    <domain uri=""*""/>
                </allow-from>
                <grant-to>
                    <resource path=""/"" include-subpaths=""true""/>
                </grant-to>
            </policy>
        </cross-domain-access>
    </access-policy>";
    
                return StringToStream(result);
            }
    
            public Stream GetFlashPolicy()
            {
                string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
    <cross-domain-policy>
        <site-control permitted-cross-domain-policies=""all""/>
        <allow-access-from domain=""*"" secure=""false"" />
        <allow-http-request-headers-from domain=""*"" headers=""*"" secure=""false"" />
    </cross-domain-policy>";
    
                return StringToStream(result);
            }
        }
    }
    

    创建这些类后,几乎就像启动“net.tcp”服务一样创建服务,但当然要将其更改为 BasicHttpBinding 并使用与 BasicHttpBinding 相关的一些不同行为和属性值(如 TransferMode = Buffered等)。

    不用说,该策略服务应该在站点根目录 (http://localhost) 上启动。 如果您有在此服务器上运行的 IIS,请不要启动此策略服务,因为这将接管此地址 :-)

    希望这能让你朝着正确的方向前进 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      相关资源
      最近更新 更多