Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/thrift
diff --git a/compiler/cpp/src/generate/t_csharp_generator.cc b/compiler/cpp/src/generate/t_csharp_generator.cc
index c718353..3bba2b7 100644
--- a/compiler/cpp/src/generate/t_csharp_generator.cc
+++ b/compiler/cpp/src/generate/t_csharp_generator.cc
@@ -48,6 +48,8 @@
 
       std::map<std::string, std::string>::const_iterator iter;
       iter = parsed_options.find("async");
+      async_ = (iter != parsed_options.end());
+      iter = parsed_options.find("asyncctp");
       async_ctp_ = (iter != parsed_options.end());
       iter = parsed_options.find("nullable");
       nullable_ = (iter != parsed_options.end());
@@ -127,7 +129,7 @@
     std::string declare_field(t_field* tfield, bool init=false, std::string prefix="");
     std::string function_signature_async_begin(t_function* tfunction, std::string prefix = "");
     std::string function_signature_async_end(t_function* tfunction, std::string prefix = "");
-    std::string function_signature_async_ctp(t_function* tfunction, std::string prefix = "");
+    std::string function_signature_async(t_function* tfunction, std::string prefix = "");
     std::string function_signature(t_function* tfunction, std::string prefix="");
     std::string argument_list(t_struct* tstruct);
     std::string type_to_enum(t_type* ttype);
@@ -153,6 +155,7 @@
     std::string namespace_name_;
     std::ofstream f_service_;
     std::string namespace_dir_;
+    bool async_;
     bool async_ctp_;
     bool nullable_;
     bool serialize_;
@@ -203,7 +206,7 @@
     "using System.Collections.Generic;\n" +
     "using System.Text;\n" +
     "using System.IO;\n" +
-	(async_ctp_ ? "using System.Threading.Tasks;\n" : "") +
+    ((async_||async_ctp_) ? "using System.Threading.Tasks;\n" : "") +
     "using Thrift;\n" +
     "using Thrift.Collections;\n" +
     (wcf_ ? "//using System.ServiceModel;\n" : "") +
@@ -910,15 +913,19 @@
 
     indent(f_service_) <<
       function_signature(*f_iter) << ";" << endl;
-    indent(f_service_) << "#if SILVERLIGHT" << endl;
+    if(!async_) {
+      indent(f_service_) << "#if SILVERLIGHT" << endl;
+    }
     indent(f_service_) <<
       function_signature_async_begin(*f_iter, "Begin_") << ";" << endl;
     indent(f_service_) <<
       function_signature_async_end(*f_iter, "End_") << ";" << endl;  
-    if( async_ctp_)
+    if(async_||async_ctp_) {
       indent(f_service_) <<
-        function_signature_async_ctp(*f_iter) << ";" << endl;  
-    indent(f_service_) << "#endif" << endl;
+        function_signature_async(*f_iter) << ";" << endl;
+    } else {
+      indent(f_service_) << "#endif" << endl;
+    }
   }
   indent_down();
   f_service_ <<
@@ -996,9 +1003,11 @@
     string funname = (*f_iter)->get_name();
 
     indent(f_service_) << endl;
-	
-    indent(f_service_) << "#if SILVERLIGHT" << endl;
-	// Begin_
+    
+    if (!async_) {
+      indent(f_service_) << "#if SILVERLIGHT" << endl;
+    }
+    // Begin_
     indent(f_service_) <<
       "public " << function_signature_async_begin(*f_iter, "Begin_") << endl;
     scope_up(f_service_);
@@ -1010,8 +1019,8 @@
     const vector<t_field*>& fields = arg_struct->get_members();
     vector<t_field*>::const_iterator fld_iter;
     for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
-	    f_service_ << ", ";
-        f_service_ << (*fld_iter)->get_name();
+      f_service_ << ", ";
+      f_service_ << (*fld_iter)->get_name();
     }
     f_service_ << ");" << endl;
     scope_down(f_service_);
@@ -1034,26 +1043,26 @@
     scope_down(f_service_);
     f_service_ << endl;
 
-    // async CTP
-	bool first;
-    if( async_ctp_)
-	{
+    // async
+    bool first;
+    if( async_||async_ctp_) {
       indent(f_service_) <<
-        "public async " << function_signature_async_ctp(*f_iter, "") << endl;
+	"public async " << function_signature_async(*f_iter, "") << endl;
       scope_up(f_service_);
-	
+      
       if (!(*f_iter)->get_returntype()->is_void()) {
         indent(f_service_) <<
           type_name( (*f_iter)->get_returntype()) << " retval;" << endl;
         indent(f_service_) <<
           "retval = ";
-      }
-	  else
-	  {
+      } else {
         indent(f_service_);
-	  }
-      f_service_ <<
-        "await TaskEx.Run(() =>" << endl;
+      }
+      if (async_) {
+	f_service_ << "await Task.Run(() =>" << endl;
+      } else {
+	f_service_ << "await TaskEx.Run(() =>" << endl;
+      }
       scope_up(f_service_);
       indent(f_service_);
       if (!(*f_iter)->get_returntype()->is_void()) {
@@ -1082,8 +1091,10 @@
 	  scope_down(f_service_);
       f_service_ << endl;
     }
-	
-    indent(f_service_) << "#endif" << endl << endl;
+    
+    if (!async_) {
+      indent(f_service_) << "#endif" << endl << endl;
+    }
 
     // "Normal" Synchronous invoke
     generate_csharp_doc(f_service_, *f_iter);
@@ -1151,11 +1162,15 @@
 
     string argsname = (*f_iter)->get_name() + "_args";
 
-    indent(f_service_) << "#if SILVERLIGHT" << endl;
+    if (!async_) {
+      indent(f_service_) << "#if SILVERLIGHT" << endl;
+    }
     indent(f_service_) << "public " << function_signature_async_begin(&send_function) << endl;
-    indent(f_service_) << "#else" << endl;
-    indent(f_service_) << "public " << function_signature(&send_function) << endl;
-    indent(f_service_) << "#endif" << endl;
+    if (!async_) {
+      indent(f_service_) << "#else" << endl;
+      indent(f_service_) << "public " << function_signature(&send_function) << endl;
+      indent(f_service_) << "#endif" << endl;
+    }
     scope_up(f_service_);
 
     f_service_ <<
@@ -1170,12 +1185,16 @@
     f_service_ <<
       indent() << "args.Write(oprot_);" << endl <<
       indent() << "oprot_.WriteMessageEnd();" << endl;;
-
-    indent(f_service_) << "#if SILVERLIGHT" << endl;
+    
+    if (!async_) {
+      indent(f_service_) << "#if SILVERLIGHT" << endl;
+    }
     indent(f_service_) << "return oprot_.Transport.BeginFlush(callback, state);" << endl;
-    indent(f_service_) << "#else" << endl;
-    indent(f_service_) << "oprot_.Transport.Flush();" << endl;
-    indent(f_service_) << "#endif" << endl;
+    if (!async_) {
+      indent(f_service_) << "#else" << endl;
+      indent(f_service_) << "oprot_.Transport.Flush();" << endl;
+      indent(f_service_) << "#endif" << endl;
+    }
 
     scope_down(f_service_);
     f_service_ << endl;
@@ -2000,7 +2019,7 @@
   return type_name(ttype) + " " + prefix + tfunction->get_name() + "(IAsyncResult asyncResult)";
 }
 
-string t_csharp_generator::function_signature_async_ctp(t_function* tfunction, string prefix) {
+string t_csharp_generator::function_signature_async(t_function* tfunction, string prefix) {
   t_type* ttype = tfunction->get_returntype();
   string task = "Task";
   if( ! ttype->is_void())
@@ -2122,7 +2141,8 @@
 }
 
 THRIFT_REGISTER_GENERATOR(csharp, "C#",
-"    async:           Adds Async CTP support.\n"
+"    async:           Adds Async support using Task.Run.\n"
+"    asyncctp:        Adds Async CTP support using TaskEx.Run.\n"
 "    wcf:             Adds bindings for WCF to generated classes.\n"
 "    serial:          Add serialization support to generated classes.\n"
 "    nullable:        Use nullable types for properties.\n"