摘要:[WCF]呼叫問題
呼叫WCF時會出現三種可能性的錯誤:
a.不安全性的訊息
b.程式開啟一段時間IDLE之後,會發生WCF TimeOut
c.程式錯誤造成的WCF Channel Fault
其中目前a錯誤尚無明確之解決方案,但是可以編輯WCF的呼叫權限:
利用 Visual Studio的 "Tools" -> "WCF Configuration Editor"編輯 WCF 的呼叫權限.
若是內部的(Intranet),可以將 Security全部關閉.
Client(Service Consumer):
(a)開啟 config檔(web.config, app.config)
(b)點選 "Binding"(繫結)小節
(c)選擇 "WSHttpBinding"的 "安全性"(Securty)頁
(d)將 "Mode"改為 None
Service Provider:
(a)開啟 config檔(Web.config)
(a)開啟 config檔(Web.config)
(b)點選 "服務" 小節
(c)各個 Service 點選 "wsHttpBinding" 的 "Binding Configuration"設定
(d)點選剛剛新增的 "Binding"(繫結)
(e)點選 "Security"(安全性)
(f)將 "Mode" 改為 "None"
(g)選擇所有 Service的"端點",如果是 "wsHttpBinding",就將 Binding Configuration,改為無安全性的 Configuration
b與c可改變叫用方式於每次叫用前檢查頻道是否可用:
private ServiceProxy.OTHRService.OTHRServiceClient _othrservice = null;
public ServiceProxy.OTHRService.OTHRServiceClient OTHRService
{
get
{
if (_othrservice == null)
{
_othrservice = new ServiceProxy.OTHRService.OTHRServiceClient();
}
//如果Service已經錯誤,則強迫中止
if (_othrservice.State == System.ServiceModel.CommunicationState.Faulted)
{
_othrservice.Abort();
}
if (_othrservice.State != System.ServiceModel.CommunicationState.Opened)
{
try
{
_othrservice.Open();
}
catch
{
_othrservice = new ServiceProxy.OTHRService.OTHRServiceClient();
}
}
return _othrservice;
}
}
public ServiceProxy.OTHRService.OTHRServiceClient OTHRService
{
get
{
if (_othrservice == null)
{
_othrservice = new ServiceProxy.OTHRService.OTHRServiceClient();
}
//如果Service已經錯誤,則強迫中止
if (_othrservice.State == System.ServiceModel.CommunicationState.Faulted)
{
_othrservice.Abort();
}
if (_othrservice.State != System.ServiceModel.CommunicationState.Opened)
{
try
{
_othrservice.Open();
}
catch
{
_othrservice = new ServiceProxy.OTHRService.OTHRServiceClient();
}
}
return _othrservice;
}
}