Silverlight4在跨域访问TCP时会读取Http 80端口的跨域策略文件,可以放IIS上,也可以在服务控制台托管Http服务
例子是控制台托管TCP服务和Http跨域策略文件服务
配置文件:
<?xml version="1.0"?>
<configuration> <system.serviceModel> <services> <service name="ConsoleApplication1.DuxpexService"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBindConfig" contract="ConsoleApplication1.IDuxpexService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="" /> <add baseAddress="net.tcp://localhost:4505/DuxpexService/" /> </baseAddresses> </host> </service> <service name="ConsoleApplication1.DomainService"> <endpoint address="" behaviorConfiguration="DomainServiceBehavior" binding="webHttpBinding" bindingConfiguration="" contract="ConsoleApplication1.IDomainService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="" /> </baseAddresses> </host> </service> </services> <bindings> <netTcpBinding> <binding name="netTcpBindConfig"> <security mode="None"/> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="DomainServiceBehavior"> <webHttp/> </behavior></endpointBehaviors>
</behaviors> </system.serviceModel> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup> </configuration>
TCP跨域策略文件ClientAccessPolicy.xml
<?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"/> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy>
跨域服务:
[ServiceContract]
public interface IDomainService { [OperationContract] [WebGet(UriTemplate = "ClientAccessPolicy.xml")] Message ProvidePolicyFile(); }public class DomainService : IDomainService
{ public System.ServiceModel.Channels.Message ProvidePolicyFile() {XmlReader reader = XmlReader.Create(@"ClientAccessPolicy.xml");
System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);return result;
} }
普通双向服务:
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]
public class DuxpexService : IDuxpexService { public int GetData() { IserviceCallBack client = OperationContext.Current.GetCallbackChannel<IserviceCallBack>(); client.ClientMethod();return 10;
} }[ServiceContract(CallbackContract = typeof(IserviceCallBack))]
public interface IDuxpexService { [OperationContract] int GetData(); } public interface IserviceCallBack { [OperationContract(IsOneWay = true)] void ClientMethod(); }
启动方法:
class Program
{ static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(DuxpexService)); ServiceHost domainhost = new ServiceHost(typeof(DomainService)); domainhost.Open(); host.Open(); Console.WriteLine("Service Start..."); Console.ReadLine(); domainhost.Close(); host.Close(); } } ClientAccessPolicy.xml