济宁李尔调度
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
public static class ClientCrossDomainAccessPolicy
{
[ServiceContract]
public interface IPolicy
{
//确定可以访问“http://localhost:80/clientaccesspolicy.xml"
[OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
Stream GetSilverlightPolicy();
//确定可以访问“http://localhost:80/crossdomain.xml"
[OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
Stream GetFlashPolicy();
}
class Policy : IPolicy
{
#region IClientAccessPolicy Members
Stream StringToStream(string result)
{
var oc = WebOperationContext.Current;
if (oc == null)
throw new NullReferenceException("WebOperationContext");
oc.OutgoingResponse.ContentType = "application/xml";
return new MemoryStream(Encoding.UTF8.GetBytes(result));
}
public Stream GetSilverlightPolicy()
{
const 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>";
return StringToStream(result);
}
public Stream GetFlashPolicy()
{
const string result = @"<?xml version=""1.0""?>
<!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
<cross-domain-policy>
<allow-access-from domain=""*"" />
</cross-domain-policy>";
return StringToStream(result);
}
#endregion
}
public static ServiceHost GetHost(string hostname)
{
var policyHost = new ServiceHost(typeof(Policy), new Uri("http://"+hostname + ":81"));//端口必须为80。若非TCP,像如HTTP托管,则直接将Policy发布至根下即可
policyHost.AddServiceEndpoint(typeof(IPolicy), new WebHttpBinding(), string.Empty).Behaviors.Add(new WebHttpBehavior());
return policyHost;
}
}