WCF使用小例子

网友投稿 781 2022-11-28

WCF使用小例子

WCF使用小例子

using System.Runtime.Serialization; using System.ServiceModel; using MySpace; using System.ServiceModel.Channels; using System;namespace MySpace { [DataContract] public class Person { public Person() { } [DataMember(Name = "Name")] public string mName { get; set; } [DataMember(Name = "Age")] public int? mAge { get; set; } [DataMember(Name = "Single")] public bool? mIsSingle { get; set; } [System.Runtime.Serialization.OnDeserialized] void OnDeserialized(System.Runtime.Serialization.StreamingContext c) { mAge = (mAge == null ? 18 : mAge); } } [ServiceContract] public interface IService { [OperationContract] Person Method(Person dd); } public class Service : IService { public Person Method(Person dd) { return dd; } } class Program { static void Main(string[] args) { string Url = "​​ Binding binding = new BasicHttpBinding(); ServiceHost host = new ServiceHost(typeof(Service)); host.AddServiceEndpoint(typeof(IService), binding, Url); host.Open(); ChannelFactory fac = new ChannelFactory(binding); fac.Open(); IService proxy = fac.CreateChannel(new EndpointAddress(Url)); Person d = new Person(); d.mName = "BuzBuza"; Console.WriteLine("Data before calling service " + (d.mAge == null ? " null " : d.mAge.Value.ToString())); Console.WriteLine("Data before calling service " + (d.mIsSingle == null ? "null" : d.mIsSingle.Value.ToString())); d = proxy.Method(d); fac.Close(); host.Close(); Console.WriteLine("Data after calling service " + (d.mAge == null ? " null " : d.mAge.Value.ToString())); Console.WriteLine("Data after calling service " + (d.mIsSingle == null ? "null" : d.mIsSingle.Value.ToString())); Console.ReadLine(); } } }

龙腾一族至尊龙骑

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:键盘输入、鼠标输入、焦点处理
下一篇:浏览器原理 28 # HTTP/1:HTTP性能优化
相关文章

 发表评论

暂时没有评论,来抢沙发吧~