2016-03-07 109 views
0

我有Uri模板參數中的ComplexType的服務,我已將CanConvert()和ConvertStringToValue()方法覆蓋到類MyQueryStringConverter。如何將自定義服務行爲添加到WCF配置

我需要將該行爲添加到web.config文件。

這裏是行爲

public class MyWebHttpBehavior : WebHttpBehavior 
    { 
     protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription) 
     { 
      return new MyQueryStringConverter(); 
     } 
    } 

這裏是配置文件:

<system.serviceModel> 
     <bindings> 
      <webHttpBinding> 
       <binding name="webHttp" 
       maxReceivedMessageSize="20000000" > 
        <security mode="None"> 
         <transport clientCredentialType = "None"/> 
        </security> 
       </binding> 
      </webHttpBinding> 

     </bindings> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="mexBehaviour"> 
        <serviceMetadata httpGetEnabled="true"/> 
        <useRequestHeadersForMetadataAddress> 
         <defaultPorts> 
          <add scheme="http" port="80" /> 
         </defaultPorts> 
        </useRequestHeadersForMetadataAddress> 
       </behavior> 
      </serviceBehaviors> 
      <endpointBehaviors> 
       <behavior name="web"> 
        <webHttp/> 
       </behavior> 
      </endpointBehaviors> 
     </behaviors> 
     <services> 
      <service name="Service.Service1" behaviorConfiguration="mexBehaviour"> 
       <endpoint address="" binding="webHttpBinding" contract="Service.IService1" bindingConfiguration="webHttp" behaviorConfiguration="web"/> 
       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"> 

       </endpoint> 

      </service> 
     </services> 
    </system.serviceModel> 

請幫助如何添加行爲。

回答

0

首先,你必須註冊你的行爲,使用behaviorExtensions元素在配置文件中:

<system.serviceModel> 
    <extensions> 
     <behaviorExtensions> 
     <add name="MyWebHttpBehavior" 
      type="FullNameSpace.MyWebHttpBehavior, MyWebHttpBehavior.AssemblyName, Version=1.0.0.0, Culture=neutral" /> 
     </behaviorExtensions> 
    </extensions> 

其中:

  • 「FullNameSpace.MyWebHttpBehavior」 是完整的命名空間類,
  • 「MyWebHttpBehavior.AssemblyName」是您的程序集名稱,其擴展名爲 ,您的類已編譯。
  • 「1.0.0.0」是您的程序集版本
  • 「中性」是您的文化。更改如有特殊文化需要

謝勝利,聲明自己的行爲:

<behaviors> 
    <endpointBehaviors> 
    <behavior name="customMyWebHttpBehavior"> 
     <webHttp/> 
     <MyWebHttpBehavior/> 
    </behavior> 
    </endpointBehaviors> 
<behaviors> 

接下來,添加到綁定:

<bindings> 
      <webHttpBinding> 
       <binding name="webHttp" 
       maxReceivedMessageSize="20000000" > 
        <security mode="None"> 
         <transport clientCredentialType = "None"/> 
        </security> 
       </binding> 
       <MyWebHttpBehavior /> 
      </webHttpBinding> 

     </bindings> 

最後,行爲設置爲你的服務端點:

<endpoint address="" binding="webHttpBinding" contract="Service.IService1" bindingConfiguration="webHttp" behaviorConfiguration="customMyWebHttpBehavior"/> 

我已經複製了這個配置文件配給我開發的服務並更改名稱以適合您的班級/配置,但請檢查我是否沒有寫錯任何內容。

我已經使用這個鏈接爲基礎設置我的配置:Custom Behavior won't register in my web.config

希望它能幫助。

+0

獲取錯誤無法加載爲擴展名'MyWebHttpBehavior'註冊的類型'Service.MyWebHttpBehavior,Service,Version = 1.0.0.0,Culture = neutral'。 @Ricadro – KousiK

+0

您必須確保名稱空間和程序集名稱正確,並且該程序集位於服務二進制文件的相同文件夾中(例如,bin文件夾)。 –

+0

是的,所有的都是正確的。名稱空間和程序集名稱相同。 @Ricardo Pontual – KousiK

相關問題