0

我有一个托管在 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>

任何人都可以提供帮助吗?

4

1 回答 1

0

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,请不要启动此策略服务,因为这将接管此地址 :-)

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

于 2012-12-30T19:59:49.500 回答