大佬教程收集整理的这篇文章主要介绍了客户端WCF DataContract具有来自服务的空/空值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <GetTimeResponse xmlns="http://tempuri.org/"> <GetTimeResult xmlns:a="http://schemas.datacontract.org/2004/07/TestService.DataObjects" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <a:theTime>2010-03-26T09:14:38.066372-06:00</a:theTime> </GetTimeResult> </GetTimeResponse> </s:Body> </s:Envelope>
所以,据我所知,服务器端没有任何错误.它正在接收请求并返回结果.
但是在我的silverlight客户端上,返回对象的所有成员都是null,空白或默认的vaule.您可以看到服务器返回当前日期和时间.但在silverlight中,我的对象的time属性设置为1/1/0001 12:00 AM(默认值).
在DataContracts与服务器和silverlight客户端之间不匹配的Sooo表示.这是服务器的DataContract
[DataContract] public class Time { [DataMember] public DateTime theTime { get; set; } }
难以置信的简单这是我的silverlight客户端上的datacontract.
[DataContract] public class Time { [DataMember] public DateTime theTime { get; set; } }
字面上唯一的区别是应用程序中的命名空间.但是,返回的值仍然为null,为空或.NET默认值.
感谢您的帮助!
UPDATE
这是我所有的服务都通过的ClientBase.我读了一个excellent article here来构造它.
public class ClientBase<T> where T :class { private T Channel { get; set; } private Type ContractType { get; set; } private ClientBase() { ContractType = typeof( T ); } public ClientBase(string endPointConfiguration) :this() { Channel = new ChannelFactory<T>( endPointConfiguration ).CreateChannel(); } public ClientBase( EndpointAddress address,Binding binding ):this() { Channel = new ChannelFactory<T>( binding,address ).CreateChannel(); } public void Begin(string methodName,object state,params object[] parameterArray) { Begin( methodName,null,state,parameterArray ); } public void Begin(string methodName,EventHandler<ClientEventArgs> callBack,params object[] parameterArray) { if(parameterArray != null) { Array.Resize(ref parameterArray,parameterArray.Length + 2); } else { parameterArray = new object[2]; } parameterArray[ parameterArray.Length - 1 ] = new ObjectClientState {CallBack = callBack,MethodName = methodName,UserState = state}; parameterArray[ parameterArray.Length - 2 ] = new AsyncCallback( OnCallBack ); ContractType.InvokeMember( "Begin" + methodName,System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public,Channel,parameterArray ); } private void OnCallBack(IAsyncResult result) { ObjectClientState state = result.AsyncState as ObjectClientState; if(state == null) return; Object obj = ContractType.InvokeMember( "End" + state.MethodName,System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public,new object[] {result} ); if(state.CallBack != null) { state.CallBack( this,new ClientEventArgs {Object = obj,UserState = state.UserState} ); } } public class ClientEventArgs : EventArgs { public object Object { get; set; } public object UserState { get; set; } public T LoadResult<T>() { if( Object is T ) return ( T ) Object; return default( T ); } } private class ObjectClientState { public EventHandler<ClientEventArgs> CallBack { get; set; } public string MethodName { get; set; } public object UserState { get; set; } } }
这是我的界面
[ServiceContract] public interface ITestService { [OperationContract( AsyncPattern = true )] IAsyncResult BeginGetTime( AsyncCallback callback,object state ); Time EndGetTime( IAsyncResult result ); }
现在我有我的服务类,通过我的BaseService类使用这个接口进行调用.
public class TestSiteService : ClientBase<ITestService> { public TestSiteService (string endPointConfiguration):base(endPointConfiguration) { } public TestSiteService ( EndpointAddress address,Binding binding ) : base( address,binding ) { } public void GetTime( EventHandler<ClientEventArgs> callBack ) { Begin( "GetTime",callBack,null ); } }
TestSiteService client = new TestSiteService ( new EndpointAddress( "http://localhost:3483/wcf/Service.svc" ),new BasicHttpBinding() ); client.GetTime( delegate( object res,ClientBase<ITestService>.ClientEventArgs e ) { Dispatcher.BeginInvoke( () => lblDisplay.Text = "Welcome " + e.LoadResult<Time>().theTime ); } );
我希望没有人丢失所有这些代码我发布:P
http://schemas.datacontract.org/2004/07/TestService.DataObjects
为了使合同被视为相等,您必须将DataContract上的Namespace属性设置为两边的相同值.这可能看起来像这样一些东西:
[DataContract(Namespace="urn:my-test-namespace")]
以上是大佬教程为你收集整理的客户端WCF DataContract具有来自服务的空/空值全部内容,希望文章能够帮你解决客户端WCF DataContract具有来自服务的空/空值所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。