我有WCF服务返回以下DataContract:
[DataContract]
public class Employee
{
[DataMember]
public DataSet DS { get; set; }
[DataMember]
public string ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
}
在GetEmployee服务中,我返回的是Employee对象,但是除DS属性(其中包含Nodes列表内的Employee属性的值)外,所有列的值都返回为null。
public Employee GetEmployee()
{
return new Employee
{
ID = "76072",
Name = "name",
Email = "emp@test.com",
DS = null
};
}
但是,如果我从Employee DataContract中删除DS属性,它将正确返回属性中的数据。
我的问题是为什么WCF隐式返回DataSet属性中的数据?
答案 0 :(得分:1)
我不建议使用数据集对象作为要在WCF中传递的属性。这对WCF来说是一个巨大的负担。尽管这可能无法完全回答您的问题,但Scott Hanselman支持我:Returning Datasets in webservices is the Spawn of Satan
答案 1 :(得分:0)
我已经通过分配新的Dataset()对象而不是null来解决它,如下所示:
public Employee GetEmployee()
{
return new Employee
{
ID = "76072",
Name = "name",
Email = "emp@test.com",
DS = new DataSet()
};
}
现在在WCF服务的客户端中,正确映射了雇员的属性,而不是在DS属性中返回了雇员的属性。
答案 2 :(得分:0)
我是否误解了您的问题,或者我丢失了配置?我无法根据您的步骤重现您的问题。在我这边客户可以正常接收正确的值。我建议您将名称空间添加到数据契约中。
这是我的演示,希望对您有用。
服务器(IP:10.157.13.69)
class Program
{
static void Main(string[] args)
{
using (ServiceHost sh = new ServiceHost(typeof(MyService)))
{
sh.Opened += delegate
{
Console.WriteLine("service is ready...");
};
sh.Closed += delegate
{
Console.WriteLine("Service is closed");
};
sh.Open();
Console.ReadLine();
sh.Close();
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
Product SayHello();
}
public class MyService : IService
{
public Product SayHello()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Rows.Add(10, "Lemon");
dt.Rows.Add(11, "Peach");
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return new Product()
{
Id = 1,
Name = "Apple",
DS = ds
};
}
}
[DataContract(Namespace ="MyCorporation")]
public class Product
{
[DataMember]
public DataSet DS { get; set; }
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
}
App.config(服务器)
<system.serviceModel>
<services>
<service name="Server6.MyService" behaviorConfiguration="mybeh">
<endpoint address="" binding="basicHttpBinding" contract="Server6.IService" >
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:13060"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mybeh">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"></serviceMetadata>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户。
class Program
{
static void Main(string[] args)
{
ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
try
{
var result = client.SayHello();
var r2 = result.DS.Tables[0];
Console.WriteLine($"{result.Id},{result.Name}");
Console.WriteLine($"{r2.Rows[0][0]},{r2.Rows[0][1]}\n{r2.Rows[1][0]},{r2.Rows[1][1]}");
}
catch (Exception)
{
throw;
}
}
}
App.config(客户端)。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://10.157.13.69:13060/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>