Self-hosted cross-origin service for silverlight4

Source: Internet
Author: User

As we all know, cross-domain verification is required for SL and network communication. This has always been a headache for many SL beginners. The solutions and application scenarios are also different. Today, cool Superman shared a special application scenario.

Requirement: Develop a service called to SL and bind it with WCF. tcp. The client uses silverlight4 RTM, and the server has a self-host console application.

Problem:

1. The cross-origin service must also be run in the host's console program.

2. compatible with logical WCF services.

Solution:

A) first, we need to understand. After the release of silverlight4rtm, the cross-domain service for calling WCF. TCP has changed the check port and is no longer port 943. And changed to port 80. This is also the problem that many friends have been very depressed when using silverlight4 to call WCT. TCP.

B) according to the above prompts, we can write the following code:

Using system;
Using system. IO;
Using system. servicemodel;
Using system. servicemodel. description;
Using system. servicemodel. Web;
Using system. text;

Namespace selfhostedtcpandsilverlight
{
[Servicecontract]
Public interface itcppolicyretriever
{
[Operationcontract, webget (uritemplate = "/clientaccesspolicy. xml")]
Stream getsilverlightpolicy ();
}
Public class service: itcppolicyretriever
{
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>
<Socket-resource Port = "" 4502-4534 "" protocol = "" TCP "/>
</Grant-to>
</Policy>
</Cross-Domain-access>
</Access-Policy> ";
Weboperationcontext. Current. outgoingresponse. contenttype = "application/XML ";
Return new memorystream (encoding. utf8.getbytes (result ));
}
}
Class Program
{
Static void main (string [] ARGs)
{
String baseaddresshttp = "http: //" + environment. machinename + ": 80 ";
Servicehost host = new servicehost (typeof (service), new uri (baseaddresshttp ));
Host. addserviceendpoint (typeof (itcppolicyretriever), new webhttpbinding (), ""). behaviors. Add (New webhttpbehavior ());
Host. open ();
Console. writeline ("cross-origin service started ");
Console. Readline ();
Host. Close ();
}
}
}

Such a self-hosted sl4 cross-origin WCF Service is complete.

PS: when running this service, the administrator privilege is required to register the HTTP service on the local host in win7 and Vista. Therefore, cool Superman reminds everyone to run vs as an administrator to debug this cross-origin service.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.