Incorrect use of yield in WCF-errors that may be made by 99% of developers [Part 1]

Source: Internet
Author: User

When defining APIs, many people like to define the return type as IEnumerable for some methods to return collection objects. This is no problem. Here we will talk about another problem: for the method with the returned type IEnumerable <T>, we can use yield return to output elements of the returned set. However, if we do not understand the implementation mechanism behind the yield keyword, it may cause a lot of problems.

This is a WCF-related issue. I think 99% of people may make such a mistake-even if you have a thorough understanding of yield. In other words, we can use a simple example to illustrate this problem. For example, the next IDemoService interface is defined as a service contract. The unique method GetItems returns an IEnumerable <string> Object of the type and has a unique string parameter category.

   1:  [ServiceContract]
   2:  public interface IDemoService
   3:  {
   4:      [OperationContract]
   5:      IEnumerable<string> GetItems(string category);
   6:  }

The following describes the implementation of DemoService for this contract interface: The GetItems method returns a set containing three strings, but we need to verify the parameter before returning it. If the string provided by the category parameter is Null or an empty string, A FaultException exception is thrown and the message "Invalid Category" is displayed. In this way, the client can get an error message when an Invalid parameter is entered.This programming method is no longer normal, isn't it?

public class DemoService : IDemoService{    public IEnumerable<string> GetItems(string categoty)    {        if (string.IsNullOrEmpty(categoty))        {            throw new FaultException("Invalid category");        }        yield return "Foo";        yield return "Bar";        yield return "Baz";    }}

However, normal does not mean that the client is correct. In fact, the client cannot obtain the error message provided by the server. The following shows the error message obtained when the client calls the service by specifying an empty string parameter. A CommunicationException is thrown. the returned error message is "An error occurred while loading ing the HTTP response to http: // 127.0.0.1: 3721/demoservice. this cocould be due to the service endpoint binding not using the HTTP protocol. this coshould also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down ). see server logs for more details."

This seems to be different from what we expected. what we hope is that the client will throw a FaultException and prompt "Invalid category ". This is actually because "yield" is at work. If you don't believe it, you can replace the GetItems method defined in DemoService with the following definition, that is,String []Object.

public class DemoService : IDemoService{    public IEnumerable<string> GetItems(string categoty)    {        if (string.IsNullOrEmpty(categoty))        {            throw new FaultException("Invalid category");        }        return new string[] { "Foo", "Bar", "Baz" };    }}

Run our program again. This time we can get the expected results.

If you are interested, you can think about why the two seemingly equivalent methods have completely different results. For details, refer to [next].

Incorrect use of yield in WCF-errors that may be made by 99% of developers [Part 1]

Incorrect use of yield in WCF-errors that may be made by 99% of developers [Part II]

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.