Implement and debug a WCF Service (medium) in Windows Azure)

Source: Internet
Author: User
Make some changes

If you have jumped to the final summary file, welcome back.

Now we will make some changes to this basic project so that we can tell you how to expand this solution, how to interrupt it, and how to find out the cause of its interruption.

First, I will add a new method to this service so that we can see how to expand the service. Go to the IService1 interface and add the following code:

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

Now, we have a new method on this interface, and we must implement it. Open "Service1.svc. cs" and add the following code:

Public float Divide (float dividend, float divisor)

{

If (divisor = 0F)

{

Throw new DivideByZeroException ();

}

Return dividend/divisor;
}

Now we have a new method, and we can finally make some things fail!

Run it (or debug) in Visual Studio and you will see the following page:

Although this ensures that the WCF Service works normally, we cannot use a browser to call it. Instead, we will turn to a simple Worker role client that can communicate with WCF.

First, add a new project to the solution and right-click the node in the figure:

Then, this Worker role needs to create a client that can communicate with the previously created WCF Service. To do this, right-click "References" and add a "Service Reference ":

Then, it allows us to choose whether to add an existing service or add a service in a solution. Currently, we use the internal WCF Service of the solution.

Try

{

For (int I = 100; I> = 0; I --)

{

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

}

}

Catch (Exception ex)

{

Trace. TraceError (ex. ToString ());

}

Actually, binding to an existing Azure instance is easier -- this is because binding your solution locally may obtain the wrong port number (the local IIS port is not the port running Windows Azure Emulator) -- if you accidentally shut down your debugging session, the Windows Azure Emulator port may change ). As shown in, when searching for a local solution, an incorrect port number is obtained:

To solve this problem, you can replace this port number with the port you manually configured for the WCF role. You can go to "ServiceDefinition. in the csdef file, configure the port number, or right-click the WCF role and open its property page. In this example, I did this:

Note that you must modify the address attribute of <client> <endpoint> to match the port number configured above. Compute Emulator does not close properly at any time. You must restart them to make sure they match. Otherwise, you will get an exception, it tells you that the specified port in the WCF client configuration does not have an endpoint in the listener.

To successfully call this WCF Service, we need to add some code to the Worker role. We simply iterate from 100 to 0 and use 100F as the parameter to call the Divide method. Finally, When iteration to 0, our code will intentionally throw a "DivideByZeroException" exception.

Try

{

For (int I = 100; I> = 0; I --)

{

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

}

}

Catch (Exception ex)

{

Trace. TraceError (ex. ToString ());

}

The client's WCF communication output will receive a WCF exception, but it will not contain some details.

Snip...

10

11.11111

12.5

14.28571

16.66667

20

25

33.33333

50

100

Wait 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

Theoretically, we can open the details of the exception information, but this is not a safe method. To debug this information, we need to take a look at the Windows Azure Diagnostics Just configured.

 

(This article is reproduced from 51cto, translator Zhou Xuefeng)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.