62,243
社区成员




using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Data;
namespace DataSetInWCFService
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Press <ENTER> to start service.");
System.Console.ReadLine();
StartService();
Console.WriteLine("Serivce started.");
System.Console.WriteLine("Press <ENTER> to terminate service.");
System.Console.ReadLine();
StopService();
Console.WriteLine("Serivce stopped.");
}
static ServiceHost BNIIIBusinessDuplexServiceHost = null;
static void StartService()
{
BNIIIBusinessDuplexServiceHost = new ServiceHost(typeof(DataSetInWCFService.ServiceLib));
BNIIIBusinessDuplexServiceHost.Open();
}
static void StopService()
{
if (BNIIIBusinessDuplexServiceHost.State != CommunicationState.Closed)
{
BNIIIBusinessDuplexServiceHost.Close();
}
}
}
public class ServiceLib : DataSetInWCFService.IServiceLib
{
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("TestCol", typeof(string));
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["TestCol"] = "SomeMessages";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
public string GetString()
{
return "SomeMessages";
}
}
[ServiceContract(Namespace = "http://DataSetInWCF/TestService")]
public interface IServiceLib
{
[OperationContract()]
System.Data.DataSet GetDataSet();
[OperationContract()]
string GetString();
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="DataSetInWCFService.ServiceLib">
<clear />
<endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
name="DataSetInWCFBHttp" contract="DataSetInWCFService.IServiceLib" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:3001/DataSetInWCF/TestService" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="NewBinding0" />
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DataSetInWCFClient
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Press <ENTER> to start service.");
System.Console.ReadLine();
using (ServiceReference1.ServiceLibClient client = new DataSetInWCFClient.ServiceReference1.ServiceLibClient("DataSetInWCFBHttp"))
{
DataSet ds= client.GetDataSet();
Console.WriteLine(ds.Tables[0].Rows[0]["TestCol"] + "");
}
System.Console.ReadLine();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:3001/DataSetInWCF/TestService"
binding="basicHttpBinding"
contract="ServiceReference1.IServiceLib" name="DataSetInWCFBHttp" />
</client>
</system.serviceModel>
</configuration>