在Windows Azure中實現(xiàn)和調(diào)試一個WCF服務(wù)(中)
原創(chuàng)本文接《在Windows Azure中實現(xiàn)和調(diào)試一個WCF服務(wù)(上)》和《在Windows Azure中實現(xiàn)和調(diào)試一個WCF服務(wù)(下)》
做一些改動
如果你跳轉(zhuǎn)到了***的總結(jié)文件上,那么歡迎回來。
現(xiàn)在我們會對這個基礎(chǔ)項目做一些改動,以便于我們可以告訴大家,如何擴(kuò)展這個解決方案的,如何中斷它,以及如何找出它中斷的原因。
首先,我會給這個服務(wù)添加一個新的方法,以便于我們可以看到如何開始擴(kuò)展這個服務(wù)。定位到“IService1”接口,然后添加下面這些代碼:
[OperationContract]
float Divide(float dividend, float divisor);
現(xiàn)在,在這個接口上,我們擁有了一個新方法,我們必須要實現(xiàn)他。打開“Service1.svc.cs”,然后添加下面這些代碼:
public float Divide(float dividend, float divisor)
{
if (divisor == 0F)
{
throw new DivideByZeroException();
}
return dividend / divisor;
}
現(xiàn)在,我們擁有了一個新方法,我們終于可以讓一些事情失敗了!
在Visual Studio中運(yùn)行它(或者debug),然后你會看到下面這個頁面:
雖然這可以保證這個WCF服務(wù)是可以正常工作的,但是我們無法使用瀏覽器來調(diào)用它。取而代之,我們會求助于一個簡單的,可以和WCF進(jìn)行通信的Worker角色客戶端。
首先,向這個解決方案中添加一個新的項目,在圖中那個節(jié)點上右擊:
然后,這個Worker角色需要創(chuàng)建一個可以和我們前面創(chuàng)建的WCF服務(wù)進(jìn)行通信的客戶端。要完成這個工作,需要在“References”上右擊,然后添加一個“Service Reference”:
然后,它可以讓我們選擇是添加一個現(xiàn)有的服務(wù),還是添加是一個解決方案中的服務(wù)。目前來說,我們使用解決方案內(nèi)部的WCF服務(wù)。
try
{
for (int i = 100; i >= 0; i--)
{
Trace.WriteLine(service1.Divide(100F, (float)i));
}
}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
}
實際上,綁定到一個現(xiàn)有Azure實例會更加容易一些——這是因為在本地綁定你的解決方案可能會獲得錯誤的端口號(本地的IIS端口而不是Windows Azure Emulator運(yùn)行的端口——如果你沒有小心地關(guān)閉掉你的調(diào)試會話,那么這個Windows Azure Emulator端口可能會改變)。如下圖所示,當(dāng)查找一個本地解決方案的時候,獲得了一個錯誤的端口號:
為了糾正這個問題,可以用你為WCF角色手工配置的端口替換掉這個端口號。你可以在“ServiceDefinition.csdef”文件中配置這個端口號,或者也可以通過在WCF角色上右擊,然后打開它的屬性頁的方式來達(dá)到這個目的,在這個例子中,我就是這樣做的:
注意,接下來你必須修改<client><endpoint>的地址屬性,讓它的端口號和上面配置的端口號相匹配。任何時候,Compute Emulator都不會正確地關(guān)閉,你必須重啟它們,以確保它們是匹配的,否則,你會得到一個異常,告訴你在WCF客戶端配置中指定的端口沒有端點(endpoint)在監(jiān)聽。
為了成功地調(diào)用這個WCF服務(wù),我們需要給Worker角色添加一些代碼。我們簡單地從100迭代到0,以100F作為參數(shù)調(diào)用Divide方法,***,在迭代到0的時候,我們的代碼會故意地拋出一個“DivideByZeroException”異常。
try
{
for (int i = 100; i >= 0; i--)
{
Trace.WriteLine(service1.Divide(100F, (float)i));
}
}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
}
客戶端的WCF通信輸出會收到一個WCF異常,但是不會包含一些細(xì)節(jié)。
snip...
10
11.11111
12.5
14.28571
16.66667
20
25
33.33333
50
100
[WaWorkerHost.exe] System.ServiceModel.FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at WcfClientRole.AzureWcfBasic.IService1.Divide(Single dividend, Single divisor)
at WcfClientRole.AzureWcfBasic.Service1Client.Divide(Single dividend, Single divisor) in c:\dev\Blog\WCFBasic\WcfClientRole\Service References\AzureWcfBasic\Reference.cs:line 119
at WcfClientRole.WorkerRole.Run() in c:\dev\Blog\WCFBasic\WcfClientRole\WorkerRole.cs:line 31
從理論上來說,我們可以打開異常信息的細(xì)節(jié),但是這不是一種安全的做法。為了接下來可以調(diào)試這個信息,我們需要看一看剛才配置過的Windows Azure Diagnostics。
本文接《在Windows Azure中實現(xiàn)和調(diào)試一個WCF服務(wù)(上)》和《在Windows Azure中實現(xiàn)和調(diào)試一個WCF服務(wù)(下)》
原文名:Implementing and Debugging a WCF Service in Windows Azure 作者:Andy
【本文乃51CTO精選譯文,轉(zhuǎn)載請標(biāo)明出處!】
【編輯推薦】
- 微軟公布云計算平臺Azure收費模式細(xì)節(jié)
- 云計算意在長遠(yuǎn),微軟云計算服務(wù)Windows Azure已經(jīng)啟用
- 技術(shù)透析:Windows Azure Platform框架與組成
- 微軟Windows Azure Platform技術(shù)解析
- 走近微軟云:SQL Server到Azure數(shù)據(jù)同步
- 當(dāng)微軟Azure遭遇亞馬遜EC2:五大關(guān)鍵區(qū)別
- Windows Azure云計算平臺新增五大功能
- 云計算前途光明 Azure用戶數(shù)突破31000
- 如何把應(yīng)用程序部署到Windows Azure中