我想知道如何将DataContract添加到我的服务?我的意思是,我知道我必须创建一个类,将[DATACONTRACT]放在它的顶部,然后在每个成员的顶部添加[DATAMEMBER],然后如何将DataContract添加到服务中(我已经有一个[ServiceContract]和[OperationContract]在服务上运行)?WCF和[DATACONTRACT] + [DATAMEMBER]
我正在做一切programmaticaly在此刻(没有.config文件)。
一些代码片段展示我如何启动并添加我的OperationContract的:(我使用.NET 4.0)
服务端:
using (ServiceHost host = new ServiceHost(typeof(StringReverser), new Uri[]{ new Uri("net.tcp://localhost") }))
{
host.AddServiceEndpoint(typeof(IStringReverser), new NetTcpBinding(), "TCPReverse");
host.Open();
}
客户端:
Callbacks myCallbacks = new Callbacks();
DuplexChannelFactory<IStringReverser> TCPFactory =
new DuplexChannelFactory<IStringReverser>(
myCallbacks,
new NetTcpBinding());
TCPFactory.Credentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password");
IStringReverser TCPProxy = TCPFactory.CreateChannel();
Console.WriteLine("Client connected");
在此先感谢
好吧,如果我没有错,我不需要任何DataContract和DataMember,如果我使用简单的类型,我可以将它们作为参数或通过返回值传递,并且工作正常。但是,如果我想要使用复杂类型,则必须添加DataContract和DataMember才能够执行与简单类型相同的操作 感谢您回答如此之快! – John 2011-05-24 14:01:28