悬赏园豆:10
[已解决问题]
解决于 2010-08-23 21:51
<pre> <system.serviceModel><br /> <services><br /> <service behaviorConfiguration="Service2Behavior" name="SevenPrincess.Server.WCF.Service.Service.IM.IMConnect"><br /> <endpoint address="IMConnect" binding="netTcpBinding"<br /> bindingConfiguration="netTcpConnectConfigration" contract="SevenPrincess.Server.WCF.Service.Interface.IM.IIMConnect"/><br /> <endpoint address="IMConnect/mex" binding="mexTcpBinding" contract="IMetadataExchange"/><br /> <host><br /> <baseAddresses><br /> <add baseAddress="net.tcp://localhost:601/IM/"/><br /> </baseAddresses><br /> </host><br /> </service><br /> </services><br /> <behaviors><br /> <serviceBehaviors><br /> <behavior name="Service2Behavior"><br /> <serviceMetadata httpGetEnabled="false"/><br /> <serviceDebug includeExceptionDetailInFaults="False"/><br /> <serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000"/><br /> <serviceCredentials><br /> <serviceCertificate storeName="My" findValue="WCFService" storeLocation="LocalMachine" x509FindType="FindBySubjectName"/><br /> </serviceCredentials><br /> </behavior><br /> </serviceBehaviors><br /> </behaviors><br /> <bindings><br /> <netTcpBinding><br /> <binding name="netTcpConnectConfigration" maxConnections="5000" receiveTimeout="00:10:00" sendTimeout="00:10:00" portSharingEnabled="true"><br /> <reliableSession inactivityTimeout="01:10:00" enabled="true"/><br /> <security mode="Transport"><br /> <transport clientCredentialType="None"/><br /> </security><br /> </binding><br /> </netTcpBinding><br /> </bindings><br /> </system.serviceModel></pre>
<p>先来贴上配置文件内容。</p>
<p>2张截图。</p>
<p> </p>
<p><img src="http://pic002.cnblogs.com/img/blood/201008/2010082314584015.jpg" alt="" /> <br /> <br /> <br /> <img src="http://pic002.cnblogs.com/img/blood/201008/2010082314590536.jpg" alt="" /> <br /> <br /> 我并没有创建host服务,而是直接使用wcf项目自带的test host。项目中使用发现服务来自动创建客户端代码。</p>
<p> </p>
<p>但是现在遇到的问题是,只要在配置中添加了mex,就会出错了。出错内容如下:</p>
<p> </p>
<p>System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:10001. Make sure that you are not trying to use this endpoint multiple times in your application and that there are no other applications listening on this endpoint. ---> System.Net.Sockets.SocketException: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。<br /> at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)<br /> at System.Net.Sockets.Socket.Bind(EndPoint localEP)<br /> at System.ServiceModel.Channels.SocketConnectionListener.Listen()<br /> --- End of inner exception stack trace ---<br /> at System.ServiceModel.Channels.SocketConnectionListener.Listen()<br /> at System.ServiceModel.Channels.ConnectionAcceptor.StartAccepting()<br /> at System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()<br /> at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)<br /> at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)<br /> at System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan timeout)<br /> at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)<br /> at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)<br /> at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)<br /> at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)<br /> at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)<br /> at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)<br /> System.Net.Sockets.SocketException (0x80004005): 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。<br /> at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)<br /> at System.Net.Sockets.Socket.Bind(EndPoint localEP)<br /> at System.ServiceModel.Channels.SocketConnectionListener.Listen()</p>
<p>如果不添加mex,则服务能够启动。但是没法自动生成代码。</p>
<p> </p>
<p>我服务器端的代码如下:</p>
<p> </p>
<pre>using System;<br />using System.Collections.Generic;<br />using System.Diagnostics;<br />using System.ServiceModel;<br />using Microsoft.Practices.EnterpriseLibrary.Logging;<br />using SevenPrincess.Server.WCF.Service.Interface.IM;<br />using IMBLL = SevenPrincess.Server.Database.BLL.IM;<br />using IMModel = SevenPrincess.Server.WCF.Entity.IM;<br /><br />namespace SevenPrincess.Server.WCF.Service.Service.IM<br />{<br /> /// <summary><br /> /// IM账号登录信息WCF方法<br /> /// </summary><br /> [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]<br /> public class IMConnect : IIMConnect<br /> {<br /> private static Dictionary<IIMConnectCallback, object[]> dic_Client;<br /><br /> static IMConnect()<br /> {<br /> dic_Client = new Dictionary<IIMConnectCallback, object[]>();<br /> }<br /><br /> public IMConnect()<br /> {<br /> OperationContext.Current.Channel.Faulted += ClientClosed;<br /> }<br /><br /> void ClientClosed(object sender, EventArgs e)<br /> {<br /> Disconnect();<br /> }<br /><br /> object[] GetClient(IIMConnectCallback p_CallbackEventHandler)<br /> {<br /> if (dic_Client.ContainsKey(p_CallbackEventHandler))<br /> {<br /> return dic_Client[p_CallbackEventHandler];<br /> }<br /> else<br /> {<br /> return null;<br /> }<br /> }<br /><br /> /// <summary><br /> /// 添加登录记录<br /> /// </summary><br /> /// <param name="p_IMConnect">IM登录信息</param><br /> /// <param name="p_EmployeeNickname">员工昵称</param><br /> /// <param name="p_EmployeeName">员工姓名</param><br /> /// <param name="p_Token">登录验证票据</param><br /> public void Add(IMModel.IMConnect p_IMConnect, string p_EmployeeNickname, string p_EmployeeName, string p_Token)<br /> {<br /> IIMConnectCallback callbackEventHandler = OperationContext.Current.GetCallbackChannel<IIMConnectCallback>();<br /><br /> try<br /> {<br /> p_IMConnect.ConnectTime = DateTime.Now;<br /> p_IMConnect.LastActiveTime = p_IMConnect.ConnectTime;<br /> IMBLL.IMConnect bll_IMConnect = new IMBLL.IMConnect();<br /> int[] int_IDs = bll_IMConnect.Add(p_IMConnect);<br /><br /> if (int_IDs[1] > 0)<br /> {<br /> p_IMConnect.ID = int_IDs[0];<br /><br /> object[] obj_Client = new object[3];<br /> obj_Client[0] = p_IMConnect;<br /> obj_Client[1] = p_EmployeeNickname;<br /> obj_Client[2] = p_EmployeeName;<br /><br /> dic_Client.Add(callbackEventHandler, obj_Client);<br /><br /> callbackEventHandler.ResponseAdd(int_IDs[0], p_IMConnect.ConnectTime);<br /> }<br /> else<br /> {<br /> callbackEventHandler.ResponseAdd(int_IDs[0], p_IMConnect.ConnectTime);<br /> }<br /> }<br /> catch (Exception e)<br /> {<br /> LogEntry log = new LogEntry<br /> {<br /> EventId = 1,<br /> Priority = 1,<br /> Severity = TraceEventType.Error,<br /> Title = "添加登录记录",<br /> TimeStamp = DateTime.Now,<br /> Message = e.Message<br /> };<br /><br /> ICollection<string> coll = new List<string>();<br /> coll.Add("IM.IMConnect");<br /> log.Categories = coll;<br /><br /> Dictionary<string, object> dic = new Dictionary<string, object>();<br /> dic.Add("EmployeeID", p_IMConnect.EmployeeID);<br /> dic.Add("IMAccountID", p_IMConnect.IMAccountID);<br /> dic.Add("IMID", p_IMConnect.IMID);<br /> dic.Add("EmployeeNickname", p_EmployeeNickname);<br /> dic.Add("EmployeeName", p_EmployeeName);<br /><br /> log.ExtendedProperties = dic;<br /><br /> Logger.Write(log);<br /><br /> callbackEventHandler.ResponseAdd(-1, p_IMConnect.ConnectTime);<br /> }<br /> }<br /><br /> /// <summary><br /> /// 更新最后活动时间<br /> /// </summary><br /> /// <param name="p_ID">ID</param><br /> /// <param name="p_Token">登录验证票据</param><br /> /// <returns>更新状态</returns><br /> public int? UpdateLastActiveTime(int p_ID, string p_Token)<br /> {<br /> try<br /> {<br /> IMBLL.IMConnect bll_IMConnect = new IMBLL.IMConnect();<br /> return bll_IMConnect.UpdateLastActiveTime(p_ID, DateTime.Now);<br /> }<br /> catch (Exception e)<br /> {<br /> LogEntry log = new LogEntry<br /> {<br /> EventId = 2,<br /> Priority = 1,<br /> Severity = TraceEventType.Error,<br /> Title = "更新最后活动时间",<br /> TimeStamp = DateTime.Now,<br /> Message = e.Message<br /> };<br /><br /> ICollection<string> coll = new List<string>();<br /> coll.Add("IM.IMConnect");<br /> log.Categories = coll;<br /><br /> Dictionary<string, object> dic = new Dictionary<string, object>();<br /> dic.Add("p_ID", p_ID);<br /><br /> log.ExtendedProperties = dic;<br /><br /> Logger.Write(log);<br /><br /> return null;<br /> }<br /> }<br /><br /> /// <summary><br /> /// 断开连接<br /> /// </summary><br /> public void Disconnect()<br /> {<br /> IIMConnectCallback callbackEventHandler = OperationContext.Current.GetCallbackChannel<IIMConnectCallback>();<br /> object[] client = GetClient(callbackEventHandler);<br /><br /> if (client != null)<br /> {<br /> IMModel.IMConnect mol_IMConnect = client[0] as IMModel.IMConnect;<br /> string str_EmployeeNickname = client[1] as string;<br /> string str_EmployeeName = client[2] as string;<br /><br /> try<br /> {<br /> IMBLL.IMConnect bll_IMConnect = new IMBLL.IMConnect();<br /> bll_IMConnect.UpdateDisconnectTime(mol_IMConnect.ID, DateTime.Now, str_EmployeeNickname, str_EmployeeName);<br /><br /> dic_Client.Remove(callbackEventHandler);<br /> }<br /> catch (Exception e)<br /> {<br /> LogEntry log = new LogEntry<br /> {<br /> EventId = 3,<br /> Priority = 1,<br /> Severity = TraceEventType.Error,<br /> Title = "断开连接",<br /> TimeStamp = DateTime.Now,<br /> Message = e.Message<br /> };<br /><br /> ICollection<string> coll = new List<string>();<br /> coll.Add("IM.IMConnect");<br /> log.Categories = coll;<br /><br /> Dictionary<string, object> dic = new Dictionary<string, object>();<br /> dic.Add("ID", mol_IMConnect.ID);<br /> dic.Add("DisconnectTime", DateTime.Now);<br /> dic.Add("EmployeeNickname", str_EmployeeNickname);<br /> dic.Add("EmployeeName", str_EmployeeName);<br /><br /> log.ExtendedProperties = dic;<br /><br /> Logger.Write(log);<br /> }<br /> }<br /> }<br /><br /> /// <summary><br /> /// 取得IM登录信息记录列表<br /> /// <param name="p_EmployeeID">员工ID</param><br /> /// <param name="p_BeginTime">开始时间</param><br /> /// <param name="p_EndTime">结束时间</param><br /> /// <param name="p_Token">登录验证票据</param><br /> /// <returns>IM登录信息记录列表</returns><br /> /// </summary><br /> public List<IMModel.IMConnect> GetList(int p_EmployeeID, DateTime p_BeginTime, DateTime p_EndTime, string p_Token)<br /> {<br /> try<br /> {<br /> IMBLL.IMConnect bll_IMConnect = new IMBLL.IMConnect();<br /> return bll_IMConnect.GetList(p_EmployeeID, p_BeginTime, p_EndTime);<br /> }<br /> catch (Exception e)<br /> {<br /> LogEntry log = new LogEntry<br /> {<br /> EventId = 4,<br /> Priority = 1,<br /> Severity = TraceEventType.Error,<br /> Title = "取得IM登录信息记录列表",<br /> TimeStamp = DateTime.Now,<br /> Message = e.Message<br /> };<br /><br /> ICollection<string> coll = new List<string>();<br /> coll.Add("IM.IMConnect");<br /> log.Categories = coll;<br /><br /> Dictionary<string, object> dic = new Dictionary<string, object>();<br /> dic.Add("p_EmployeeID", p_EmployeeID);<br /> dic.Add("p_BeginTime", p_BeginTime);<br /> dic.Add("p_EndTime", p_EndTime);<br /><br /> log.ExtendedProperties = dic;<br /><br /> Logger.Write(log);<br /><br /> return null;<br /> }<br /> }<br /> }<br />}<br /></pre>
<pre>using System;<br />using System.Collections.Generic;<br />using System.ServiceModel;<br />using IMModel = SevenPrincess.Server.WCF.Entity.IM;<br /><br />namespace SevenPrincess.Server.WCF.Service.Interface.IM<br />{<br /> /// <summary><br /> /// IM账号登录信息WCF接口<br /> /// </summary><br /> [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IIMConnectCallback))]<br /> public interface IIMConnect<br /> {<br /> /// <summary><br /> /// 添加登录记录<br /> /// </summary><br /> /// <param name="p_IMConnect">IM登录信息</param><br /> /// <param name="p_EmployeeNickname">员工昵称</param><br /> /// <param name="p_EmployeeName">员工姓名</param><br /> /// <param name="p_Token">登录验证票据</param><br /> [OperationContract(IsOneWay = true)]<br /> void Add(IMModel.IMConnect p_IMConnect, string p_EmployeeNickname, string p_EmployeeName, string p_Token);<br /><br /> /// <summary><br /> /// 断开连接<br /> /// </summary><br /> /// <param name="p_ID">ID</param><br /> /// <param name="p_DisconnectTime">离线时间</param><br /> /// <param name="p_EmployeeNickname">员工昵称</param><br /> /// <param name="p_EmployeeName">员工姓名</param><br /> /// <param name="p_Token">登录验证票据</param><br /> /// <returns>更新状态</returns><br /> [OperationContract(IsOneWay = true)]<br /> void Disconnect();<br /><br /> /// <summary><br /> /// 更新最后活动时间<br /> /// </summary><br /> /// <param name="p_ID">ID</param><br /> /// <param name="p_Token">登录验证票据</param><br /> /// <returns>更新状态</returns><br /> [OperationContract]<br /> int? UpdateLastActiveTime(int p_ID, string p_Token);<br /><br /> /// <summary><br /> /// 取得IM登录信息记录列表<br /> /// <param name="p_EmployeeID">员工ID</param><br /> /// <param name="p_BeginTime">开始时间</param><br /> /// <param name="p_EndTime">结束时间</param><br /> /// <param name="p_Token">登录验证票据</param><br /> /// <returns>IM登录信息记录列表</returns><br /> /// </summary><br /> [OperationContract]<br /> List<IMModel.IMConnect> GetList(int p_EmployeeID, DateTime p_BeginTime, DateTime p_EndTime, string p_Token);<br /> }<br />}<br /></pre>
<p> </p>
<p>请遇到问题的人不吝赐教,帮忙解决一下问题。谢谢。</p>