我正在從我的Silverlight應用程序調用WCF服務方法。 Wcf服務正在返回故障時的故障異常。我能夠從我的WCF服務中拋出異常異常。但它沒有收到我的Silverlight應用程序(.xaml.cs)。相反,我得到一個異常「通訊異常是由用戶未處理,遠程服務器在References.cs文件(自動生成文件)中返回一個錯誤:未找到」 無法捕獲由Silverlight客戶端中的WCF服務拋出的FaultException應用程序
我在我的.Xaml.cs中調用WCF服務方法文件中像下面
private void btnExecuteQuery_Click(object sender, RoutedEventArgs e)
{
try
{
objService.GetDataTableDataAsync(_DATABASENAME, strQuery);
objService.GetDataTableDataCompleted += new EventHandler<GetDataTableDataCompletedEventArgs>(objService_GetDataTableDataCompleted);
}
catch (FaultException<MyFaultException> ex)
{
lblErrorMessage.Content = "Please Enter a Valid SQL Query";
}
}
And I wrote my GetDataTableDataCompleted event as below
void objService_GetDataTableDataCompleted(object sender, GetDataTableDataCompletedEventArgse)
{
//code
}
這裏是我的服務方法
public IEnumerable<Dictionary<string, object>> GetDataTableData(string dataBaseName, string query)
{
try
{
IEnumerable<Dictionary<string, object>> objDictionary;
objDictionary = objOptimityDAL.GetDataForSelectQuery(dataBaseName, query);
return objDictionary;
}
catch (Exception ex)
{
MyFaultException fault = new MyFaultException();
fault.Reason = ex.Message.ToString();
throw new FaultException<MyFaultException>(fault, new FaultReason("Incorrect SQL Query"));
}
}
這裏我的WCF服務與數據訪問層進行交互,併成功拋出故障異常,但沒有收到我的客戶方法,而我得到的未處理的異常像 「通信異常是由用戶未處理,則遠程服務器返回錯誤:NOTFOUND」在References.cs代碼如下所示
public System.Collections.ObjectModel.ObservableCollection<System.Collections.Generic.Dictionary<string, object>> EndGetDataTableData(System.IAsyncResult result) {
object[] _args = new object[0];
System.Collections.ObjectModel.ObservableCollection<System.Collections.Generic.Dictionary<string, object>> _result = ((System.Collections.ObjectModel.ObservableCollection<System.Collections.Generic.Dictionary<string, object>>)(base.EndInvoke("GetDataTableData", _args, result)));
return _result;
}
這裏是WCF的Web.config服務
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
下面是我的ServiceReferences.ClientConfig文件
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
closeTimeout="01:00:00"
receiveTimeout="01:00:00"
sendTimeout="01:00:00">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:3132/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
請建議我好歹趕上的FaultException在我的實verlightClient
由於提前
或許有在配置,因爲你得到通信異常一些錯誤。服務是否可以接到客戶的電話?這聽起來像你試圖調用服務時出現錯誤?也許發佈你的web.configs和完整的堆棧跟蹤? – Jocke
嗨Jocke,感謝您的回覆,我已經使用Config文件編輯了我的文章,並且我能夠成功地調用服務方法,它引發了一個異常,但這裏的問題在於捕獲服務中拋出的異常。 –
你有做[FaultContract](http://msdn.microsoft.com/en-us/library/system.servicemodel.faultcontractattribute(v = vs.95)。aspx)'[FaultContract(typeof(MyFaultException))]'? [您可以閱讀本文](http://msdn.microsoft.com/en-us/library/ee844556(v = vs.95).aspx)以獲取更多詳細信息。 – Tonio