在Windows Azure中實現和調試一個WCF服務(中)

來源:互聯網
上載者:User
做一些改動

如果你跳轉到了最後的總結檔案上,那麼歡迎回來。

現在我們會對這個基礎項目做一些改動,以便於我們可以告訴大家,如何擴充這個解決方案的,如何中斷它,以及如何找出它中斷的原因。

首先,我會給這個服務添加一個新的方法,以便於我們可以看到如何開始擴充這個服務。定位到“IService1”介面,然後添加下面這些代碼:

[OperationContract]       
float Divide(float dividend, float divisor);

現在,在這個介面上,我們擁有了一個新方法,我們必須要實現他。開啟“Service1.svc.cs”,然後添加下面這些代碼:

public float Divide(float dividend, float divisor) 

{

if (divisor == 0F)

{

throw new DivideByZeroException();    

 }

return dividend / divisor;
}

現在,我們擁有了一個新方法,我們終於可以讓一些事情失敗了!

在Visual Studio中運行它(或者debug),然後你會看到下面這個頁面:

雖然這可以保證這個WCF服務是可以正常工作的,但是我們無法使用瀏覽器來調用它。取而代之,我們會求助於一個簡單的,可以和WCF進行通訊的Worker角色用戶端。

首先,向這個解決方案中添加一個新的項目,在圖中那個節點上右擊:

然後,這個Worker角色需要建立一個可以和我們前面建立的WCF服務進行通訊的用戶端。要完成這個工作,需要在“References”上右擊,然後添加一個“Service Reference”:

然後,它可以讓我們選擇是添加一個現有的服務,還是添加是一個解決方案中的服務。目前來說,我們使用解決方案內部的WCF服務。

try

{

for (int i = 100; i >= 0; i--)

{

Trace.WriteLine(service1.Divide(100F, (float)i));

}

}

catch (Exception ex)

{

Trace.TraceError(ex.ToString());

}

實際上,綁定到一個現有Azure執行個體會更加容易一些——這是因為在本地綁定你的解決方案可能會獲得錯誤的連接埠號碼(本地的IIS連接埠而不是 Windows Azure Emulator啟動並執行連接埠——如果你沒有小心地關閉掉你的偵錯工作階段,那麼這個Windows Azure Emulator連接埠可能會改變)。如所示,當尋找一個本地解決方案的時候,獲得了一個錯誤的連接埠號碼:
 

為了糾正這個問題,可以用你為WCF角色手工配置的連接埠替換掉這個連接埠號碼。你可以在“ServiceDefinition.csdef”檔案中配置這個連接埠號碼,或者也可以通過在WCF角色上右擊,然後開啟它的屬性頁面的方式來達到這個目的,在這個例子中,我就是這樣做的:

注意,接下來你必須修改<client><endpoint>的地址屬性,讓它的連接埠號碼和上面配置的連接埠號碼相匹配。任何時 候,Compute Emulator都不會正確地關閉,你必須重啟它們,以確保它們是匹配的,否則,你會得到一個異常,告訴你在WCF用戶端配置中指定的連接埠沒有端點 (endpoint)在監聽。

為了成功地調用這個WCF服務,我們需要給Worker角色添加一些代碼。我們簡單地從100迭代到0,以100F作為參數調用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異常,但是不會包含一些細節。

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

從理論上來說,我們可以開啟異常資訊的細節,但是這不是一種安全的做法。為了接下來可以調試這個資訊,我們需要看一看剛才配置過的Windows Azure Diagnostics。

 

(本文轉載自51cto,譯者周雪峰) 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.