Silverlight WCF引发System.ServiceModel.ComunicationException. [英] Silverlight WCF throws System.ServiceModel.ComunicationException.

查看:67
本文介绍了Silverlight WCF引发System.ServiceModel.ComunicationException.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Silverlight中的WCF服务将数据写入SQL Server,但是当我尝试执行此操作时,出现此异常:

Hi, I am trying to write data into SQL server using WCF service in Silverlight, but when i tried to do it, i get this exception:

System.ServiceModel.CommunicationException was unhandled by user code
  Message=The remote server returned an error: NotFound.
  StackTrace:
       at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
       at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
       at SL_PilgrimManagement.DBSourceService.DBDataServiceClient.DBDataServiceClientChannel.EndAddNewPilgrim(IAsyncResult result)
       at SL_PilgrimManagement.DBSourceService.DBDataServiceClient.SL_PilgrimManagement.DBSourceService.DBDataService.EndAddNewPilgrim(IAsyncResult result)
       at SL_PilgrimManagement.DBSourceService.DBDataServiceClient.OnEndAddNewPilgrim(IAsyncResult result)
       at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
  InnerException: System.Net.WebException
       Message=The remote server returned an error: NotFound.
       StackTrace:
            at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
            at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
            at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
       InnerException: System.Net.WebException
            Message=The remote server returned an error: NotFound.
            StackTrace:
                 at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
                 at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<endgetresponse>b__4(Object sendState)
                 at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<beginonui>b__1(Object sendState)
            InnerException: 
</beginonui></endgetresponse>




字段之一是varbinary(MAX)类型,我用它来存储图像,而我是这样进行的:




one of the field is a varbinary(MAX) type, which i use to store images, and i did this in this way:

Stream photosream = browse.File.OpenRead();
                BitmapImage bm = new BitmapImage();

                photobytes = (new BinaryReader(photosream)).ReadBytes((int)browse.File.Length);

                bm.SetSource(photosream);
                image1.Source = bm;



这里photobytesbyte[]类型的类属性,让我更加困惑的是,有一段时间我忘记初始化photobytes,并且没有引发异常.但是,当然,SQL db的photo字段为空,但是other字段正确填充了...
我在哪里做错了?


我没有得到任何答案,也许我还不够清楚,让我补充一点,
如我之前所说,在某些情况下,我忘记初始化photobytes了,因为我的意思是在方法

我错误地声明了一个新变量,如下所示:



here photobytes is a class attribute of byte[] type, and one thing that makes me even more confused is, there was a time where i forgot initialize the photobytes, and it didnt throw the exception. but of course the photo field at the SQL db is empty, but the others field are filled correctly...
where did i do wrong?


edit: im not getting any answer, maybe im not clear enough, let me add some points,
as i said before, there was a case where i forgot to initialiaze the photobytes, by that i mean at the method,

i mistakenly declared a new variable, something like this:

Stream photosream = browse.File.OpenRead();
                BitmapImage bm = new BitmapImage();
                byte[] photobytes = (new BinaryReader(photosream)).ReadBytes((int)browse.File.Length);
                bm.SetSource(photosream);
                image1.Source = bm;


并且服务调用正确运行,并且尽管image字段保留为空,但数据已添加到数据库中..
而且我不认为这是一个连接问题,因为获取表内容的服务调用可以正常工作而没有任何问题...是什么原因引起的?


我浏览了一下,我认为引起该错误的原因是因为我试图将字节数组传递给WCF服务调用.我的服务调用是这样的:


and the service call run correctly, and the data is added to the database, although the image field is left null..
and i dont think this is a connection problem, as the service call to get the content of the table work correctly without any problem... what could cause this?


edit 2: i browsed around, and i think what caused the error was because i tried to pass array of bytes to the WCF Service call.. my service call is something like this:

DataServiceClient svc = new DataServiceClient();
svc.AddNewRecordAsyncCompleted += *Event Handling Here*
svc.AddNewRecordAsync(PKField, *Other Fields here*, photobytes);



而在OperationContract定义本身上,将是这样的:



while at the OperationContract definition itself, it would be something like this:

DBSourceDataContext db = new DBSourceDataContext();
            Table<dbtable> tbl = db.GetTable<dbtable>();
            dbtable tblRecord = new dbtable();
            tblRecord.ID = PKField;
//*Other assignments here
            tblRecord.photo = new Binary(Photo);

            tbl.InsertOnSubmit(tblRecord);
            tbl.Context.SubmitChanges();</dbtable></dbtable>



我用谷歌搜索了我应该如何将数据传递给服务,似乎我应该将流对象传递给服务调用,但这只允许我将该流作为参数传递,而我却没有将上传的图像与要插入的记录相关联的方法.我该怎么做?



I googled on how am i supposed to pass the data to the service, and it seems like im supposed to pass a stream object to the service call, but it would only allow me to pass that stream as the argument, and i would have no way to associate the image uploaded with the record that is being inserted.. how should i do this?

推荐答案

最后使它起作用了!请按照此处的解决方案进行操作: http://forums.silverlight.net/t/40770.aspx [ ^ ]

好像我只需要增加大小限制...
Finally got it to work! followed the solution here: http://forums.silverlight.net/t/40770.aspx[^]

seems like i only need to increase the size limit...


这篇关于Silverlight WCF引发System.ServiceModel.ComunicationException.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆