“不是Found"在Silverlight 3中尝试通过WCF保存到SQL Server时出现错误
本文关键字:保存 WCF SQL Server 错误 quot Found 不是 Silverlight | 更新日期: 2023-09-27 18:01:29
我正在尝试使用。net 3.5框架上的Silverlight 3将一些数据保存到SQL Server 2008数据库。
这里是关于这里发生的事情的概要。
当应用程序启动时,它调用WCF从DB下载一个包含行的List对象,这样所有可用的数据都在客户端,防止需要重复的客户机-服务器数据库调用。下载数据正常,并反馈数据下载成功。
用户在文本框中输入信息。当用户准备好了,他们点击"保存",调用WCF保存这些数据,在一个加密的字符串可能不超过2kb的大小,连同一些整数和短字符串字段。
当他们点击Save时,会抛出以下异常:
远程服务器返回一个错误:NotFound.
我怎么也弄不明白发生了什么事。未保存数据
我试过的事:
- 在web中添加a绑定。配置(实际上导致第一次WCF调用失败并出现相同的错误)
- 用这个问题的内容添加一个crossdomainpolicy.xml文件
- 增加maxReceivedMessageSize和maxBufferSize的大小
- 将"includeExceptionDetailInFaults"设置为"True"(它没有提供任何有用的数据)
web . config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="AFF_DataConnectionString"
connectionString="Data Source=.'SQLEXPRESS;AttachDbFilename=|DataDirectory|'AFF_Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DataWCFBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DataWCFBehavior" name="DataWCF">
<endpoint address="" binding="basicHttpBinding" contract="IDataWCF">
<identity>
<dns value="localhost:4493"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
</configuration>
DataWCF.cs(函数)
void IDataWCF.SaveFile(int theUserId, int theCustId, int theDairyCat, string theFilename, string theData, string theFeedId, string theFileType)
{
using (var db = new DataClassesDataContext())
{
var theFile = new AFF_DairyFile
{
userID = theUserId,
custID = theCustId,
dairyCat = theDairyCat,
filename = theFilename,
data = theData,
feedID = Convert.ToInt32(theFeedId),
fileType = theFileType
};
db.AFF_DairyFiles.InsertOnSubmit(theFile);
db.SubmitChanges();
}
}
IDataWCF.cs
[ServiceContract]
public interface IDataWCF {
[OperationContract]
string GetFeed(String feedID);
[OperationContract]
List<Attribute> getNutrients();
[OperationContract]
List<Attribute> getNutrients2(string nutrientID);
[OperationContract]
List<Feed> getFeeds();
[OperationContract]
void SaveFile(int userId, int custId, int dairyCat, string filename, string data, string feedId, string fileType);
}
[DataContract]
public class Attribute
{
[DataMember]
public decimal specCorrectVal = 0m;
[DataMember]
public decimal specVal = 0m;
[DataMember]
public string specID = "";
[DataMember]
public string feedID = "";
}
[DataContract]
public class Feed
{
[DataMember]
public string feedID;
[DataMember]
public string feedName;
}
ServiceReferences。ClientConfig
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataWCF" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:4493/DairyCalcApp.Web/DataWCF.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IDataWCF"
contract="DataWCF.IDataWCF" name="BasicHttpBinding_IDataWCF" />
</client>
</system.serviceModel>
</configuration>
调用WCF失败的代码
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
var theUserId = 0; // Real user ID here
var theCustId = 0; // Real Customer ID here
var theDairyCat = 1;
var theFilename = FileNameTextBox.Text;
var theData = AssembleData();
var theFeedId = "";
var theFileType = "Ration";
var proxy = new DairyCalcApp.DataWCF.DataWCFClient();
try
{
proxy.SaveFileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_SaveFileCompleted);
proxy.SaveFileAsync(theUserId,theCustId,theDairyCat,theFilename,theData,theFeedId,theFileType);
}
catch (Exception ex)
{
FileStatusLabel.Text = ex.InnerException.Message;
}
}
void proxy_SaveFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
FileStatusLabel.Text = "File Saved!";
LastEditDate.Text = DateTime.Now.ToShortTimeString();
}
我能给你的简短答案是调试你的wcf服务。
以调试模式启动wcf服务,并在被调用的方法上设置一个断点。启动您的Silverlight客户端并调用save方法(按下按钮等),WCF服务断点应该被击中,您可以发现后端发生了什么。
信息:
The remote server returned an error: NotFound.
是从WCF服务返回的通用错误。Silverlight错误(故障)开箱处理不是很好。但是如果你在WCF服务上设置了一个断点,你应该能够找出后端发生的异常并修复代码。
之后,我将实现适当的Silverlight错误处理,以便错误以某种方式从WCF服务发送到SL客户端,并且您不会得到此通用消息与WCF服务上发生的任何未处理的异常。
如果数据库不可用、无法插入数据、违反约束等,这里的代码肯定会抛出异常:
using (var db = new DataClassesDataContext())
{
var theFile = new AFF_DairyFile
{
userID = theUserId,
custID = theCustId,
dairyCat = theDairyCat,
filename = theFilename,
data = theData,
feedID = Convert.ToInt32(theFeedId),
fileType = theFileType
};
db.AFF_DairyFiles.InsertOnSubmit(theFile);
db.SubmitChanges();
}