0

サーバーとの通信に WCF を使用する Silverlight アプリケーションがあります。Silverlight と WCF の両方がローカル コンピューター (localhost) で実行されています。Silverlight がサービスを呼び出すと、通信例外で失敗します。これは clientaccesspolicy ファイルがないためだと理解していますが、WCF エンドポイントがhttp://localhost:portで実行されているため、インターフェイス IPolicyRetriver を定義し、clientaccesspolicy を返すサービスに実装を追加しました。ストリーム。

私の質問は、問題なく実行するには何を構成する必要があるかということです。ServiceReference.ClientConfig ファイルに何かを変更または追加する必要があることは理解していますが、何を理解していません。以下に ServiceReference.ClientConfig を含めました。何を変更または追加するか、およびこのコードを Silverlight のどこに追加するかを教えてください。この 2 日間で可能な限りすべてのリンクを開いたので、ここにリンクを貼り付けないでください。それでも理解できません。

<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>

お願い助けて!

4

2 に答える 2

1

言及した IPolicyRetriever 実装は含まれていませんが、使用できるサンプルを次に示します。

インターフェース仕様:

[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 ファイルに以下を含めることができます。これは、クライアント側ではなく、サーバー側で行う必要があります。上記のクライアント構成を質問に含めたので、これを強調しています。

<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% 正しくない可能性があります):

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

あなたがリンクを提供しないように頼んだことは知っていますが、私は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を参照して記憶をリフレッシュしてください。現時点では Silverlight/WCF プロジェクトにアクセスできないためです。

于 2011-02-15T11:20:20.127 に答える
0

You shouldn't need to change anything with your service configuration or code. Place the clientaccesspolicy.xml in the ROOT of the service website. If you are using Visual Studio, you may need to make a property change to get this to work. Silverlight will look for the existence of the file. I might help you to use a tool like Fiddler to see where Silverlight is looking for the file.

There is one link that I found very helpful but since you don't want any links, I won't provide it.

于 2011-02-07T17:28:49.027 に答える