Silverlight-WCF在本地主机上获取clientaccesspolicy [英] Silverlight - WCF get clientaccesspolicy on localhost

查看:82
本文介绍了Silverlight-WCF在本地主机上获取clientaccesspolicy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight应用程序,该应用程序使用WCF与服务器进行通信. Silverlight和WCF都在本地计算机(本地主机)上运行.当Silverlight调用该服务时,它将失败,并出现通信异常. 我了解这是因为我没有clientaccesspolicy文件,但是由于WCF终结点正在 http://localhost:port 我定义了一个接口IPolicyRetriver,并为该服务添加了一个实现,该实现将以流的形式返回clientaccesspolicy.

I have a Silverlight application which uses WCF for its communications with the server. Both Silverlight and WCF are running on the local machine (localhost). When the Silverlight makes a call to the service it fails with aa communication exception. I understand that this is because I don't have a clientaccesspolicy file, but since the WCF endpoint is running on http://localhost:port I defined an interface, IPolicyRetriver, and added an implementation to the service which is returning the clientaccesspolicy in a stream.

我的问题是,我必须进行哪些配置才能使其正常运行?我知道我必须更改或向ServiceReference.ClientConfig文件中添加一些内容,但是我不知道是什么.我在下面包含了我的ServiceReference.ClientConfig.请让我知道要更改或添加的内容,以及在Silverlight的何处添加此代码. 请不要在此处粘贴任何链接来帮助我,因为我已经打开了过去两天我可以打开的每个链接,但仍然不明白.

My question is, what do I have to configure so that it will run without a problem? I understand that I have to change or add something to my ServiceReference.ClientConfig file, but I don't understand what. I've included my ServiceReference.ClientConfig below. Please let me know what to change or add to it, and where in Silverlight to add this code. Please do not paste any links here to help me as I have opened every link I could during the last two days - but still don't understand.

<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IMapService" maxBufferSize="2147483647"
                maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="../MapService.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IMapService" contract="MapService.IMapService"
            name="BasicHttpBinding_IMapService" />
    </client>
</system.serviceModel>

请帮助我!

推荐答案

您尚未包括您提到的IPolicyRetriever实现,但这是您可以使用的示例.

You haven't included the IPolicyRetriever implementation you mention, but here is a sample that you can use.

接口规范:

[ServiceContract]
public interface IPolicyRetriever
{
    [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
    Stream GetSilverlightPolicy();

    //[OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
    //Stream GetFlashPolicy();
}

接口的实现:

    // IPolicyRetriever implementation
    private 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=""*"">
                  <domain uri=""*""/>
                </allow-from>
                <grant-to>
                  <resource path=""/"" include-subpaths=""true""/>
                </grant-to>
              </policy>
            </cross-domain-access>
          </access-policy>";

        return StringToStream(result);
     }

然后,您可以在服务器的配置XML文件中包括以下内容.这需要在服务器端,而不是在客户端.我要强调的是因为您在问题中包括了上面的客户端配置.

Then you can include the following in your server's configuration XML file. This needs to be on the server side, not on the client side. I'm emphasising this because you included the client config above in your question.

<behaviors>
  <endpointBehaviors>
    <behavior name="WebHttpNewBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  ...
</behaviors>
<services>
  <service behaviorConfiguration="NewBehavior">
    <endpoint behaviorConfiguration="WebHttpNewBehavior" binding="webHttpBinding"
                bindingConfiguration="" name="PolicyEndpoint" contract="WCFService.IPolicyRetriever" />
    ...
  </service>
</services>

或者,如果您决定以编程方式创建主机(这是我这样做的方法,而不是使用ClientConfig文件,因此上述示例可能不是100%正确):

Alternatively if you decide to create your host programmatically (this is how I do it, rather than use a ClientConfig file, so the above sample might not be 100% correct):

ServiceHost host = new ServiceHost(serviceType);
host.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());

我知道您要求不提供链接,但我使用了

I know you asked not to provide links but I used http://blogs.msdn.com/b/asiatech/archive/2010/05/07/how-to-consume-a-self-hosted-wcf-service-in-a-cross-domain-environment-by-silverlight-client.aspx as a reference to refresh my memory, because I don't have access to my Silverlight/WCF project right at this minute.

这篇关于Silverlight-WCF在本地主机上获取clientaccesspolicy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆