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.