在SilverLight是允许通过OpenFileDialog控件读取本地文件的,故用SL做文件上传是一个很好的解决方案,思路也很简单,就将文件分块上传,然后在服务端写入到文件即可。本以为很简单的实现,却没想到中间横生枝节,那就是SilverLight在连续调用WCF服务时,当达到一定次数时(这个次数不确定),偶尔会发生 System.ExecutionEngineException 异常,并且此异常无法扑获。

为了验证是不是SilverLight调用WCF的问题,写了一下最简单的,但是问题依旧
Service1.svc
[ServiceContract]
public interface IService1
{
[OperationContract]
int DoWork(System.Byte[] bytes);
}
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config.
public class Service1 : IService1
{
/// <summary>
/// 最简单的实现,就传入的内容写到日志文件中
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public int DoWork(System.Byte[] bytes)
{
Jon.Services.LogHelper.GetLogger(this.GetType().ToString()).Debug(System.Text.Encoding.Unicode.GetString(bytes));
return bytes.Length;
}
}
服务端的配置
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SilverlightApplication1Web.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="SilverlightApplication1Web.Service1Behavior" name="SilverlightApplication1Web.Service1">
<endpoint address="" bindingConfiguration="LargeBuffer" binding="basicHttpBinding" contract="SilverlightApplication1Web.IService1" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="LargeBuffer" maxBufferSize="4096000" maxReceivedMessageSize="4096000">
<readerQuotas maxDepth="4096000" maxStringContentLength="4096000" maxArrayLength="4096000" maxBytesPerRead="4096000" maxNameTableCharCount="4096000"/>
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
SilverLight端的也很简单
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
ServiceReference1.Service1Client client = new SilverlightApplication1.ServiceReference1.Service1Client(new BasicHttpBinding(BasicHttpSecurityMode.None), new EndpointAddress("http://localhost:9228/Service1.svc"));
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < 538; i++)
{
sb.Append(Guid.NewGuid().ToString());
sb.Append(System.Environment.NewLine);
}
int index = 0;
client.DoWorkCompleted += delegate(object sender, SilverlightApplication1.ServiceReference1.DoWorkCompletedEventArgs e)
{
if (index < 500000000)
{
System.Threading.Thread.Sleep(50);
client.DoWorkAsync(System.Text.Encoding.Unicode.GetBytes(sb.ToString()));
index++;
//System.Windows.Browser.HtmlPage.Document.SetProperty("title", index.ToString() + "_" + e.Result.ToString());
}
};
client.DoWorkAsync(System.Text.Encoding.Unicode.GetBytes(sb.ToString()));
//System.Windows.Browser.HtmlPage.Document.SetProperty("title", index.ToString());
}
}
就是循环调用WCF,向服务器端上传文件内容。System.ExecutionEngineException异常就发生在 client.DoWorkAsync() 时,(DoWorkAsync方法是VS添加引用时自动生成的),我想应该是SilverLight对WCF支持不好的原因吧,,各位达人指点迷津一下吧,,(真心希望不是SilverLight的问题,而是本人菜鸟水平所致)。
测试项目在这里