none
如何配置serviceBehavior RRS feed

  • 问题

  • 问题来自:http://www.cnblogs.com/frank_xl/archive/2009/04/14/1433712.html#1750298
    如果我只用TCP方式:
    配置文件是这样的
    <system.serviceModel>
    <services>
    <service behaviorConfiguration="WCFTransfer.WCFServiceBehavior"
    name="WCFTransfer.WCFService">

    <endpoint
    address="net.tcp://localhost:8002/WCFService"
    binding="netTcpBinding"
    bindingConfiguration="ReliableTCP"
    contract="WCFTransfer.IWCFService">
    <--只用TCP协议传输-->
    </endpoint>

    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />

    <host>
    <baseAddresses>
    <add baseAddress="net.tcp://localhost:8002/"/>
    </baseAddresses>
    </host>

    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
    <behavior name="WCFTransfer.WCFServiceBehavior">
    //此区域应该写什么
    </behavior>
    </serviceBehaviors>
    </behaviors>
    <bindings>
    <netTcpBinding >
    <binding name="ReliableTCP">
    <reliableSession enabled="True"/>
    </binding>
    </netTcpBinding>
    </bindings>
    </system.serviceModel>

    此区域应该写什么 不知道为什么,如果我选择了TCP协议传输,而不要HTTP方式,而且我设置这个地方
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />

    如果还按照原来那样写就会报错
    <serviceBehaviors>
    <behavior name="WCFTransfer.WCFServiceBehavior">
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    </serviceBehaviors>
    这个是不是只用于HTTP协议的?那如果是这样的话,我只用TCP协议的时候<serviceBehaviors>里我应该怎么写,难道是用两种元数据交换方式的另外一种?

    Frank Xu Lei--谦卑若愚,好学若饥
    专注于.NET平台下分布式应用系统开发和企业应用系统集成
    Focus on Distributed Applications Development and EAI based on .NET
    欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
    欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
    欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum
    2010年1月19日 14:40
    版主

答案

全部回复


  • 是的,<serviceMetadata httpGetEnabled="true" />就表示,服务里要提供基于http协议的元数据交换功能。而你的配置值使用WCF,所以这个不能设置为true.修改为false,或者不设置就可以。这样不会出错。

    Frank Xu Lei--谦卑若愚,好学若饥
    专注于.NET平台下分布式应用系统开发和企业应用系统集成
    Focus on Distributed Applications Development and EAI based on .NET
    欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
    欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
    欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum
    2010年1月19日 14:41
    版主
  • 倒,已经不太用配置文件的方式咯。我贴段用代码生成的。每次都要改配置文件太烦
    	   static System.Reflection.Assembly ServiceAssembly = null;
    static System.Reflection.Assembly ServiceInterfaceAssembly = null;
    if(ServiceAssembly == null) ServiceAssembly = System.Reflection.Assembly.Load(WCF_LIB_NAME) if(ServiceInterfaceAssembly == null) ServiceInterfaceAssembly = System.Reflection.Assembly.Load(WCF_INTERFACE_LIB_NAME); ServiceHost tempHost; for (int i = 0; i < WCF_CLASS_NAMES.Length; i++ ) { try { Type implementedContract = ServiceInterfaceAssembly.GetType(string.Format(@"{0}.I{1}", WCF_INTERFACE_LIB_NAME, WCF_CLASS_NAMES[i])); Type serviceType = ServiceAssembly.GetType(string.Format(@"{0}.{1}", WCF_LIB_NAME, WCF_CLASS_NAMES[i])); Uri basicAddress = new Uri(string.Format(BASIC_URI_ADDRESS_FORMAT, WCF_CLASS_NAMES[i])); Uri wsAddress = new Uri(string.Format(WS_URI_ADDRESS_FORMAT, WCF_CLASS_NAMES[i])); Uri tcpAddress = new Uri(string.Format(NETTCP_URI_ADDRESS_FORMAT, WCF_CLASS_NAMES[i])); System.ServiceModel.Channels.Binding baseBinding = null; if (WCF_CLASS_NAMES[i] != @"CrossDomainService") { tempHost = new ServiceHost(serviceType, wsAddress, tcpAddress); #region --添加终结点 if (WCF_CLASS_NAMES[i] == @"DaulServices" ) { tempHost.AddServiceEndpoint(implementedContract, ServiceModel.GetPollDuplexBinding(), basicAddress, basicAddress); baseBinding = ServiceModel.GetWsDaulBinding(); tempHost.AddServiceEndpoint(implementedContract, ServiceModel.GetWsDaulBinding(), wsAddress, wsAddress); } else { tempHost.AddServiceEndpoint(implementedContract, ServiceModel.GetBasicBinding(), basicAddress, basicAddress); baseBinding = ServiceModel.GetWsBinding(); tempHost.AddServiceEndpoint(implementedContract, baseBinding, wsAddress, wsAddress); } tempHost.AddServiceEndpoint(implementedContract, ServiceModel.GetNetTcpBinding(), tcpAddress, tcpAddress); #endregion //---------------------------------我是分割线---------------------------------------- #region --添加元数据交换 ServiceMetadataBehavior metadataBehavior; metadataBehavior = tempHost.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (metadataBehavior == null) { metadataBehavior = new ServiceMetadataBehavior() { HttpGetEnabled = true, HttpGetBinding = baseBinding }; tempHost.Description.Behaviors.Add(metadataBehavior); } ServiceDebugBehavior debugBehavior; debugBehavior = tempHost.Description.Behaviors.Find<ServiceDebugBehavior>(); if (debugBehavior == null) { debugBehavior = new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = false }; tempHost.Description.Behaviors.Add(debugBehavior); } ServiceThrottlingBehavior throttlingBehavior; throttlingBehavior = tempHost.Description.Behaviors.Find<ServiceThrottlingBehavior>(); if (throttlingBehavior == null) { throttlingBehavior = new ServiceThrottlingBehavior() { MaxConcurrentCalls = 10000, MaxConcurrentSessions = 10000, MaxConcurrentInstances = 10000 }; tempHost.Description.Behaviors.Add(throttlingBehavior); } #endregion } else { tempHost = new ServiceHost(serviceType); } if(tempHost != null) { tempHost.Open(); ServiceHostGroup.Add(tempHost); } } catch (Exception ex) { string msg = string.Format(Resources.Exception_Start_Server_String_Fmt, ex.Message, WCF_CLASS_NAMES[i]); msgInfo(msg); LogHelper.WriteInfoError(msg); LogHelper.WriteDebug(msg, ex); } }
    2010年1月22日 7:20