Friday 1 January 2016

How to enable WCF Tracing?




In this post I am going to take the same example to demonstrate how to enable wcf service trace. By default WCF service does not trace any messages. To add wcf tracing, please follow below steps –  

Step-1  Open your web.config file from solution explorer. The contents of newly created Service would look a like –


<? Xml version=1.0?>

<configuration>
  <system.web>
    <compilation debug=true targetFramework=4.0 />
  </system.web>
  <system.serviceModel>
    <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>


Note:- In above code tracing and message logging both are not enabled. 


Step-2 To enable the MessageLogging we need to add following code in above web.config file. The important point is MessageLogging can be done at two levels. i.e. service level and another is transport level. Please add given code after the </behaviours> tag highlighted in above code snippet. 
 
    <diagnostics>

      <messageLogging logEntireMessage=true
                      logMessagesAtServiceLevel=false
                      logMessagesAtTransportLevel=false
                      logMalformedMessages=true
                      maxMessagesToLog=5000
                      maxSizeOfMessageToLog=2000>
      </messageLogging>
    </diagnostics>


Attribute details:-
Sr. No
Attribute
Description
1
logEntireMessage
“true” value indicates that the entire message should be logged.
2
logMessagesAtServiceLevel
“true” means service-level messages will be logged.
3
logMessagesAtTransportLevel
Transport level messages will be logged if set to “true”.
4
logMalformedMessages
Malformed message should be logged if set to “true”
5
maxMessagesToLog
This is the number that tells how many messages to be logged.
6
maxSizeOfMessageToLog
The value indicates the maximum size of the message log. Default is 256Kb


Step-3 As mentioned above, we need to define trace source and TraceListener to activate wcf tracing. In below code snippet we have added both Trace Source mentioned earlier and created an object of a tracelistener named as “TextLogger” which will act as a shared tracelistener for both trace sources.Copy and paste below code snippet just after </system.webServer> tag highlighted in first 
 
  <system.diagnostics>
    <trace autoflush=true indentsize=4 />
    <sources>
      <source name=System.ServiceModel switchValue=All>
        <listeners>
          <add name=textLogger />
        </listeners>
      </source>
      <source name=System.ServiceModel.MessageLogging switchValue=All>
        <listeners>
          <add name=textLogger />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name=textLogger
           type=System.Diagnostics.TextWriterTraceListener
           initializeData=C:WCF_logswcf_svclogging.txt />
    </sharedListeners>
  </system.diagnostics>

Please note : Here we have set autoflush=true. If we don’t do that there will be a possibility that the trace file would be corrupted. It would open in any text editor, however WCF trace viewer would not be able to open such corrupted file. 

Step-4 Just change the location of a trace output file highlighted in yellow in above code as per your convenience. Save the file and we are done with the process of enabling wcf tracing. Now execute the WCF service and check the log/trace file created at above highlighted location. 

Step-5 Complete code of Web.config file used in above example for MessageLogging and TraceListener is available here – 
 
<?xml version=1.0?>

<configuration>
  <system.web>
    <compilation debug=true targetFramework=4.0 />
  </system.web>
  <system.serviceModel>
    <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>
    <diagnostics>
      <messageLogging logEntireMessage=true
                      logMessagesAtServiceLevel=false
                      logMessagesAtTransportLevel=false
                      logMalformedMessages=true
                      maxMessagesToLog=5000
                      maxSizeOfMessageToLog=2000>
      </messageLogging>
    </diagnostics>
    <serviceHostingEnvironment multipleSiteBindingsEnabled=true />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests=true/>
  </system.webServer>
  <system.diagnostics>
    <trace autoflush=true indentsize=4 />
    <sources>
      <source name=System.ServiceModel switchValue=All>
        <listeners>
          <add name=textLogger />
        </listeners>
      </source>
      <source name=System.ServiceModel.MessageLogging switchValue=All>
        <listeners>
          <add name=textLogger />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name=textLogger
           type=System.Diagnostics.TextWriterTraceListener
           initializeData=C:WCF_logswcf_svclog.txt />
    </sharedListeners>
  </system.diagnostics>
</configuration>



In the above post I ecplained how to enable WCF trace. I hope you enjoyed this post so please comment your feedback and queries. Thanks You.

Wednesday 30 December 2015

Please explain different modes of security in WCF? Or Explain the difference between Transport and Message Level Security.



In Windows Communication Foundation, we can configure to use security at different levels

  •  Transport Level security.
  •  Message Level Security


Transport Level security.:-

Transport Level security means providing security at the transport layer itself. When dealing with security at Transport level, we are concerned about integrity, privacy and authentication of message as it travels along the physical wire. It depends on the binding being used that how WCF makes it secure because most of the bindings have built-in security.


  <netTcpBinding>
         <binding name=”netTcpTransportBinding”>
                    <security mode=”Transport”>
                          <Transport clientCredentialType=”Windows” />
                    </security>
          </binding>
  </netTcpBinding>


Message Level Security.:-

For Tranport level security, we actually ensure the transport that is being used should be secured but in message level security, we actually secure the message. We encrypt the message before transporting it.

   <wsHttpBinding>

            <binding name=”wsHttpMessageBinding”>
                          <security mode=”Message”>
                                     <Message clientCredentialType=”UserName” />
                          </security>
             </binding>
     </wsHttpBinding>



It totally depends upon the requirements but we can use a mixed security mode also as follows:

  <basicHttpBinding>
             <binding name=”basicHttp”>
                          <security mode=”TransportWithMessageCredential”>
                               <Transport />
                               <Message clientCredentialType=”UserName” />
                          </security>
              </binding>
       </basicHttpBinding>




In this above post I explained  how to use security at different levels in WCF service. I hpe you enjoyed it so please send your feedback and queries. Thnak You.