using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
namespace WCFPractice
{
[DataContract(Name = "Data")]
public class ServiceData : IExtensibleDataObject
{
[DataMember(Name = "Info")]
private string info;
public string Info
{
get { return info; }
set { info = value; }
}
private ExtensionDataObject extensionData;
public ExtensionDataObject ExtensionData
{
get { return extensionData; }
set { extensionData = value; }
}
}
[DataContract(Name = "Data")]
public class ClientData : IExtensibleDataObject
{
[DataMember(Name = "Info")]
private string info;
public string extraInfo;
public string Info
{
get { return info; }
set { info = value; }
}
private ExtensionDataObject extensionData;
public ExtensionDataObject ExtensionData
{
get { return extensionData; }
set { extensionData = value; }
}
}
[ServiceContract(Name = "GO")]
public interface IService
{
[OperationContract(Name = "DispInfo")]
void MakeSense(string info);
[OperationContract(Name = "DispEntity")]
void MakeSense(ServiceData serviceData);
}
public class ServiceImplementation : IService
{
public void MakeSense(string info)
{
Console.WriteLine(info);
}
public void MakeSense(ServiceData serviceData)
{
Console.WriteLine("Info=" + serviceData.Info + " Extra Data=" + serviceData.ExtensionData);
}
}
//Name = "GO"必须与服务端设置的值一样,这样做是为了能找到该服务
[ServiceContract(Name = "GO")]
public interface IClient
{
[OperationContract(Name = "DispInfo")]
void MakeSense(string info);
[OperationContract(Name = "DispEntity")]
void MakeSense(ClientData clientData);
}
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(ServiceImplementation),
new Uri("http://localhost:8080"));//Uri随便定义
host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Make");//Make随便定义
host.Open();
ChannelFactory<IClient> factory = new ChannelFactory<IClient>(new BasicHttpBinding(),
new EndpointAddress("http://localhost:8080/Make"));//将以上两个地址组合即可
IClient proxy = factory.CreateChannel();
proxy.MakeSense("Let's Go");//能正确调用
ClientData clientData = new ClientData();
clientData.Info = "I am Info";
clientData.extraInfo = "I am extra info";
proxy.MakeSense(clientData);//clientData传到WCF Service后值为空
((IChannel)proxy).Close();
host.Close();
Console.ReadKey();
}
}
}