THRIFT-4684: Fix WCF related C# code generation
Client: C#
Complete the namespace in System.ServiceModel in WCF related code.
Also when generating fields in fault classes, we follow the same behavior in normal struct,
to avoid generating unused private fields, when auto properties are also generated.
diff --git a/compiler/cpp/src/thrift/generate/t_csharp_generator.cc b/compiler/cpp/src/thrift/generate/t_csharp_generator.cc
index 37d6f9d..9d1e29e 100644
--- a/compiler/cpp/src/thrift/generate/t_csharp_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_csharp_generator.cc
@@ -422,7 +422,7 @@
+ ((async_) ? "using System.Threading.Tasks;\n" : "") + "using Thrift;\n"
+ "using Thrift.Collections;\n" + ((serialize_ || wcf_) ? "#if !SILVERLIGHT\n" : "")
+ ((serialize_ || wcf_) ? "using System.Xml.Serialization;\n" : "")
- + ((serialize_ || wcf_) ? "#endif\n" : "") + (wcf_ ? "//using System.ServiceModel;\n" : "")
+ + ((serialize_ || wcf_) ? "#endif\n" : "")
+ "using System.Runtime.Serialization;\n";
}
@@ -903,7 +903,10 @@
// make private members with public Properties
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "private " << declare_field(*m_iter, false, "_") << endl;
+ // if the field is requied, then we use auto-properties
+ if (!field_is_required((*m_iter)) && (!nullable_ || field_has_default((*m_iter)))) {
+ indent(out) << "private " << declare_field(*m_iter, false, "_") << endl;
+ }
}
out << endl;
@@ -1475,7 +1478,7 @@
generate_csharp_doc(f_service_, tservice);
if (wcf_) {
- indent(f_service_) << "[ServiceContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl;
+ indent(f_service_) << "[System.ServiceModel.ServiceContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl;
}
indent(f_service_) << "public interface ISync" << extends_iface << " {" << endl;
@@ -1487,12 +1490,12 @@
// if we're using WCF, add the corresponding attributes
if (wcf_) {
- indent(f_service_) << "[OperationContract]" << endl;
+ indent(f_service_) << "[System.ServiceModel.OperationContract]" << endl;
const std::vector<t_field*>& xceptions = (*f_iter)->get_xceptions()->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- indent(f_service_) << "[FaultContract(typeof("
+ indent(f_service_) << "[System.ServiceModel.FaultContract(typeof("
+ type_name((*x_iter)->get_type(), false, false) + "Fault))]" << endl;
}
}
@@ -1514,7 +1517,7 @@
generate_csharp_doc(f_service_, tservice);
if (wcf_) {
- indent(f_service_) << "[ServiceContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl;
+ indent(f_service_) << "[System.ServiceModel.ServiceContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl;
}
indent(f_service_) << "public interface IAsync" << extends_iface << " {" << endl;
@@ -1526,12 +1529,12 @@
// if we're using WCF, add the corresponding attributes
if (wcf_) {
- indent(f_service_) << "[OperationContract]" << endl;
+ indent(f_service_) << "[System.ServiceModel.OperationContract]" << endl;
const std::vector<t_field*>& xceptions = (*f_iter)->get_xceptions()->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- indent(f_service_) << "[FaultContract(typeof("
+ indent(f_service_) << "[System.ServiceModel.FaultContract(typeof("
+ type_name((*x_iter)->get_type(), false, false) + "Fault))]" << endl;
}
}
@@ -1552,7 +1555,7 @@
generate_csharp_doc(f_service_, tservice);
if (wcf_) {
- indent(f_service_) << "[ServiceContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl;
+ indent(f_service_) << "[System.ServiceModel.ServiceContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl;
}
indent(f_service_) << "public interface Iface" << extends_iface << " {" << endl;