我想在 WCF4 中开发一个非常小的服务,使用 NetTcpBinding 提供存在信息(用户、登录、注销等)的回调。
我的开发机器是带有 Visual Studio 2010 的 Windows 7 64 Es,没有安装 IIS。
为了创建服务,我创建了一个 WCF 服务应用程序项目类型并编辑了它的 web.config 是:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NewBinding0" portSharingEnabled="true">
<reliableSession enabled="true" />
<security mode="None" />
</binding>
</netTcpBinding>
<mexTcpBinding>
<binding name="NewBinding1" />
</mexTcpBinding>
</bindings>
<services>
<service name="JuguesServices.JuguesPresenceService">
<endpoint address="net.tcp://localhost:57920/JuguesServices/JuguesPresenceService"
binding="netTcpBinding" bindingConfiguration="NewBinding0" contract="JuguesServices.IJuguesPresenceService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
然后我点击“播放”按钮,一个 ASP.NET 开发服务器在端口 57920 上启动,但抛出了这个异常:
Server Error in '/' Application.
The protocol 'net.tcp' is not supported.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The protocol 'net.tcp' is not supported.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidOperationException: The protocol 'net.tcp' is not supported.]
System.ServiceModel.Activation.HostedTransportConfigurationManager.InternalGetConfiguration(String scheme) +98456
System.ServiceModel.Activation.HostedAspNetEnvironment.GetBaseUri(String transportScheme, Uri listenUri) +24
System.ServiceModel.Channels.TransportChannelListener.OnOpening() +12082260
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +274
System.ServiceModel.Channels.ReliableChannelListenerBase`1.OnOpen(TimeSpan timeout) +110
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +72
[InvalidOperationException: The ChannelDispatcher at 'net.tcp://localhost:57920/' with contract(s) '"IJuguesPresenceService"' is unable to open its IChannelListener.]
System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +118
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +111
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
System.ServiceModel.Channels.CommunicationObject.Open() +36
System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +184
System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +615
[ServiceActivationException: The service '/JuguesPresenceService.svc' cannot be activated due to an exception during compilation. The exception message is: The ChannelDispatcher at 'net.tcp://localhost:57920/' with contract(s) '"IJuguesPresenceService"' is unable to open its IChannelListener..]
System.Runtime.AsyncResult.End(IAsyncResult result) +679246
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, String routeServiceVirtualPath, Boolean flowContext, Boolean ensureWFService) +234
System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +355
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
我的配置文件不正确吗?
是否可以使用带有 ASP.NET 开发服务器的 NetTcpBinding 来调试服务?
是否必须使用 NetTcpBinding 才能使用 CallBacks?
PS:我正在尝试遵循此示例: http: //www.codeproject.com/KB/WCF/WCFWPFChat.aspx
编辑:合同:
/// <summary>
/// The service specification
/// </summary>
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IJuguesPresenceServiceCallback))]
interface IJuguesPresenceService
{
/// <summary>
/// Registers that an user is now logged in the system, and
/// notifyes it to its contacts.
/// </summary>
/// <param name="user_id">The user identificator who logs in.</param>
/// <returns>True if the login was successfull, false elsewhere.</returns>
[OperationContract(IsOneWay = false, IsInitiating = true, IsTerminating = false)]
bool UserLogin(string user_id);
/// <summary>
/// Registers that a user is no longer logged in the system, and
/// notifies it to its contact.
/// </summary>
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = true)]
void UserLogout();
/// <summary>
/// Returns the contact list of a logged in user.
/// </summary>
/// <returns>The list of contacts for the user.</returns>
/// <exception cref="InvalidOperationException">When tring to get the contacts without log in.</exception>
[OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]
JuguesContact[] GetContacts();
}
回调
/// <summary>
/// The events for the server-to-client communication.
/// </summary>
interface IJuguesPresenceServiceCallback
{
/// <summary>
/// Ocurrs when a contact logs in in the system.
/// </summary>
/// <param name="user_id">The identificator of the user who logged in.</param>
[OperationContract(IsOneWay = true)]
void UserLoggedIn(string user_id);
/// <summary>
/// Occurs when a contact logs out of the system.
/// </summary>
/// <param name="user_id">The identificator of the user who logged out.</param>
[OperationContract(IsOneWay = true)]
void UserLoggedOut(string user_id);
}