简体   繁体   中英

How to consume a WCF service using silverlight without need to cross-domain policy

我在Internet上有一个RESTful服务,但是silverlight需要跨域设置,我需要使用Silverlight使用的该服务,我已经尝试过将WCF服务用作中间件,但是不幸的是,这没有用。

You could try using Javascript Bridge . I have not tried this approach but you could use ajax call and then "push" to SL via the JS bridge.

Create an util class for server side such as Util.DoRequest(string address):string

From msdn :

return readStream.ReadToEnd to your client over WCF RIA Services.(Domain Service Class)

By this way if your server has access this RESTful service or it's already located there you do not need any crossdomain configuration for your xap .

HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

        // Set some reasonable limits on resources used by this request
        request.MaximumAutomaticRedirections = 4;
        request.MaximumResponseHeadersLength = 4;
        // Set credentials to use for this request.
        request.Credentials = CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

        Console.WriteLine ("Content length is {0}", response.ContentLength);
        Console.WriteLine ("Content type is {0}", response.ContentType);

        // Get the stream associated with the response.
        Stream receiveStream = response.GetResponseStream ();

        // Pipes the stream to a higher level stream reader with the required encoding format. 
        StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

        Console.WriteLine ("Response stream received.");
        Console.WriteLine (readStream.ReadToEnd ());
        response.Close ();
        readStream.Close ();

Instead of trying unknown ways, it is better to place a cross domain policy file in the server. Why you don't need to have a cross domain policy file and client access file in the server? Is there any specific reason for it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM