3. WCF – Web.Config ayarları

Web projelerinde çalışma ayarlarımızı kaydetmek için XML tabanlı web.config’i kullanırız. Wcf host’u için boş bir web site’ı wcf service olarak açtığımızda ve yeni bir web.config eklediğimizde default olarak aşağıdaki gibi olacaktır.

<configuration>

<system.web>
    <compilation debug="false" targetFramework="4.6.1" />
     <httpRuntime targetFramework="4.6.1" />
 </system.web>

</configuration>

Hostumuza sağ tuş ile tıklayığ “Edit WCF Configuration” seçeneğini tıklayıp yeni bir servis nesnesi yarattığımızda web.configimize otomatik olarak aşağıdaki eklemeler gelecektir.

Görüldüğü gibi <system.serviceModel> taglerinin arasında <services></services> tagleri onun içerisinde de bir <service> nesnesi oluşmuştur.  Service’nin içerisinde de bir <endpointoluşmuştur.  Bir istemci bir WCF ile endpoint sayesinde iletişim kurabilir. Bu Endpointteki   address bizim Service.svc üzerinden alıp verdiğimiz adrestir. binding service yaratma sırasında bizim seçtiğimiz binding şekli ve conctract  yine başlangıç sırasında bizim seçtiğimiz Conctracttır.

Aynı endpoint Web.configte sağ tuşla “Edit Wcf Configuration”a tıklandığında servisimizin altındaki end point arayüzünde de görülebilir.

<configuration>

 <system.web>
    <compilation debug="false" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
 </system.web>

<system.serviceModel>
  <services>
       <service name="WcfServisKütüphanesi.Hesaplama">
           <endpoint address="http://localhost:53843/Service.svc" binding="basicHttpBinding" bindingConfiguration="" contract="WcfServisKütüphanesi.IHesaplama" />
       </service>
   </services>
 </system.serviceModel>

</configuration>

Eğer Endpointimize bir name eklersek tag’e name attribute’ı eklenecek ve aşağıdaki gibi değişime uğrayacaktır.

<endpoint address="http://localhost:53843/Service.svc" binding="basicHttpBinding" bindingConfiguration="" name="VERDİGİMİZ İSİM" contract="WcfServisKütüphanesi.IHesaplama" />

Servisimize Behaviour eklemek istediğimizde de yine Web.confige sağ tuş ile tıklayıp “Edit  WCF Configuration -> Advanced -> Service Behavior” üzerinden behaviour oluşturup bunu servisimize verebiliriz. Bir behaviour oluşturduğumuzda system.ServiceModel içerisine aşağıdaki tagler eklenecektir. Service behavioruna her eklediğimiz özellik  <behavior > </behavior > içerisine tek taraflı tag olarak eklenecektir.

Aşağıdaki hiyerarşi  “Davranışlar / Nitelikler –> ServisDavranışları- -> Davranış –> Davranışın özellikleri”  şeklindedir.

<behaviors>
    <serviceBehaviors>
      <behavior name="HesaplamaBehaviour">
          <serviceMetadata />
          <serviceDebug />
      </behavior>
     </serviceBehaviors>
 </behaviors>

Bu davranışı servisimize eklediğimizde web.configteki oluşacak değişiklik de service’e  behaviorConfiguration tagi eklenmesi olacaktır.

<service behaviorConfiguration="HesaplamaBehaviour" name="WcfServisKütüphanesi.Hesaplama">

Son olarak service meta datalarını ulaşılabilir yaptığımızda da aşağıdaki şekilde bir eklenti gelecektir.

<serviceMetadata httpGetEnabled="true" />

Sonuç olarak tüm web.config aşağıdaki gibi olacaktır.

<configuration>

 <system.web>
    <compilation debug="false" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
 </system.web>

   <system.serviceModel>
      
      <behaviors>
         <serviceBehaviors>
            <behavior name="HesaplamaBehaviour">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug />
             </behavior>
         </serviceBehaviors>
      </behaviors>
      <services>
         <service behaviorConfiguration="HesaplamaBehaviour" name="WcfServisKütüphanesi.Hesaplama">
              <endpoint address="http://localhost:53843/Service.svc" binding="basicHttpBinding" bindingConfiguration="" name="HesaplamaEndPoint" contract="WcfServisKütüphanesi.IHesaplama" />
          </service>
      </services>

   </system.serviceModel>
 </configuration>

İkinci bir Servis ve Svc daha eklendiğindeyse aşağıdaki gibi çoklu site bağlama yetkisi veren bir tag eklenecek.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

ve Web.Config son hali aşağıdaki gibi olacaktır.

<configuration>

  <system.web>
     <compilation debug="true" targetFramework="4.6.1" />
     <httpRuntime targetFramework="4.6.1" />
  </system.web>

  <system.serviceModel>
     <serviceHostingEnvironment aspNetCompatibilityEnabled="truemultipleSiteBindingsEnabled="true" />
       <behaviors>
          <serviceBehaviors>
               <behavior name="HesaplamaBehaviour">
                 <serviceMetadata httpGetEnabled="true" />
                 <serviceDebug />
               </behavior>
               <behavior name="YazdirBehavior">
                <serviceMetadata httpGetEnabled="true" />
              </behavior>
         </serviceBehaviors>
       </behaviors>
    <services>
        <service behaviorConfiguration="HesaplamaBehaviour" name="WcfServisKütüphanesi.Hesaplama">
            <endpoint address="http://localhost:53843/Service.svc" binding="basicHttpBindingbindingConfiguration="" name="HesaplamaEndPoint" contract="WcfServisKütüphanesi.IHesaplama" />
         </service>
        <service behaviorConfiguration="YazdirBehavior" name="WcfServisKütüphanesi.Yazdır">
            <endpoint address="net.tcp://localhost:53843/Service2.svc" binding="netTcpBindingbindingConfiguration="" name="YazdirEndPoint" contract="WcfServisKütüphanesi.IYazdir" />
        </service>
    </services>
    </system.serviceModel>
 </configuration>