首页 新闻 会员 周边 捐助

求助一下。TCP双工WCF。添加了mex之后,就报错,无法启动服务。

0
悬赏园豆:10 [已解决问题] 解决于 2010-08-23 21:51
<pre> &lt;system.serviceModel&gt;<br /> &lt;services&gt;<br /> &lt;service behaviorConfiguration="Service2Behavior" name="SevenPrincess.Server.WCF.Service.Service.IM.IMConnect"&gt;<br /> &lt;endpoint address="IMConnect" binding="netTcpBinding"<br /> bindingConfiguration="netTcpConnectConfigration" contract="SevenPrincess.Server.WCF.Service.Interface.IM.IIMConnect"/&gt;<br /> &lt;endpoint address="IMConnect/mex" binding="mexTcpBinding" contract="IMetadataExchange"/&gt;<br /> &lt;host&gt;<br /> &lt;baseAddresses&gt;<br /> &lt;add baseAddress="net.tcp://localhost:601/IM/"/&gt;<br /> &lt;/baseAddresses&gt;<br /> &lt;/host&gt;<br /> &lt;/service&gt;<br /> &lt;/services&gt;<br /> &lt;behaviors&gt;<br /> &lt;serviceBehaviors&gt;<br /> &lt;behavior name="Service2Behavior"&gt;<br /> &lt;serviceMetadata httpGetEnabled="false"/&gt;<br /> &lt;serviceDebug includeExceptionDetailInFaults="False"/&gt;<br /> &lt;serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000"/&gt;<br /> &lt;serviceCredentials&gt;<br /> &lt;serviceCertificate storeName="My" findValue="WCFService" storeLocation="LocalMachine" x509FindType="FindBySubjectName"/&gt;<br /> &lt;/serviceCredentials&gt;<br /> &lt;/behavior&gt;<br /> &lt;/serviceBehaviors&gt;<br /> &lt;/behaviors&gt;<br /> &lt;bindings&gt;<br /> &lt;netTcpBinding&gt;<br /> &lt;binding name="netTcpConnectConfigration" maxConnections="5000" receiveTimeout="00:10:00" sendTimeout="00:10:00" portSharingEnabled="true"&gt;<br /> &lt;reliableSession inactivityTimeout="01:10:00" enabled="true"/&gt;<br /> &lt;security mode="Transport"&gt;<br /> &lt;transport clientCredentialType="None"/&gt;<br /> &lt;/security&gt;<br /> &lt;/binding&gt;<br /> &lt;/netTcpBinding&gt;<br /> &lt;/bindings&gt;<br /> &lt;/system.serviceModel&gt;</pre> <p>先来贴上配置文件内容。</p> <p>2张截图。</p> <p>&nbsp;</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>&nbsp;</p> <p>但是现在遇到的问题是,只要在配置中添加了mex,就会出错了。出错内容如下:</p> <p>&nbsp;</p> <p>System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:10001.&nbsp; 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. ---&gt; System.Net.Sockets.SocketException: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。<br /> &nbsp;&nbsp; at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)<br /> &nbsp;&nbsp; at System.Net.Sockets.Socket.Bind(EndPoint localEP)<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.SocketConnectionListener.Listen()<br /> &nbsp;&nbsp; --- End of inner exception stack trace ---<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.SocketConnectionListener.Listen()<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.ConnectionAcceptor.StartAccepting()<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan timeout)<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)<br /> &nbsp;&nbsp; at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)<br /> &nbsp;&nbsp; at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)<br /> &nbsp;&nbsp; at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)<br /> System.Net.Sockets.SocketException (0x80004005): 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。<br /> &nbsp;&nbsp; at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)<br /> &nbsp;&nbsp; at System.Net.Sockets.Socket.Bind(EndPoint localEP)<br /> &nbsp;&nbsp; at System.ServiceModel.Channels.SocketConnectionListener.Listen()</p> <p>如果不添加mex,则服务能够启动。但是没法自动生成代码。</p> <p>&nbsp;</p> <p>我服务器端的代码如下:</p> <p>&nbsp;</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 /> /// &lt;summary&gt;<br /> /// IM账号登录信息WCF方法<br /> /// &lt;/summary&gt;<br /> [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]<br /> public class IMConnect : IIMConnect<br /> {<br /> private static Dictionary&lt;IIMConnectCallback, object[]&gt; dic_Client;<br /><br /> static IMConnect()<br /> {<br /> dic_Client = new Dictionary&lt;IIMConnectCallback, object[]&gt;();<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 /> /// &lt;summary&gt;<br /> /// 添加登录记录<br /> /// &lt;/summary&gt;<br /> /// &lt;param name="p_IMConnect"&gt;IM登录信息&lt;/param&gt;<br /> /// &lt;param name="p_EmployeeNickname"&gt;员工昵称&lt;/param&gt;<br /> /// &lt;param name="p_EmployeeName"&gt;员工姓名&lt;/param&gt;<br /> /// &lt;param name="p_Token"&gt;登录验证票据&lt;/param&gt;<br /> public void Add(IMModel.IMConnect p_IMConnect, string p_EmployeeNickname, string p_EmployeeName, string p_Token)<br /> {<br /> IIMConnectCallback callbackEventHandler = OperationContext.Current.GetCallbackChannel&lt;IIMConnectCallback&gt;();<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] &gt; 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&lt;string&gt; coll = new List&lt;string&gt;();<br /> coll.Add("IM.IMConnect");<br /> log.Categories = coll;<br /><br /> Dictionary&lt;string, object&gt; dic = new Dictionary&lt;string, object&gt;();<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 /> /// &lt;summary&gt;<br /> /// 更新最后活动时间<br /> /// &lt;/summary&gt;<br /> /// &lt;param name="p_ID"&gt;ID&lt;/param&gt;<br /> /// &lt;param name="p_Token"&gt;登录验证票据&lt;/param&gt;<br /> /// &lt;returns&gt;更新状态&lt;/returns&gt;<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&lt;string&gt; coll = new List&lt;string&gt;();<br /> coll.Add("IM.IMConnect");<br /> log.Categories = coll;<br /><br /> Dictionary&lt;string, object&gt; dic = new Dictionary&lt;string, object&gt;();<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 /> /// &lt;summary&gt;<br /> /// 断开连接<br /> /// &lt;/summary&gt;<br /> public void Disconnect()<br /> {<br /> IIMConnectCallback callbackEventHandler = OperationContext.Current.GetCallbackChannel&lt;IIMConnectCallback&gt;();<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&lt;string&gt; coll = new List&lt;string&gt;();<br /> coll.Add("IM.IMConnect");<br /> log.Categories = coll;<br /><br /> Dictionary&lt;string, object&gt; dic = new Dictionary&lt;string, object&gt;();<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 /> /// &lt;summary&gt;<br /> /// 取得IM登录信息记录列表<br /> /// &lt;param name="p_EmployeeID"&gt;员工ID&lt;/param&gt;<br /> /// &lt;param name="p_BeginTime"&gt;开始时间&lt;/param&gt;<br /> /// &lt;param name="p_EndTime"&gt;结束时间&lt;/param&gt;<br /> /// &lt;param name="p_Token"&gt;登录验证票据&lt;/param&gt;<br /> /// &lt;returns&gt;IM登录信息记录列表&lt;/returns&gt;<br /> /// &lt;/summary&gt;<br /> public List&lt;IMModel.IMConnect&gt; 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&lt;string&gt; coll = new List&lt;string&gt;();<br /> coll.Add("IM.IMConnect");<br /> log.Categories = coll;<br /><br /> Dictionary&lt;string, object&gt; dic = new Dictionary&lt;string, object&gt;();<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 /> /// &lt;summary&gt;<br /> /// IM账号登录信息WCF接口<br /> /// &lt;/summary&gt;<br /> [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IIMConnectCallback))]<br /> public interface IIMConnect<br /> {<br /> /// &lt;summary&gt;<br /> /// 添加登录记录<br /> /// &lt;/summary&gt;<br /> /// &lt;param name="p_IMConnect"&gt;IM登录信息&lt;/param&gt;<br /> /// &lt;param name="p_EmployeeNickname"&gt;员工昵称&lt;/param&gt;<br /> /// &lt;param name="p_EmployeeName"&gt;员工姓名&lt;/param&gt;<br /> /// &lt;param name="p_Token"&gt;登录验证票据&lt;/param&gt;<br /> [OperationContract(IsOneWay = true)]<br /> void Add(IMModel.IMConnect p_IMConnect, string p_EmployeeNickname, string p_EmployeeName, string p_Token);<br /><br /> /// &lt;summary&gt;<br /> /// 断开连接<br /> /// &lt;/summary&gt;<br /> /// &lt;param name="p_ID"&gt;ID&lt;/param&gt;<br /> /// &lt;param name="p_DisconnectTime"&gt;离线时间&lt;/param&gt;<br /> /// &lt;param name="p_EmployeeNickname"&gt;员工昵称&lt;/param&gt;<br /> /// &lt;param name="p_EmployeeName"&gt;员工姓名&lt;/param&gt;<br /> /// &lt;param name="p_Token"&gt;登录验证票据&lt;/param&gt;<br /> /// &lt;returns&gt;更新状态&lt;/returns&gt;<br /> [OperationContract(IsOneWay = true)]<br /> void Disconnect();<br /><br /> /// &lt;summary&gt;<br /> /// 更新最后活动时间<br /> /// &lt;/summary&gt;<br /> /// &lt;param name="p_ID"&gt;ID&lt;/param&gt;<br /> /// &lt;param name="p_Token"&gt;登录验证票据&lt;/param&gt;<br /> /// &lt;returns&gt;更新状态&lt;/returns&gt;<br /> [OperationContract]<br /> int? UpdateLastActiveTime(int p_ID, string p_Token);<br /><br /> /// &lt;summary&gt;<br /> /// 取得IM登录信息记录列表<br /> /// &lt;param name="p_EmployeeID"&gt;员工ID&lt;/param&gt;<br /> /// &lt;param name="p_BeginTime"&gt;开始时间&lt;/param&gt;<br /> /// &lt;param name="p_EndTime"&gt;结束时间&lt;/param&gt;<br /> /// &lt;param name="p_Token"&gt;登录验证票据&lt;/param&gt;<br /> /// &lt;returns&gt;IM登录信息记录列表&lt;/returns&gt;<br /> /// &lt;/summary&gt;<br /> [OperationContract]<br /> List&lt;IMModel.IMConnect&gt; GetList(int p_EmployeeID, DateTime p_BeginTime, DateTime p_EndTime, string p_Token);<br /> }<br />}<br /></pre> <p>&nbsp;</p> <p>请遇到问题的人不吝赐教,帮忙解决一下问题。谢谢。</p>
Blood的主页 Blood | 初学一级 | 园豆:185
提问于:2010-08-23 14:57
< >
分享
最佳答案
0

两个终结点使用了同一个TCP端口.

收获园豆:10
Launcher | 高人七级 |园豆:45050 | 2010-08-23 15:09
非双工的这种添加方法都OK的。 所以不是端口重复的问题。
Blood | 园豆:185 (初学一级) | 2010-08-23 15:46
@Blood:别用WcfServiceHost,你使用IIS,或者 ConsoleApplication 来做. WcfServiceHost,WcfTestClient都是第三方开发,有局限性,我记得上次我在msdn回答过一个.
Launcher | 园豆:45050 (高人七级) | 2010-08-23 16:05
一样的。我自己做host。在启动服务的时候,还是会提示一样的错误。 我不知道这到底是为什么。我服务器端程序的问题?还是其他的问题?
Blood | 园豆:185 (初学一级) | 2010-08-23 16:49
@Blood:得,回头我还是测试下吧.
Launcher | 园豆:45050 (高人七级) | 2010-08-23 17:16
@Blood:我测试了下,是这样的,你修改了netTcpBinding 的 maxConnections 属性,这就造成在同一个tcp端口上,mexTcpBinding的配置和netTcpBinding不一致,所以就不允许再创建新的监听. 解决办法: 1,使用默认的 netTcpBinding 设置; 2,把 mex 终结点的 binding 也设置为 "netTcpBinding";另外bindingConfiguration指定的绑定配置,一定要确认 portSharingEnabled="true". 最后,有可能也可以使用自定义的元数据绑定来解决该问题.
Launcher | 园豆:45050 (高人七级) | 2010-08-23 18:11
我按照你说的方法改了。 maxConnections去掉了。 mexTcpBinding如果改成netTcpBinding的话,portSharingEnabled='true',就会报一样的错。就是地址已经被使用,服务无法启动。 如果不设置端口共享,则服务可以启动,打开服务,则提示目标主机积极拒绝。 如果设置为mexTcpBinding的话,服务是可以正常启动了,但是打开服务之后就会报 Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:601/IM/IMConnect/mex'. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:601/IM/IMConnect/mex'. If the service is defined in the current solution, try building the solution and adding the service reference again. 这个错误了。无endpoint监听点。还是蛮奇怪的。我也不知道是为什么?是我的程序的问题?还是其他的问题?
Blood | 园豆:185 (初学一级) | 2010-08-23 19:23
@Blood:你的 netTcpConnectConfigration 也要加 portSharingEnabled ='true' ,就是说要共享端口的所有绑定设置都要加 portSharingEnabled ='true' , Metadata contains a reference that cannot be resolved,一是因为你引用服务的路径不对,另外就是你的服务有问题。你可以尝试用简单的接口来测试下。
Launcher | 园豆:45050 (高人七级) | 2010-08-23 21:15
已经找到问题了。呵呵。是callback中的一个方法名和服务的方法名重名,造成出错。多谢你了。呵呵。
Blood | 园豆:185 (初学一级) | 2010-08-23 21:51

@Blood: 我现在也遇到和你一样的问题,我也开启的有些服务,但是还是报一样的问题,端口被占用,请问下,callback的方法名与服务的方法名重名造成的么

菊花香 | 园豆:200 (初学一级) | 2013-06-06 00:15

@Blood: 

我也出現這情況,請教一下具體哪裡重複名稱

Drek | 园豆:200 (初学一级) | 2013-12-12 16:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册