服务器未提供有意义的回复;这可能是由协定不匹配、会话过早关闭或内部服务器错误引起的。
这是什么意思?代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;namespace Common //公用包
{
[Serializable]
public enum UserAction:byte
{
Gone =1,
Entered =2,
FinishedAnswer =3
} [ServiceContract(CallbackContract = typeof(IServiceCallBack))]
public interface IService
{
[OperationContract]
void Unregister(Guid id); [OperationContract]
Guid Register(String name); [OperationContract]
void Say(Guid from,Guid target, String message); [OperationContract]
UserList GetNewestUserList();
} public interface IServiceCallBack
{
[OperationContract]
void ReceiveMessage(User user, String message); [OperationContract]
void UserAction(User user, UserAction action);
}
[DataContract]
public class User:IEquatable<Guid>
{
[DataMember]
String username; [DataMember]
Guid userId = Guid.NewGuid(); public User(String username)
{
this.username = username; }
public String UserName
{
get { return username; }
}
public Guid UserId
{
get { return userId; }
} public override string ToString()
{
return UserName;
} #region IEquatable<Guid> 成员 public bool Equals(Guid other)
{
return UserId == other;
} #endregion
}
[DataContract]
public class UserList
{
User[] users= new User[0]; [DataMember]
public User[] Users
{
get { return users; }
set { users = value; }
}
public void Add(User u)
{
Array.Resize<User>(ref users, users.Length+1);
users[users.Length - 1] = u;
}
public void Remove(User u)
{
int index=0;
if ((index = Array.IndexOf(users, u)) > -1)
{
while (index++ < users.Length)
users[index - 1] = users[index]; Array.Resize<User>(ref users, index - 1);
}
}
public Int32 Count
{
get { return users.Length; }
}
public User this[int index]
{
get
{
if (index > users.Length - 1)
throw new IndexOutOfRangeException(); return users[index];
}
set
{
if (index > users.Length - 1)
throw new IndexOutOfRangeException(); users[index] = value;
}
}
public User this[Guid index]
{
get
{
foreach (User u in users)
if (u.Equals(index))
return u; return null;
}
}
public Int32 IndexOf(User u)
{
return Array.IndexOf<User>(users, u);
}
}
}
namespace Srv //服务器
{
/// <summary>
/// This is the implementation of IService. We have to keep this in the server.
/// So the server can use the stub in BaseService to call client.
/// </summary>
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,IncludeExceptionDetailInFaults=true)]
public class BaseService : IService
{
#region IService Member
//public IServiceCallBack callback;
List<UserInfo> stubs = new List<UserInfo>(); public BaseService()
{
//this.s = c;
} private UserInfo FindUser(Guid g)
{
var foundStubs = from s in stubs where s.UserId == g select s; List<UserInfo> info = foundStubs.ToList<UserInfo>(); Debug.Assert(info.Count <= 1); if (info.Count == 0)
return null;
else
return info[0];
} public void Unregister(Guid id)
{
stubs.Remove(FindUser(id)); foreach (UserInfo info in stubs)
info.Callback.UserAction(info, UserAction.Gone);
} public Guid Register(string name)
{
IServiceCallBack callback = OperationContext.Current.GetCallbackChannel<IServiceCallBack>(); UserInfo newuser = new UserInfo(name,callback);
//Client registed!!
if (callback != null)
this.stubs.Add(newuser); foreach (UserInfo info in stubs)
{
if (info == newuser)
continue; info.Callback.UserAction(newuser, UserAction.Entered);
} return newuser.UserId; } public void Say(Guid from,Guid target, String message)
{ if (target != Guid.Empty)
{
UserInfo info = FindUser(target); if(info!=null)
info.Callback.ReceiveMessage(FindUser(from), message);
}
else
{
foreach (UserInfo info in stubs)
info.Callback.ReceiveMessage(FindUser(from), message);
}
} public UserList GetNewestUserList()
{
UserList list = new UserList();
foreach (UserInfo user in stubs)
list.Add(user);
return list;
} #endregion } ....
[ServiceKnownType(typeof(User))]
public class UserInfo : User
{
public UserInfo(String name, IServiceCallBack callback)
: base(name)
{
this.callback = callback;
} IServiceCallBack callback;
internal IServiceCallBack Callback
{
get { return callback; }
} } ....
}
客户端调用顺序: Register() --> GetNewestUserList()
客户端在调用Register时候没问题
调用GetNewestUserList时候就会报错:
System.ServiceModel.CommunicationException: 服务器未提供有意义的回复;这可能是由协定不匹配、会话过早关闭或内部服务器错误引起的。怎么回事? 请各位大师不吝赐教! 谢谢
这是什么意思?代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;namespace Common //公用包
{
[Serializable]
public enum UserAction:byte
{
Gone =1,
Entered =2,
FinishedAnswer =3
} [ServiceContract(CallbackContract = typeof(IServiceCallBack))]
public interface IService
{
[OperationContract]
void Unregister(Guid id); [OperationContract]
Guid Register(String name); [OperationContract]
void Say(Guid from,Guid target, String message); [OperationContract]
UserList GetNewestUserList();
} public interface IServiceCallBack
{
[OperationContract]
void ReceiveMessage(User user, String message); [OperationContract]
void UserAction(User user, UserAction action);
}
[DataContract]
public class User:IEquatable<Guid>
{
[DataMember]
String username; [DataMember]
Guid userId = Guid.NewGuid(); public User(String username)
{
this.username = username; }
public String UserName
{
get { return username; }
}
public Guid UserId
{
get { return userId; }
} public override string ToString()
{
return UserName;
} #region IEquatable<Guid> 成员 public bool Equals(Guid other)
{
return UserId == other;
} #endregion
}
[DataContract]
public class UserList
{
User[] users= new User[0]; [DataMember]
public User[] Users
{
get { return users; }
set { users = value; }
}
public void Add(User u)
{
Array.Resize<User>(ref users, users.Length+1);
users[users.Length - 1] = u;
}
public void Remove(User u)
{
int index=0;
if ((index = Array.IndexOf(users, u)) > -1)
{
while (index++ < users.Length)
users[index - 1] = users[index]; Array.Resize<User>(ref users, index - 1);
}
}
public Int32 Count
{
get { return users.Length; }
}
public User this[int index]
{
get
{
if (index > users.Length - 1)
throw new IndexOutOfRangeException(); return users[index];
}
set
{
if (index > users.Length - 1)
throw new IndexOutOfRangeException(); users[index] = value;
}
}
public User this[Guid index]
{
get
{
foreach (User u in users)
if (u.Equals(index))
return u; return null;
}
}
public Int32 IndexOf(User u)
{
return Array.IndexOf<User>(users, u);
}
}
}
namespace Srv //服务器
{
/// <summary>
/// This is the implementation of IService. We have to keep this in the server.
/// So the server can use the stub in BaseService to call client.
/// </summary>
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,IncludeExceptionDetailInFaults=true)]
public class BaseService : IService
{
#region IService Member
//public IServiceCallBack callback;
List<UserInfo> stubs = new List<UserInfo>(); public BaseService()
{
//this.s = c;
} private UserInfo FindUser(Guid g)
{
var foundStubs = from s in stubs where s.UserId == g select s; List<UserInfo> info = foundStubs.ToList<UserInfo>(); Debug.Assert(info.Count <= 1); if (info.Count == 0)
return null;
else
return info[0];
} public void Unregister(Guid id)
{
stubs.Remove(FindUser(id)); foreach (UserInfo info in stubs)
info.Callback.UserAction(info, UserAction.Gone);
} public Guid Register(string name)
{
IServiceCallBack callback = OperationContext.Current.GetCallbackChannel<IServiceCallBack>(); UserInfo newuser = new UserInfo(name,callback);
//Client registed!!
if (callback != null)
this.stubs.Add(newuser); foreach (UserInfo info in stubs)
{
if (info == newuser)
continue; info.Callback.UserAction(newuser, UserAction.Entered);
} return newuser.UserId; } public void Say(Guid from,Guid target, String message)
{ if (target != Guid.Empty)
{
UserInfo info = FindUser(target); if(info!=null)
info.Callback.ReceiveMessage(FindUser(from), message);
}
else
{
foreach (UserInfo info in stubs)
info.Callback.ReceiveMessage(FindUser(from), message);
}
} public UserList GetNewestUserList()
{
UserList list = new UserList();
foreach (UserInfo user in stubs)
list.Add(user);
return list;
} #endregion } ....
[ServiceKnownType(typeof(User))]
public class UserInfo : User
{
public UserInfo(String name, IServiceCallBack callback)
: base(name)
{
this.callback = callback;
} IServiceCallBack callback;
internal IServiceCallBack Callback
{
get { return callback; }
} } ....
}
客户端调用顺序: Register() --> GetNewestUserList()
客户端在调用Register时候没问题
调用GetNewestUserList时候就会报错:
System.ServiceModel.CommunicationException: 服务器未提供有意义的回复;这可能是由协定不匹配、会话过早关闭或内部服务器错误引起的。怎么回事? 请各位大师不吝赐教! 谢谢
解决方案 »
- 新人请教个问题 datatable select
- 大家给推荐一本.net 和C#关于各种控件的属性、事件的书吧
- 请问下csgl中如何显示文字?是否有类似C++中glPrint()的函数
- Access数据库 表A中的数据导入到表B中
- datagrid分页,给当前页码加下滑线,高分求教,在线等,解决就结贴!顶者有分!!
- 关于.Net2003 C# 中MacAddress/IpAddress 控件的问题。
- 一个非常简单的问题?
- 请高手帮我看看,这个关于SmartClient的问题怎么解决?
- 难,难,难,如何实现批量改名!!!
- SqlDataReader result = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
- 有个C#写的网络程序,在一般机上cpu占用只有一两个点.很少超过10%,但在极个别客户机上cpu占用100%?多线程的GetResponse()去按顺序排队过
- 菜鸟问题!关于ContextMenuStrip的.
[ServiceContract]
public interface IServiceCallBack
{
[OperationContract]
void ReceiveMessage(User user, String message); [OperationContract]
void UserAction(User user, UserAction action);
}
还有就是ServiceBehavior配置问题
参考
WCF中不允许在协议中定义一个类型而传输其子类型. 除非在该类型上定义了[KnownType(typeof(子类型))]这个错误犯得够幼稚的