天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

模擬IIS向Silverlight輸出策略文件

  問題

  最近的Silverlight開發中,由于部分需求對實時性和數據量下載速度有要求,部分WCF服務配置成了NETTcpBinding,這種方式跟普通的service.svc寄宿IIS不同的是,Silverlight需要的策略文件需要放置在本機IIS的根下,也就是wwwroot文件夾下,以滿足Silverlight在以TCP協議調用本機WCF服務時請求策略文件。(注:Silverlight通過TCP協議調用WCF服務時,會以http方式請求主機的一個策略文件,地址是http://localhost/clientaccesspolicy.xml)

  這其實是個不太好的選擇,程序運行的所需的環境被分成了兩部分,同事的機器上并未安裝IIS,為了大家開發簡便,不用在額外安裝IIS,也為了讓程序更加獨立,我就想能不能寫代碼監控80端口模擬IIS向Silverlight輸出這個策略文件。

  解決方法

  有了這個想法之后,首先想到的是通過Socket進行監聽,因為此前在MSDN上看到過這種方式,但很無奈,將代碼轉移過來之后,并未成功。相信做過Silverlight在Socket方面應用的朋友對下面這個PolicyServer類很熟悉吧。

using System;
using System.IO;
using System.NET;
using System.NET.Sockets;

namespace PolicyServer
{
// Encapsulate and manage state for a single connection from a client
class PolicyConnection
{
private Socket m_connection;

// buffer to receive the request from the client
private byte[] m_buffer;
private int m_received;

// the policy to return to the client
private byte[] m_policy;

// the request that we're expecting from the client
private static string s_policyRequestString = "<policy-file-request/>";



public PolicyConnection(Socket client, byte[] policy)
{
m_connection
= client;
m_policy
= policy;

m_buffer
= new byte[s_policyRequestString.Length];
m_received
= 0;

try
{
// receive the request from the client
m_connection.BeginReceive(m_buffer, 0, s_policyRequestString.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
}
catch (SocketException)
{
m_connection.Close();
}
}

// Called when we receive data from the client
private void OnReceive(IAsyncResult res)
{
try
{
m_received
+= m_connection.EndReceive(res);

// if we haven't gotten enough for a full request yet, receive again
if (m_received < s_policyRequestString.Length)
{
m_connection.BeginReceive(m_buffer, m_received, s_policyRequestString.Length
- m_received, SocketFlags.None, new AsyncCallback(OnReceive), null);
return;
}

// make sure the request is valid
string request = System.Text.Encoding.UTF8.GetString(m_buffer, 0, m_received);
if (StringComparer.InvariantCultureIgnoreCase.Compare(request, s_policyRequestString) != 0)
{
m_connection.Close();
return;
}

// send the policy
m_connection.BeginSend(m_policy, 0, m_policy.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
}
catch (SocketException)
{
m_connection.Close();
}
}

// called after sending the policy to the client; close the connection.
public void OnSend(IAsyncResult res)
{
try
{
m_connection.EndSend(res);
}
finally
{
m_connection.Close();
}
}
}

// Listens for connections on port 943 and dispatches requests to a PolicyConnection
class PolicyServer
{
private Socket m_listener;
private byte[] m_policy;

// pass in the path of an XML file containing the socket policy
public PolicyServer(string policyFile)
{
// Load the policy file
FileStream policyStream = new FileStream(policyFile, FileMode.Open);

m_policy
= new byte[policyStream.Length];
policyStream.Read(m_policy,
0, m_policy.Length);

policyStream.Close();

m_listener
= new Socket(AddressFamily.InterNETworkV6, SocketType.Stream, ProtocolType.Tcp);

m_listener.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)
27, 0);

m_listener.Bind(
new IPEndPoint(IPAddress.IPv6Any, 943));
m_listener.Listen(
10);

m_listener.BeginAccept(
new AsyncCallback(OnConnection), null);
}

public void OnConnection(IAsyncResult res)
{
Socket client
= null;

try
{
client
= m_listener.EndAccept(res);
}
catch (SocketException)
{
return;
}

// handle this policy request with a PolicyConnection
PolicyConnection pc = new PolicyConnection(client, m_policy);

// look for more connections
m_listener.BeginAccept(new AsyncCallback(OnConnection), null);
}

public void Close()
{
m_listener.Close();
}
}
public class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine(
"usage: PolicyServer.exe PolicyFile.xml");
return;
}

PolicyServer ps
= new PolicyServer(args[0]);
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
}

NET技術模擬IIS向Silverlight輸出策略文件,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 校园全肉高h湿一女多男 | 美女扒开尿口让男生添动态图 | 美女也烦恼主题曲 | 校草让我脱了内裤给全班看 | 国产在线高清视频无码 | 爱豆剧果冻传媒在线播放 | 我就去色色 | 99re久久热在线视频 | 亚洲精品综合在线影院 | 久久久性色精品国产免费观看 | 国产乱对白精彩在线播放 | 中文字幕亚洲无线码一区 | 久久超碰色中文字幕 | 琪琪电影午夜理论片YY6080 | 午夜福利在线观看6080 | 国产精品永久免费视频 | 亚洲免费福利在线视频 | 欧美香蕉大胸在线视频观看 | 6 10young俄罗斯 | 我的美女房东未删减版免费观看 | 免费高清国产 | 色AV色婷婷66人妻久久久 | 国产高清视频青青青在线 | 青草影院内射中出高潮-百度 | 囯产精品一区二区三区线 | 玉娇龙续集春雪瓶txt免费阅读 | 在线国产a不卡 | 国产精品一区二区在线观看 | 射漂亮黑b丝女 | 果冻传媒视频在线观看完整版免费 | 国产精品久久久久久免费播放 | 亚洲精品天堂在线 | 99久久99久久久精品齐齐鬼色 | 仓井空torrent| 精品三级久久久久电影网1 精品日韩视频 | 亚洲国产精品无码AV久久久 | 爽a中文字幕一区 | 看看妇女的B免费看 | 伊人久99久女女视频精品免 | 两性色午夜视频免费国产 | 亚洲乱码中文字幕久久孕妇黑人 |