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 = @"
";
return StringToStream(result);
}
public Stream GetFlashPolicy()
{
const string result = @"
";
return StringToStream(result);
}
#endregion
}
public static ServiceHost GetHost(string hostname)
{
var policyHost = new ServiceHost(typeof(Policy), new Uri("http://"+hostname + ":80"));//端口必须为80。若非TCP,像如HTTP托管,则直接将Policy发布至根下即可
policyHost.AddServiceEndpoint(typeof(IPolicy), new WebHttpBinding(), string.Empty).Behaviors.Add(new WebHttpBehavior());
return policyHost;
}
}