WCF 和 [数据合同]+[数据成员]
我想知道如何将 DataContract 添加到我的服务中?我的意思是,我知道我必须创建一个类,将 [DATACONTRACT] 放在其顶部,然后在每个成员顶部添加 [DATAMEMBER],但是如何将 DataContract 添加到服务中(我已经有一个 [ServiceContract]和 [OperationContract] 在服务上运行)?
我目前正在以编程方式执行所有操作(没有 .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");
提前致谢
I was wondering how to add the DataContract to my service? I mean, I know I have to create a class, put [DATACONTRACT] on top of it and then add [DATAMEMBER] on top of each members, but then how to add the DataContract to the service (I already have a [ServiceContract] and [OperationContract] running on a service)??
I am doing everything programmaticaly at the moment (no .config file).
some piece of code showing how I launch and add my OperationContract: (I'm using .NET 4.0)
Service side:
using (ServiceHost host = new ServiceHost(typeof(StringReverser), new Uri[]{ new Uri("net.tcp://localhost") }))
{
host.AddServiceEndpoint(typeof(IStringReverser), new NetTcpBinding(), "TCPReverse");
host.Open();
}
Client side:
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");
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要在任何地方“添加”数据合同。您只需将其用作服务合约或回调合约中任何操作的参数或返回值即可。数据契约定义了作为服务请求或响应的一部分传输的数据。如果服务的任何操作都没有使用数据契约,那么它显然与服务没有关系(如果是元数据,则不包括在内)。
唯一的例外是一些具有已知类型的高级场景,但本问题中的情况可能并非如此。
You don't need to "add" data contract anywhere. You just have to use it as parameter or return value of any operation in your service contract or callback contract. Data contract defines data transferred as part of service request or response. If the data contract is not used by any operation of the service it has obviously no relation to the service (in case of metadata it is not included).
The only exception are some advanced scenarios with known types but that will probably not be the case in this question.
数据契约是传递给您的服务操作的内容。通常,如果您有复杂类型作为参数,您将使用 DataContract 属性来装饰它们。
如果您只将原始类型作为参数处理,则无需使用 DataContract 进行修饰。
DataContracts are what are passed to the operations of your service. Generally if you have complex types as parameters you will decorate them with a DataContract attribute.
If your only dealing with primitive types as parameters there is nothing to decorate with DataContract.