<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Framework Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/category/development/framework/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/category/development/framework/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 10 Jan 2019 14:18:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>.NET XML Serialization of CDATA ATTRIBUTE</title>
		<link>https://blogit.create.pt/marcosilva/2008/11/23/net-xml-serialization-of-cdata-attribute/</link>
					<comments>https://blogit.create.pt/marcosilva/2008/11/23/net-xml-serialization-of-cdata-attribute/#comments</comments>
		
		<dc:creator><![CDATA[Marco Silva]]></dc:creator>
		<pubDate>Sun, 23 Nov 2008 14:05:11 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[.NET]]></category>
		<guid isPermaLink="false">http://blogcreate.azurewebsites.net/marcosilva/?p=51</guid>

					<description><![CDATA[<p>Recently I needed to create a .NET class (C#) that when serialized should contain a CDATA attribute similar to this: &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34; ?&#62; &#60;RootNode Type=&#34;zzzzz&#34; Version=&#34;1&#34; &#62; &#60;CDataAttribute&#62; &#60;![CDATA[ &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34; ?&#62; ]]&#62; &#60;/CDataAttribute&#62; &#60;/RootNode&#62; &#160; I started by creating a schema for validating this xml that can be (generated with xsd.exe and [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/11/23/net-xml-serialization-of-cdata-attribute/">.NET XML Serialization of CDATA ATTRIBUTE</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Recently I needed to create a .NET class (C#) that when serialized should contain a <strong>CDATA</strong> attribute similar to this:</p>
<div>
<div>
<pre><span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span> <span style="color: #ff0000">encoding</span><span style="color: #0000ff">=&quot;utf-8&quot;</span> ?<span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">RootNode</span> <span style="color: #ff0000">Type</span><span style="color: #0000ff">=&quot;zzzzz&quot;</span> <span style="color: #ff0000">Version</span><span style="color: #0000ff">=&quot;1&quot;</span> <span style="color: #0000ff">&gt;</span>    </pre>
<pre>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">CDataAttribute</span><span style="color: #0000ff">&gt;</span></pre>
<pre>        <span style="color: #0000ff">&lt;!</span>[CDATA[</pre>
<pre>         <span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span> <span style="color: #ff0000">encoding</span><span style="color: #0000ff">=&quot;utf-8&quot;</span> ?<span style="color: #0000ff">&gt;</span></pre>
<pre>        ]]<span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">CDataAttribute</span><span style="color: #0000ff">&gt;</span>    </pre>
<pre><span style="color: #0000ff">&lt;/</span><span style="color: #800000">RootNode</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p><p>&#160;</p>
<p>I started by creating a schema for validating this xml that can be (generated with <strong>xsd.exe </strong>and slightly altered):</p>
<div>
<div>
<pre><span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span> <span style="color: #ff0000">encoding</span><span style="color: #0000ff">=&quot;utf-8&quot;</span>?<span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:schema</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;Example&quot;</span> <span style="color: #ff0000">xmlns:xs</span><span style="color: #0000ff">=&quot;http://www.w3.org/2001/XMLSchema&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:element</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;RootNode&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:complexType</span><span style="color: #0000ff">&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:sequence</span><span style="color: #0000ff">&gt;</span></pre>
<pre>        <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:element</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;CDataAttribute&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;xs:string&quot;</span> <span style="color: #ff0000">minOccurs</span><span style="color: #0000ff">=&quot;0&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:sequence</span><span style="color: #0000ff">&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:attribute</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Type&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;xs:string&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:attribute</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Version&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;xs:string&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:complexType</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:element</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:element</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Example&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:complexType</span><span style="color: #0000ff">&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:choice</span> <span style="color: #ff0000">minOccurs</span><span style="color: #0000ff">=&quot;0&quot;</span> <span style="color: #ff0000">maxOccurs</span><span style="color: #0000ff">=&quot;unbounded&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>        <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:element</span> <span style="color: #ff0000">ref</span><span style="color: #0000ff">=&quot;RootNode&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:choice</span><span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:complexType</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:element</span><span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:schema</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p>Note that the <strong>CDataAttribute</strong> is specified as type string and I could not find any type for referring a CDATA.</p>
<p>Now if you take this schema and generate a class from it (again with <strong>xsd.exe</strong>) you will see that the CDataAttribute is, as expected, represented by a string.</p>
<div>
<div>
<pre><span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> cDataAttributeField;</pre>
</p></div>
</div>
<p><p>If you use the generated class and serialize a object from it, the .NET serializer will not encode the CDataAttribute between a CDATA xml structure, but instead will escape all the special characters that you place inside the CDATA.</p>
<div>
<div>
<pre><span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span>?<span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">Example</span> <span style="color: #ff0000">xmlns:xsi</span><span style="color: #0000ff">=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></pre>
<pre>         <span style="color: #ff0000">xmlns:xsd</span><span style="color: #0000ff">=&quot;http://www.w3.org/2001/XMLSchema&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;</span><span style="color: #800000">RootNode</span> <span style="color: #ff0000">Type</span><span style="color: #0000ff">=&quot;xxxx&quot;</span> <span style="color: #ff0000">Version</span><span style="color: #0000ff">=&quot;1&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">CDataAttribute</span><span style="color: #0000ff">&gt;</span><span style="color: #ff0000">&amp;lt;</span>?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?<span style="color: #ff0000">&amp;gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">CDataAttribute</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">RootNode</span><span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Example</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p>As I really needed it to be placed inside a CData section, I searched on the Internet but did found a satisfying solution for this “problem”, but using the idea behind some of the articles (use <strong>XmlNode </strong>for the class CDataAttribute member instead of string) my solution was the following.</p>
<p>&#160;</p>
<p>First I generated my schema class without the <strong>CDataAttribute</strong>.</p>
<div>
<div>
<pre><span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span> <span style="color: #ff0000">encoding</span><span style="color: #0000ff">=&quot;utf-8&quot;</span>?<span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:schema</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;Example&quot;</span> <span style="color: #ff0000">xmlns:xs</span><span style="color: #0000ff">=&quot;http://www.w3.org/2001/XMLSchema&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:element</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;RootNode&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:complexType</span><span style="color: #0000ff">&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:sequence</span><span style="color: #0000ff">&gt;</span></pre>
<pre>        <span style="color: #008000">&lt;!-- NOTE: We could not generate a CDATA directly from XSD.</span></pre>
<pre><span style="color: #008000">             Generated by partial class --&gt;</span></pre>
<pre>        <span style="color: #008000">&lt;!--&lt;xs:element name=&quot;CDataAttribute&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot;/&gt;--&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:sequence</span><span style="color: #0000ff">&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:attribute</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Type&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;xs:string&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:attribute</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Version&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;xs:string&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:complexType</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:element</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:element</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Example&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:complexType</span><span style="color: #0000ff">&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:choice</span> <span style="color: #ff0000">minOccurs</span><span style="color: #0000ff">=&quot;0&quot;</span> <span style="color: #ff0000">maxOccurs</span><span style="color: #0000ff">=&quot;unbounded&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>        <span style="color: #0000ff">&lt;</span><span style="color: #800000">xs:element</span> <span style="color: #ff0000">ref</span><span style="color: #0000ff">=&quot;RootNode&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre>      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:choice</span><span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:complexType</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:element</span><span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;/</span><span style="color: #800000">xs:schema</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p><p>Then I extended the partial generated class with my own implementation of the CDataAttribute.</p>
<div><strong></strong></p>
<div>
<pre><span style="color: #0000ff">using</span> System;</pre>
<pre><span style="color: #0000ff">using</span> System.Xml;</pre>
<pre>&#160;</pre>
<pre><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> RootNode</pre>
<pre>{</pre>
<pre>    <span style="color: #0000ff">private</span> XmlNode cDataAttributeField;</pre>
<pre>&#160;</pre>
<pre>    <span style="color: #008000">/// &lt;remarks/&gt;</span></pre>
<pre>    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]</pre>
<pre>    <span style="color: #0000ff">public</span> XmlNode CDataAttribute</pre>
<pre>    {</pre>
<pre>        get {<span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.cDataAttributeField;}</pre>
<pre>        set {<span style="color: #0000ff">this</span>.cDataAttributeField = <span style="color: #0000ff">value</span>;}</pre>
<pre>    }</pre>
<pre>    </pre>
<pre>    <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre>    <span style="color: #008000">/// Getter/Setter method for the CDATA document content field.</span></pre>
<pre>    <span style="color: #008000">/// Encapsulates the internal CDATA XmlNode</span></pre>
<pre>    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<pre>    [System.Xml.Serialization.XmlIgnore()]</pre>
<pre>    <span style="color: #0000ff">public</span> String CDataAttributeByString</pre>
<pre>    {</pre>
<pre>        get </pre>
<pre>        { <span style="color: #008000">// Retrieves the content of the encapsulated CDATA</span></pre>
<pre>          <span style="color: #0000ff">return</span> cDataAttributeField.Value;</pre>
<pre>        }</pre>
<pre>&#160;</pre>
<pre>        set</pre>
<pre>        { <span style="color: #008000">// Encapsulate in a CDATA XmlNode</span></pre>
<pre>          XmlDocument xmlDocument = <span style="color: #0000ff">new</span> XmlDocument();</pre>
<pre>          <span style="color: #0000ff">this</span>.cDataAttributeField = xmlDocument.CreateCDataSection(<span style="color: #0000ff">value</span>);</pre>
<pre>        }</pre>
<pre>    }</pre>
<pre>}</pre>
</p></div>
</div>
<p>Note that I used a <strong>XMLNode</strong> for representing the class member that will be serialized. For setting this XMLNode I created a property (<strong>CDataAttributeByString</strong>) that receives a string and places in the XmlNode a <strong>CDataSection </strong>containing the received string value.</p>
<p>As so I only work with the CDataAttributeByString property, that is excluded from the serialization, and I let the .NET serializer use the CDataAttribute property for properly creating my CDataAttribute.</p>
<p>&#160;</p>
<p>Using the extended class, the serialized xml will now be:</p>
<div>
<div>
<pre><span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span>?<span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">Example</span> <span style="color: #ff0000">xmlns:xsi</span><span style="color: #0000ff">=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></pre>
<pre>         <span style="color: #ff0000">xmlns:xsd</span><span style="color: #0000ff">=&quot;http://www.w3.org/2001/XMLSchema&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;</span><span style="color: #800000">RootNode</span> <span style="color: #ff0000">Type</span><span style="color: #0000ff">=&quot;xxxx&quot;</span> <span style="color: #ff0000">Version</span><span style="color: #0000ff">=&quot;1&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">CDataAttribute</span><span style="color: #0000ff">&gt;</span></pre>
<pre>        <span style="color: #0000ff">&lt;!</span>[CDATA[<span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span> <span style="color: #ff0000">encoding</span><span style="color: #0000ff">=&quot;utf-8&quot;</span> ?<span style="color: #0000ff">&gt;</span>]]<span style="color: #0000ff">&gt;</span></pre>
<pre>    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">CDataAttribute</span><span style="color: #0000ff">&gt;</span></pre>
<pre>  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">RootNode</span><span style="color: #0000ff">&gt;</span></pre>
<pre><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Example</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p>&#160;</p>
<p>I hope that this can help some of you, and if you know a better solution please tell me.</p>
<p>I leave a sample project <a href="http://cid-9f675488377b7270.skydrive.live.com/self.aspx/Blog/CDataSerialization.7z" target="_blank">here</a>.</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/11/23/net-xml-serialization-of-cdata-attribute/">.NET XML Serialization of CDATA ATTRIBUTE</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/marcosilva/2008/11/23/net-xml-serialization-of-cdata-attribute/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Developing a WCF Service &#8211; Fault Exceptions AND FAULT Contracts</title>
		<link>https://blogit.create.pt/marcosilva/2008/05/18/developing-a-wcf-service-fault-exceptions-and-fault-contracts/</link>
					<comments>https://blogit.create.pt/marcosilva/2008/05/18/developing-a-wcf-service-fault-exceptions-and-fault-contracts/#respond</comments>
		
		<dc:creator><![CDATA[Marco Silva]]></dc:creator>
		<pubDate>Sun, 18 May 2008 12:59:54 +0000</pubDate>
				<category><![CDATA[WebServices]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Integration]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Service]]></category>
		<guid isPermaLink="false">http://blogcreate.azurewebsites.net/marcosilva/?p=111</guid>

					<description><![CDATA[<p>This next post about WCF will show how to integrate Fault Exceptions and Fault Contracts in your service. As written in MSDN Fault Exceptions are used for &#34;&#8230; mapping managed exception objects to SOAP fault objects and SOAP fault objects to managed exception objects &#8230;&#34;. This means that if you are looking for interoperability in [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/05/18/developing-a-wcf-service-fault-exceptions-and-fault-contracts/">Developing a WCF Service &#8211; Fault Exceptions AND FAULT Contracts</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This next post about WCF will show how to integrate Fault Exceptions and Fault Contracts in your service.</p>
<p>As written in MSDN Fault Exceptions are used for &quot;&#8230; mapping managed exception objects to SOAP fault objects and SOAP fault objects to managed exception objects &#8230;&quot;. This means that if you are looking for interoperability in your service development then you definitely should use this, because no Java client will understand a .NET exception.</p>
<p>You can read some more about it in this link <a href="http://msdn.microsoft.com/en-us/library/ms733721.aspx" target="_blank">Specifying and Handling Faults in Contracts and Services</a>.</p>
<p>So, when using Fault Exceptions (or SOAP Faults) you have two alternatives: </p>
<ul>
<li>Normal typed (or &quot;un-typed&quot;) &#8211; You throw a regular <font face="Courier New">FaultException <font face="Verdana">which only contains general information about the SOAP Fault</font></font></li>
<li>Strongly-typed &#8211; You define your own SOAP Fault object, which can contain any kind of information you want about the SOAP Fault</li>
</ul>
<p>Strongly-typed SOAP Faults are then used when you wish to give to the client some more information and context about the cause of the exception. They also couldn&#8217;t be much easier to define in WCF, because they are no more, no less than another Contract in the service definition, which are semantically called<strong> Fault Contracts</strong>.</p>
<p>As such, you define a Fault Contract in your service definition in the following way:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// Strongly-typed fault.</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre>[DataContract]</pre>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ArgumentFault</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #cc6633">#region</span> Get / Set</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// </span></pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre>    [DataMember]</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> String Operation { get; set; }</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// </span></pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre>    [DataMember]</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> String Argument { get; set; }</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// </span></pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre>    [DataMember]</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> String Message { get; set; }</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #cc6633">#endregion</span></pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>You then define that an operation can throw this type of fault contract by appending the following to your operation:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre>[FaultContract(<span style="color: #0000ff">typeof</span>(ArgumentFault))]</pre>
<p><!--CRLF--></p>
<pre>[OperationContract]</pre>
<p><!--CRLF--></p>
<pre>Customer GetCustomer(Int32 customerId);</pre>
<p><!--CRLF--></div>
</div>
<p>Now in the service implementation when you want to throw the defined fault contract you do like this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #0000ff">public</span> Customer GetCustomer(<span style="color: #0000ff">int</span> customerId)</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">if</span> (customerId &lt;= 0)</pre>
<p><!--CRLF--></p>
<pre>    { <span style="color: #008000">// Not possible</span></pre>
<p><!--CRLF--></p>
<pre>        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> FaultException&lt;ArgumentFault&gt;(<span style="color: #0000ff">new</span> ArgumentFault()</pre>
<p><!--CRLF--></p>
<pre>        {</pre>
<p><!--CRLF--></p>
<pre>            Operation = MethodBase.GetCurrentMethod().Name,</pre>
<p><!--CRLF--></p>
<pre>            Argument = <span style="color: #006080">&quot;customerId&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre>            Message = <span style="color: #006080">&quot;Argument must be greater than zero.&quot;</span></pre>
<p><!--CRLF--></p>
<pre>        });</pre>
<p><!--CRLF--></p>
<pre>    }</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">return</span> Customers.First(customer =&gt; </pre>
<p><!--CRLF--></p>
<pre>    {</pre>
<p><!--CRLF--></p>
<pre>        <span style="color: #0000ff">return</span> customer.Id == customerId;</pre>
<p><!--CRLF--></p>
<pre>    });</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>This is better than just doing:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> FaultException(MethodBase.GetCurrentMethod().Name + </pre>
<p><!--CRLF--></p>
<pre>               <span style="color: #006080">&quot;: customerId - Argument must be greater than zero.&quot;</span>);</pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p>In the client application for catching this Fault Exception, you code should be:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #0000ff">try</span></pre>
<p><!--CRLF--></p>
<pre>{    <span style="color: #008000">// Get customer</span></pre>
<p><!--CRLF--></p>
<pre>    Customer singleCustomer = proxy.GetCustomer(customers[0].Id);</pre>
<p><!--CRLF--></p>
<pre>    Console.WriteLine(<span style="color: #006080">&quot;SingleCustomer: Id - {0} / Name - {1}.&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre>                singleCustomer.Id, singleCustomer.Name);</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Force fault</span></pre>
<p><!--CRLF--></p>
<pre>    singleCustomer = proxy.GetCustomer(0);</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">catch</span> (FaultException&lt;ArgumentFault&gt; ex)</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    Console.WriteLine(<span style="color: #006080">&quot;FaultException&lt;ArgumentFault&gt;: {0} - {1} - {2}&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre>            ex.Detail.Operation, ex.Detail.Argument, ex.Detail.Message);</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">catch</span> (FaultException ex)</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    Console.WriteLine(<span style="color: #006080">&quot;FaultException: {0}&quot;</span>, ex.Message);</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p>Again the full code is available <a href="http://t57huw.bay.livefilestore.com/y1pJpoyaXYtws7yTirA8cPK4GHb97yAnyjv0wrs-vnuqs5hAiolrr8VMkddvE8aQUShobrhPzZtmJID5oydxb8K3a5IWn8mkif0/SampleService.7z?download" target="_blank">here</a>: </p>
<p>&#160;</p>
<p><strong></strong></p>
<p><strong>Related List of articles</strong></p>
<p><a href="http://blogit.create.pt/blogs/marcosilva/archive/2008/03/30/Developing-a-WCF-Service.aspx" target="_blank">Developing a WCF Service</a></p>
<p><a href="http://blogit.create.pt/blogs/marcosilva/archive/2008/04/12/Developing-a-WCF-Service-_2D00_-Calling-the-hosted-service.aspx" target="_blank">Developing a WCF Service &#8211; Calling the hosted service</a></p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/05/18/developing-a-wcf-service-fault-exceptions-and-fault-contracts/">Developing a WCF Service &#8211; Fault Exceptions AND FAULT Contracts</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/marcosilva/2008/05/18/developing-a-wcf-service-fault-exceptions-and-fault-contracts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Developing a WCF Service &#8211; Calling the hosted service</title>
		<link>https://blogit.create.pt/marcosilva/2008/04/12/developing-a-wcf-service-calling-the-hosted-service/</link>
					<comments>https://blogit.create.pt/marcosilva/2008/04/12/developing-a-wcf-service-calling-the-hosted-service/#respond</comments>
		
		<dc:creator><![CDATA[Marco Silva]]></dc:creator>
		<pubDate>Sat, 12 Apr 2008 14:03:54 +0000</pubDate>
				<category><![CDATA[WebServices]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Integration]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Service]]></category>
		<guid isPermaLink="false">http://blogcreate.azurewebsites.net/marcosilva/?p=131</guid>

					<description><![CDATA[<p>In the first part of this topic, I showed you how to develop a WCF service. In this next part I will show how to implement a client application that calls the exposed service methods, and how to make some enhancements to your service hosting. Ok, so he already have built and deployed our service [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/04/12/developing-a-wcf-service-calling-the-hosted-service/">Developing a WCF Service &#8211; Calling the hosted service</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In the first <a href="http://blogit.create.pt/blogs/marcosilva/archive/2008/03/30/Developing-a-WCF-Service.aspx">part</a> of this topic, I showed you how to develop a WCF service. In this next part I will show how to implement a client application that calls the exposed service methods, and how to make some enhancements to your service hosting.</p>
<p>Ok, so he already have built and deployed our service (either self-hosted, or in IIS for example), and by using a web browser we can see the service welcome page. Now, to call the service methods we must create a client proxy, and to do this we can use the Visual Studio <strong>Add Web Reference</strong>, which automatically creates the proxy, or we can use the command line <strong>svcutil </strong>to manually create the proxy. I prefer to do it manually because I like to control things like the generated proxy class namespace.</p>
<p>For example purposes, I created a Console Application that I called <strong>Tests.SampleService</strong>. There I created a batch file for calling the <strong>svcutil</strong> with my own parameters, with something similar to this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre>svcutil %SERVICE_URL% /namespace:*,%NAMESPACE% /d:%OUT_DIR% /out:%OUT_FILE%</pre>
<p><!--CRLF--></div>
</div>
<p>With the service running, I executed the batch file and svcutil generated two files:</p>
<ul>
<li><strong>output.config</strong> &#8211; Contains the WCF runtime configurations for calling the service (&lt;system.serviceModel&gt;) </li>
<li><strong>Sample.Service.Proxy.cs </strong>&#8211; Contains the service proxy class, and any other classes exposed by the service </li>
</ul>
<p>The contents of the <strong>output.config </strong>should be placed in the console application <strong>App.Config </strong>(if you prefer you can just rename the file). </p>
<p>In your console application main file you can place code like the following:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>   Console.WriteLine(<span style="color: #006080">&quot;Press any key to start&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>   Console.ReadLine();</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>   <span style="color: #008000">// Build the service proxy object</span></pre>
<p><!--CRLF--></p>
<pre>   ServiceClient proxy = <span style="color: #0000ff">new</span> ServiceClient();</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>   <span style="color: #008000">// Call service methods</span></pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>   <span style="color: #008000">// Get total number of customers</span></pre>
<p><!--CRLF--></p>
<pre>   Int32 totalCustomers = proxy.GetTotalNumberOfCustomers();</pre>
<p><!--CRLF--></p>
<pre>   Console.WriteLine(<span style="color: #006080">&quot;Total Customers: {0}&quot;</span>, totalCustomers);</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>   <span style="color: #008000">// Get list of customers</span></pre>
<p><!--CRLF--></p>
<pre>   List&lt;Customer&gt; customers = <span style="color: #0000ff">new</span> List&lt;Customer&gt;(proxy.GetCustomers());</pre>
<p><!--CRLF--></p>
<pre>   <span style="color: #0000ff">foreach</span> (Customer customer <span style="color: #0000ff">in</span> customers)</pre>
<p><!--CRLF--></p>
<pre>   {</pre>
<p><!--CRLF--></p>
<pre>       Console.WriteLine(<span style="color: #006080">&quot;Customers&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>       Console.WriteLine(<span style="color: #006080">&quot;Customer: Id - {0} / Name - {1}.&quot;</span>, customer.Id, customer.Name);</pre>
<p><!--CRLF--></p>
<pre>   }</pre>
<p><!--CRLF--></p>
<pre>            </pre>
<p><!--CRLF--></p>
<pre>   <span style="color: #008000">// Get customer</span></pre>
<p><!--CRLF--></p>
<pre>   Customer singleCustomer = proxy.GetCustomer(customers[0].Id);</pre>
<p><!--CRLF--></p>
<pre>   Console.WriteLine(<span style="color: #006080">&quot;SingleCustomer: Id - {0} / Name - {1}.&quot;</span>, singleCustomer.Id, singleCustomer.Name);</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>   Console.WriteLine(<span style="color: #006080">&quot;Press any key to exit&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>   Console.ReadLine();</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>You are know ready to start your service and use your client application to call the service methods.</p>
<p>&#160;</p>
<h2></h2>
<h2>Service Host Enhancements</h2>
<p>Regarding the self service hosting there are some steps that you can do to make it more robust to errors and faults. Two of these changes are:</p>
<ul>
<li>Subscribe the service host <strong>Faulted </strong>event </li>
<li>Subscribe the service host <strong>Unknown Message Received </strong>event </li>
</ul>
<p>This should be done before opening our service hosting, and the new <strong>StartServiceHost </strong>method should look like this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// Creates the service host</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> StartServiceHost()</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    Logger.Info(<span style="color: #006080">&quot;StartServiceHost: Starting service hosting...&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Create service host</span></pre>
<p><!--CRLF--></p>
<pre>    _serviceHost = <span style="color: #0000ff">new</span> ServiceHost(<span style="color: #0000ff">typeof</span>(SampleService.Service));</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Subscribe to the Faulted event of the service host</span></pre>
<p><!--CRLF--></p>
<pre>    _serviceHost.Faulted += <span style="color: #0000ff">new</span> EventHandler(FaultHandler);</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Subscribe to the Unknown message received event of the service host</span></pre>
<p><!--CRLF--></p>
<pre>    _serviceHost.UnknownMessageReceived += <span style="color: #0000ff">new</span> EventHandler&lt;UnknownMessageReceivedEventArgs&gt;(UnknownMessageHandler);</pre>
<p><!--CRLF--></p>
<pre>    </pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">try</span></pre>
<p><!--CRLF--></p>
<pre>    { <span style="color: #008000">// Open service host</span></pre>
<p><!--CRLF--></p>
<pre>        _serviceHost.Open();</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>        Logger.Info(<span style="color: #006080">&quot;StartServiceHost: Service hosting success.&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>    }</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">catch</span> (Exception ex)</pre>
<p><!--CRLF--></p>
<pre>    {</pre>
<p><!--CRLF--></p>
<pre>        Logger.Fatal(<span style="color: #006080">&quot;StartServiceHost: Could not start service hosting.&quot;</span>, ex);</pre>
<p><!--CRLF--></p>
<pre>    }            </pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p>The event handler&#8217;s code can be:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// Fault error event handler</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> FaultHandler(Object sender, EventArgs e)</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Examine the properties of the service host</span></pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Log reason for fault</span></pre>
<p><!--CRLF--></p>
<pre>    Logger.Error(<span style="color: #006080">&quot;FaultHandler: Host Fault occured. Aborting and restarting the hosting&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Abort the service (Could use Close, but a service in this state does not responde to requests, and Close takes more time)</span></pre>
<p><!--CRLF--></p>
<pre>    _serviceHost.Abort();</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Re-start service host</span></pre>
<p><!--CRLF--></p>
<pre>    StartServiceHost();</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// Unknown message event handler</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> UnknownMessageHandler(Object sender, UnknownMessageReceivedEventArgs e)</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Log the unknown message</span></pre>
<p><!--CRLF--></p>
<pre>    Logger.Warn(<span style="color: #006080">&quot;UnknownMessageHandler: Unknown Message Received&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p>Take into attention that when the service host goes into a <strong>Faulted </strong>state, it stops servicing requests. This is why in the <strong>FaultHandler</strong> we abort the service host and open it again.</p>
<p>You can make a quick test of the event handlers by introducing in your web browser the following address: <a href="http://localhost:8080/SampleService/Service.svc">http://localhost:8080/SampleService/Service.svc</a>.</p>
<p>This makes the service receive an <strong>unknown</strong> message and the corresponding event handler is called.</p>
<p>I leave <a href="http://t57huw.bay.livefilestore.com/y1pJpoyaXYtws7yTirA8cPK4GHb97yAnyjv0wrs-vnuqs5hAiolrr8VMkddvE8aQUShobrhPzZtmJID5oydxb8K3a5IWn8mkif0/SampleService.7z?download" target="_blank">here</a> the complete Visual Studio solution.</p>
<p>&#160;</p>
<h2>Related list of articles:</h2>
<p><a href="http://blogit.create.pt/blogs/marcosilva/archive/2008/03/30/Developing-a-WCF-Service.aspx" target="_blank">Developing a WCF Service</a></p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/04/12/developing-a-wcf-service-calling-the-hosted-service/">Developing a WCF Service &#8211; Calling the hosted service</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/marcosilva/2008/04/12/developing-a-wcf-service-calling-the-hosted-service/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Developing a WCF Service</title>
		<link>https://blogit.create.pt/marcosilva/2008/03/30/developing-a-wcf-service/</link>
					<comments>https://blogit.create.pt/marcosilva/2008/03/30/developing-a-wcf-service/#respond</comments>
		
		<dc:creator><![CDATA[Marco Silva]]></dc:creator>
		<pubDate>Sun, 30 Mar 2008 20:20:42 +0000</pubDate>
				<category><![CDATA[WebServices]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Integration]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Service]]></category>
		<guid isPermaLink="false">http://blogcreate.azurewebsites.net/marcosilva/?p=151</guid>

					<description><![CDATA[<p>It is typical to have projects based on a client-server architecture where the server publishes a number of services. These services are normally Web-Services, and as so are available through the SOAP protocol. The &#34;traditional&#34; way of implementing these services is by using ASMX, but nowadays you have another great alternative &#8211; Windows Communication Foundation [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/03/30/developing-a-wcf-service/">Developing a WCF Service</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>It is typical to have projects based on a client-server architecture where the server publishes a number of services. These services are normally Web-Services, and as so are available through the SOAP protocol.</p>
<p>The &quot;traditional&quot; way of implementing these services is by using ASMX, but nowadays you have another great alternative &#8211; Windows Communication Foundation (WCF) in .NET 3.5.</p>
<p>This, being my first WCF post, intends to demonstrate that developing a simple WCF service is as easy as doing it in ASMX, while allowing us to make future evolutions on our service architecture to support everything that WCF makes available.</p>
<h2>&#160;</h2>
<p>Well, as always you start out by creating a new Visual Studio project. I chose to use the existent project template &#8216;WCF Service Library&#8217; (but some of you may want to start by an empty project 😀 ).</p>
<p>This creates a project with the following list of files (I changed the service name to &#8216;Service&#8217;, and I only list the WCF most important files:</p>
<ul>
<li><strong>IService.cs</strong> &#8211; Describes the exposed service interface. This is where we will namely define the <strong>[ServiceContract]</strong> (The service itself) and the <strong>[OperationContract]</strong>&#8216;s (The operations exposed by the service). More information in <a href="http://msdn2.microsoft.com/en-us/library/ms729746.aspx" target="_blank">Designing and Implementing Services</a>. </li>
<li><strong>Service.cs </strong>&#8211; The service interface implementation class. This is where the actual code of the service operations will be contained. More information in <a href="http://msdn2.microsoft.com/en-us/library/ms733764.aspx" target="_blank">Implementing Service Contracts</a>. </li>
</ul>
<p>There is another important file, <strong>App.Config</strong>, that contains the service configuration (e.g. service listening url), but I will refer to this file later when talking about the service hosting (creates the service and controls its context and lifetime). More information in <a href="http://msdn2.microsoft.com/en-us/library/ms730158.aspx" target="_blank">Hosting Services</a>.</p>
<p>So a simple service contract could be:</p>
<div>
<div>
<pre>[ServiceContract]</pre>
<pre><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IService</pre>
<pre>{</pre>
<pre>    [OperationContract]</pre>
<pre>    Int32 GetTotalNumberOfCustomers();</pre>
<pre>&#160;</pre>
<pre>    [OperationContract]</pre>
<pre>    Customer GetCustomer(Int32 customerId);</pre>
<pre>&#160;</pre>
<pre>    [OperationContract]</pre>
<pre>    List&lt;Customer&gt; GetCustomers();        </pre>
<pre>}</pre>
</p></div>
</div>
<p>This code defines a service contract (<strong>IService</strong>) that exposes three operations (operation functionality is self explanatory):</p>
<ul>
<li><strong>GetTotalNumberOfCustomers</strong> </li>
<li><strong>GetCustomer</strong> </li>
<li><strong>GetCustomers</strong> </li>
</ul>
<p>You will notice that in the code we are referring a custom class that is not contained anywhere in the service contract, that is the <strong>Customer </strong>class. This class must be described in WCF like a <strong>[DataContract] </strong>because it is a new, complex data type that WCF does not know how to serialize. More information in <a href="http://msdn2.microsoft.com/en-us/library/ms733127.aspx" target="_blank">Using Data Contracts</a>.</p>
<p>Our data contract is defined like this:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre>[DataContract]</pre>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Customer</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    [DataMember]</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> Int32 Id { get; set; }</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    [DataMember]</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> String Name { get; set; }</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> Customer()</pre>
<p><!--CRLF--></p>
<pre>    {</pre>
<p><!--CRLF--></p>
<pre>        Id = 0;</pre>
<p><!--CRLF--></p>
<pre>        Name = String.Empty;</pre>
<p><!--CRLF--></p>
<pre>    }</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>One important aspect of data contracts are the <strong>[DataMember]</strong> properties, that are properties to be exposed by the data contract.</p>
<p>&#160;</p>
<p>At this point we are ready to implement our service contract, that for this example is very simple, and obviously not production code:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Service : IService</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">private</span> List&lt;Customer&gt; Customers = <span style="color: #0000ff">new</span> List&lt;Customer&gt;()</pre>
<p><!--CRLF--></p>
<pre>    {</pre>
<p><!--CRLF--></p>
<pre>        <span style="color: #0000ff">new</span> Customer() {Id = 1, Name = <span style="color: #006080">&quot;Customer1&quot;</span> },</pre>
<p><!--CRLF--></p>
<pre>        <span style="color: #0000ff">new</span> Customer() {Id = 2, Name = <span style="color: #006080">&quot;Customer2&quot;</span> },</pre>
<p><!--CRLF--></p>
<pre>        <span style="color: #0000ff">new</span> Customer() {Id = 3, Name = <span style="color: #006080">&quot;Customer3&quot;</span> }</pre>
<p><!--CRLF--></p>
<pre>    };</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> GetTotalNumberOfCustomers()</pre>
<p><!--CRLF--></p>
<pre>    {</pre>
<p><!--CRLF--></p>
<pre>        <span style="color: #0000ff">return</span> Customers.Count;</pre>
<p><!--CRLF--></p>
<pre>    }</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> Customer GetCustomer(<span style="color: #0000ff">int</span> customerId)</pre>
<p><!--CRLF--></p>
<pre>    {</pre>
<p><!--CRLF--></p>
<pre>        <span style="color: #0000ff">return</span> Customers.First(customer =&gt; </pre>
<p><!--CRLF--></p>
<pre>        {</pre>
<p><!--CRLF--></p>
<pre>            <span style="color: #0000ff">return</span> customer.Id == customerId;</pre>
<p><!--CRLF--></p>
<pre>        });</pre>
<p><!--CRLF--></p>
<pre>    }</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">public</span> List&lt;Customer&gt; GetCustomers()</pre>
<p><!--CRLF--></p>
<pre>    {</pre>
<p><!--CRLF--></p>
<pre>        <span style="color: #0000ff">return</span> Customers;</pre>
<p><!--CRLF--></p>
<pre>    }</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p>Our service is now ready to be hosted and this is one of the beauties of WCF in the sense that the hosting environment can be a:</p>
<ul>
<li>Windows/WPF Application </li>
<li>Console application </li>
<li>Windows Service </li>
<li>IIS/WAS </li>
</ul>
<p>Each of them has advantages and disadvantages an again I refer you to <a href="http://msdn2.microsoft.com/en-us/library/ms730158.aspx" target="_blank">Hosting Services</a> (look at the Choosing a Hosting Environment table). Take into attention that the Service Library that you develop is always the same regardless of the hosting environment that you choose, and even if you choose one hosting environment now, you can choose another later and easily change between them.</p>
<p>For developing/debugging purposes I normally choose for hosting a simple WPF application with just two buttons (Start and Stop), where I place the following code (the <strong>Logger</strong> object is of log4net <strong>ILog</strong> type):</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> StartServiceHost()</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    Logger.Info(<span style="color: #006080">&quot;StartServiceHost: Starting service hosting...&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Create service host</span></pre>
<p><!--CRLF--></p>
<pre>    _serviceHost = <span style="color: #0000ff">new</span> ServiceHost(<span style="color: #0000ff">typeof</span>(SampleService.Service));</pre>
<p><!--CRLF--></p>
<pre>    </pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">try</span></pre>
<p><!--CRLF--></p>
<pre>    { <span style="color: #008000">// Open service host</span></pre>
<p><!--CRLF--></p>
<pre>        _serviceHost.Open();</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>        Logger.Info(<span style="color: #006080">&quot;StartServiceHost: Service hosting success.&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre>    }</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #0000ff">catch</span> (Exception ex)</pre>
<p><!--CRLF--></p>
<pre>    {</pre>
<p><!--CRLF--></p>
<pre>        Logger.Fatal(<span style="color: #006080">&quot;StartServiceHost: Could not start service hosting.&quot;</span>, ex);</pre>
<p><!--CRLF--></p>
<pre>    }            </pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<div>&#160;</div>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> StopServiceHost()</pre>
<p><!--CRLF--></p>
<pre>{</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000">// Close service host</span></pre>
<p><!--CRLF--></p>
<pre>    _serviceHost.Close();</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></div>
</div>
<p>Now, you just have to properly configure the application configuration file (remember we talked about it earlier), and you are ready to test your service. Note that I configured the service hosting all thru the <strong>App.Config </strong>file and as simple and compatible as possible with a ASMX (e.g. Basic HTTP binding).</p>
<p>Note: I will differ binding&#8217;s and other configuration explanations to another time. Right now I just want you to see how you can get started with WCF.</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre>&lt;configuration&gt;</pre>
<p><!--CRLF--></p>
<pre>    &lt;system.serviceModel&gt;</pre>
<p><!--CRLF--></p>
<pre>        &lt;services&gt;</pre>
<p><!--CRLF--></p>
<pre>        &lt;service behaviorConfiguration=<span style="color: #006080">&quot;SampleService.Service.Behavior&quot;</span> name=<span style="color: #006080">&quot;SampleService.Service&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre>            &lt;host&gt;</pre>
<p><!--CRLF--></p>
<pre>                &lt;baseAddresses&gt;</pre>
<p><!--CRLF--></p>
<pre>                    &lt;add baseAddress=<span style="color: #006080">&quot;http://localhost:8080/SampleService&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre>                &lt;/baseAddresses&gt;</pre>
<p><!--CRLF--></p>
<pre>            &lt;/host&gt;</pre>
<p><!--CRLF--></p>
<pre>            &lt;endpoint    address=<span style="color: #006080">&quot;/Service.svc&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                        binding=<span style="color: #006080">&quot;basicHttpBinding&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                        name=<span style="color: #006080">&quot;IService_BasicHttpEndpoint&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                        contract=<span style="color: #006080">&quot;SampleService.IService&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                        bindingConfiguration=<span style="color: #006080">&quot;IService_BasicHttpBindingConfiguration&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre>            &lt;/endpoint&gt;</pre>
<p><!--CRLF--></p>
<pre>        &lt;/service&gt;</pre>
<p><!--CRLF--></p>
<pre>    &lt;/services&gt;</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    &lt;behaviors&gt;</pre>
<p><!--CRLF--></p>
<pre>        &lt;serviceBehaviors&gt;</pre>
<p><!--CRLF--></p>
<pre>            &lt;behavior name=<span style="color: #006080">&quot;SampleService.Service.Behavior&quot;</span>&gt;</pre>
<p><!--CRLF--></p>
<pre>                &lt;!-- To avoid disclosing metadata information, </pre>
<p><!--CRLF--></p>
<pre>      set the <span style="color: #0000ff">value</span> below to <span style="color: #0000ff">false</span> and remove the metadata endpoint above before deployment --&gt;</pre>
<p><!--CRLF--></p>
<pre>                &lt;serviceMetadata httpGetEnabled=<span style="color: #006080">&quot;True&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre>                &lt;!-- To receive exception details <span style="color: #0000ff">in</span> faults <span style="color: #0000ff">for</span> debugging purposes, </pre>
<p><!--CRLF--></p>
<pre>      set the <span style="color: #0000ff">value</span> below to <span style="color: #0000ff">true</span>.  Set to <span style="color: #0000ff">false</span> before deployment </pre>
<p><!--CRLF--></p>
<pre>      to avoid disclosing exception information --&gt;</pre>
<p><!--CRLF--></p>
<pre>                &lt;serviceDebug includeExceptionDetailInFaults=<span style="color: #006080">&quot;True&quot;</span> /&gt;                </pre>
<p><!--CRLF--></p>
<pre>            &lt;/behavior&gt;</pre>
<p><!--CRLF--></p>
<pre>        &lt;/serviceBehaviors&gt;</pre>
<p><!--CRLF--></p>
<pre>    &lt;/behaviors&gt;</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>    &lt;bindings&gt;</pre>
<p><!--CRLF--></p>
<pre>        &lt;basicHttpBinding&gt;</pre>
<p><!--CRLF--></p>
<pre>            &lt;!-- Service host binding configuration --&gt;</pre>
<p><!--CRLF--></p>
<pre>            &lt;binding name=<span style="color: #006080">&quot;IService_BasicHttpBindingConfiguration&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                             maxBufferSize=<span style="color: #006080">&quot;65536&quot;</span> maxBufferPoolSize=<span style="color: #006080">&quot;52428&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                             maxReceivedMessageSize=<span style="color: #006080">&quot;65536&quot;</span> transferMode=<span style="color: #006080">&quot;Buffered&quot;</span>&gt;            </pre>
<p><!--CRLF--></p>
<pre>                &lt;readerQuotas maxDepth=<span style="color: #006080">&quot;32&quot;</span> maxArrayLength=<span style="color: #006080">&quot;16384&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                                            maxBytesPerRead=<span style="color: #006080">&quot;4096&quot;</span> maxNameTableCharCount=<span style="color: #006080">&quot;16384&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                                            maxStringContentLength=<span style="color: #006080">&quot;8192&quot;</span>/&gt;</pre>
<p><!--CRLF--></p>
<pre>            &lt;/binding&gt;</pre>
<p><!--CRLF--></p>
<pre>        &lt;/basicHttpBinding&gt;</pre>
<p><!--CRLF--></p>
<pre>    &lt;/bindings&gt;</pre>
<p><!--CRLF--></p>
<pre>    </pre>
<p><!--CRLF--></p>
<pre>&lt;/system.serviceModel&gt;</pre>
<p><!--CRLF--></p>
<pre>&#160;</pre>
<p><!--CRLF--></p>
<pre>&lt;configuration&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>If you now run the WPF application and point your browser to <a href="http://localhost:8080/SampleService" target="_blank">http://localhost:8080/SampleService</a> you should see a welcome page telling you that you have created a service.</p>
<p>You are now ready to start writing a client application that will call your service operation contracts (my next post will be about this part, but you have everything you need in the service welcome page).</p>
<p>To end, I just want to show you how easy it is to change the hosting environment to IIS for example. For hosting in IIS you just have to follow these steps:</p>
<ul>
<li>Create a new web application in IIS </li>
<li>Place the presented <strong>App.Config</strong> &lt;system.serviceModel&gt; tag in a <strong>Web.Config</strong> file </li>
<li>Deploy the service library assembly to the web application bin directory </li>
<li>Create a special file, in my example called <strong>Service.svc</strong>, with the following content: </li>
</ul>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre>&lt;%@ServiceHost language=<span style="color: #006080">&quot;C#&quot;</span> Debug=<span style="color: #006080">&quot;true&quot;</span></pre>
<p><!--CRLF--></p>
<pre>                             Service=<span style="color: #006080">&quot;SampleService.Service&quot;</span>%&gt;</pre>
<p><!--CRLF--></p>
<pre>&lt;%@Assembly Name=<span style="color: #006080">&quot;SampleService&quot;</span> %&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>The only different part in hosting in IIS is just the need to create the <strong>.svc</strong> file that tells IIS which service should be instantiated.</p>
<p>Again, if you point your browser to <a href="http://localhost:8080/SampleService/Service.svc" target="_blank">http://localhost:8080/SampleService/Service.svc</a> you should see the same page as before.</p>
<p>I hope that this can get you started in WCF more easily and I will try to show what other capabilities WCF provides in some next posts.</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/03/30/developing-a-wcf-service/">Developing a WCF Service</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/marcosilva/2008/03/30/developing-a-wcf-service/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Log4net Initialization/Configuration alternatives</title>
		<link>https://blogit.create.pt/marcosilva/2008/02/24/log4net-initializationconfiguration-alternatives/</link>
					<comments>https://blogit.create.pt/marcosilva/2008/02/24/log4net-initializationconfiguration-alternatives/#respond</comments>
		
		<dc:creator><![CDATA[Marco Silva]]></dc:creator>
		<pubDate>Sun, 24 Feb 2008 11:24:06 +0000</pubDate>
				<category><![CDATA[Debug]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Log]]></category>
		<category><![CDATA[Framework]]></category>
		<guid isPermaLink="false">http://blogcreate.azurewebsites.net/marcosilva/?p=171</guid>

					<description><![CDATA[<p>In a previous post where I talked about using log4net I used one of the alternative ways to initialize and configure the logging services. This post will summarize these alternatives. &#160; 1. Programmatic initialization The programmatic initialization should be done somewhere in your code. This was the type of initialization used on the first post [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/02/24/log4net-initializationconfiguration-alternatives/">Log4net Initialization/Configuration alternatives</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In a previous <a href="http://blogit.create.pt/blogs/marcosilva/archive/2007/07/29/Using-log4net-logging-services.aspx">post</a> where I talked about using log4net I used one of the alternative ways to initialize and configure the logging services.</p>
<p>This post will summarize these alternatives.</p>
<p>&#160;</p>
<h2>1. Programmatic initialization</h2>
<p><font size="1"><span style="font-family: courier new,courier"></span></font></p>
<p>The programmatic initialization should be done somewhere in your code.</p>
<p>This was the type of initialization used on the first post and its code is something like this.</p>
<p><font size="1"><span style="font-family: courier new,courier">using log4net;</span></font></p>
<p><font size="1"><span style="font-family: courier new,courier">using log4net.Config;</span></font></p>
<p><font size="1"><span style="font-family: courier new,courier"></span></font></p>
<p><font size="1"><span style="font-family: courier new,courier">public class MyApp </span>      <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">{</span>       <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160; private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));</span>       <br style="font-family: courier new,courier">      <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160; static void Main(string[] args) </span>      <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160; {</span>       <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160;&#160;&#160;&#160;&#160; // BasicConfigurator replaced with XmlConfigurator.</span>       <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160;&#160;&#160;&#160;&#160; XmlConfigurator.Configure(new System.IO.FileInfo(args[0]));</span></font></p>
<p><font size="1"><span style="font-family: courier new,courier"></span></font><font size="1"></font></p>
<p><font size="1"></font></p>
<p><font size="1"><font size="1">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font face="Courier New">&#160; log.Info(&quot;Entering application.&quot;);</font></font></font></p>
<p><font size="1"><span style="font-family: courier new,courier">&#160;&#160;&#160; }</span></font></p>
<p><font size="1"><span style="font-family: courier new,courier"></span></font><font size="1"><span style="font-family: courier new,courier">} </span></font></p>
<p><font size="1"><span style="font-family: courier new,courier"></span></font></p>
<h2>2. Assembly attribute initialization</h2>
<p>The assembly attribute initialization does not imply the inclusion of any code in your application. Instead, you just have to place an assembly level attribute in some file of your assembly. This way your code could be:</p>
<p><font size="1"><span style="font-family: courier new,courier">using log4net;</span></font></p>
<p><font face="Courier New" size="1">[assembly: log4net.Config.XmlConfigurator()]</font></p>
<p><font size="1"><span style="font-family: courier new,courier">public class MyApp </span>      <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">{</span>       <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160; private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));</span>       <br style="font-family: courier new,courier">      <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160; static void Main(string[] args) </span>      <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160; {</span>       <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><font face="Courier New">log.Info(&quot;Entering application.&quot;);<span style="font-family: courier new,courier"></span></font>       <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&#160;&#160;&#160; }</span>       <br style="font-family: courier new,courier"><span style="font-family: courier new,courier">} </span></font></p>
<p>&#160;</p>
<p>The assembly level attribute by itself does not mean a instruction to initialize the logging framework. Instead, the logging framework is initialized by the first call that is made to log4net (in the example the <font face="Courier New" size="1">log.Info<font face="Verdana" size="2">) initialized. This means that when using assembly attributes you should make a logging call as soon as possible and before calling any other external assembly.</font></font></p>
<p>&#160;</p>
<p>Finally, if you are using logger objects defined per-class, a simple way to define a logger object that enables it to be copy-pasted in a very simple way is to do:</p>
<p><font face="Courier New" size="1">private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);</font></p>
<p><p>&#160;</p>
<p>This instruction uses reflection for determining the class type name that will be used in the logging messages.</p>
<p>For more information please refer to the log4net <a href="http://logging.apache.org/log4net" target="_blank">homepage</a>.</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2008/02/24/log4net-initializationconfiguration-alternatives/">Log4net Initialization/Configuration alternatives</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/marcosilva/2008/02/24/log4net-initializationconfiguration-alternatives/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Using log4net logging services</title>
		<link>https://blogit.create.pt/marcosilva/2007/07/29/using-log4net-logging-services/</link>
					<comments>https://blogit.create.pt/marcosilva/2007/07/29/using-log4net-logging-services/#respond</comments>
		
		<dc:creator><![CDATA[Marco Silva]]></dc:creator>
		<pubDate>Sun, 29 Jul 2007 11:22:00 +0000</pubDate>
				<category><![CDATA[Debug]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Audit]]></category>
		<category><![CDATA[Log]]></category>
		<category><![CDATA[Framework]]></category>
		<guid isPermaLink="false">http://blogcreate.azurewebsites.net/marcosilva/?p=291</guid>

					<description><![CDATA[<p>Whenever you have the need for debugging some application that by whatever reason cannot be properly debugged in Visual Studio, or even because your application requires some kind of logging/auditing functionality, instead of developing your own tool you have the option to use some existent logging frameworks. One that I have tried and enjoyed its [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2007/07/29/using-log4net-logging-services/">Using log4net logging services</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><font size="2">Whenever you have the need for debugging some application that by whatever reason cannot be properly debugged in Visual Studio, or even because your application requires some kind of logging/auditing functionality, instead of developing your own tool you have the option to use some existent logging frameworks.</font></p>
<p><font size="2">One that I have tried and enjoyed its features and ease of use is log4net &#8211; <a href="http://logging.apache.org/log4net/" title="http://logging.apache.org/log4net/" target="_blank">http://logging.apache.org/log4net/</a>.</font></p>
<p><font size="2">Like they say on their webpage, &quot;<span style="font-style: italic">log4net is a tool to help the programmer output log statements to a variety<br />
                    of output targets. log4net is a port of the excellent log4j framework to the<br />
                    .NET runtime</span>&quot;.  Its features are present here: <a href="http://logging.apache.org/log4net/release/features.html" title="http://logging.apache.org/log4net/release/features.html" target="_blank">http://logging.apache.org/log4net/release/features.html.</a></font></p>
<p><font size="2">One of its great features is the ability to control and configure the logging framework entirely by an XML file. This allows the building and deployment of applications that at run-time can dynamically change its logging configuration. For this you should initialize to log in the following manner (in the example the XML configuration file is received in the application arguments):<br /></font></p>
<p><font size="1"><span style="font-family: courier new,courier">// Import log4net classes.</span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">using log4net;</span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">using log4net.Config;</span><br style="font-family: courier new,courier"><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">public class MyApp </span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">{</span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&nbsp;&nbsp;&nbsp; private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));</span><br style="font-family: courier new,courier"><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&nbsp;&nbsp;&nbsp; static void Main(string[] args) </span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&nbsp;&nbsp;&nbsp; {</span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // BasicConfigurator replaced with XmlConfigurator.</span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlConfigurator.Configure(new System.IO.FileInfo(args[0]));</span><span style="font-family: courier new,courier">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: courier new,courier"></span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">&nbsp;&nbsp;&nbsp; }</span><br style="font-family: courier new,courier"><span style="font-family: courier new,courier">} </span></font></p>
<p>After this, you can check if the initialization went ok by testing the value of: <font size="1"><span style="font-family: courier new,courier">_log.Logger.Repository.Configured .</span></font></p>
<p>Now it is real easy to start logging things. You just have to do:</p>
<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <font size="1"><span style="font-family: courier new,courier">log.Info(&quot;Entering application.&quot;);</span><br style="font-family: courier new,courier"><br />
<span style="font-family: courier new,courier">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; log.Debug(&quot;Entering application.&quot;);</span></font></p>
<p>and the framework takes care of the rest, logging acording to the configuration that you provided. Note that in the above code I am already logging into two different log levels, just as an example for showing you, that while developing I can have the level configured at DEBUG, and at&nbsp;production I can reduce the level to INFO, or even NONE, if I dont want any logging at all. If a problem occurs in production, I can just as easily change the log level to DEBUG again, and without having to change anything in the application, the logs start being generated.&nbsp;</p>
<p>As a final example of the capabilities of the framework, I just want to show here a possible XML configuration file:</p>
<p style="font-family: courier new,courier"><font size="1">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;<br />&lt;log4net&gt;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;!&#8211; Rolling File Appender &#8211;&gt;<br />&nbsp;&nbsp;&nbsp; &lt;appender name=&quot;RollingFileAppender&quot; type=&quot;log4net.Appender.RollingFileAppender&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;file value=&quot;log-file.txt&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;appendToFile value=&quot;true&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;maximumFileSize value=&quot;1KB&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;maxSizeRollBackups value=&quot;0&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;staticLogFileName value=&quot;true&quot; /&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;conversionPattern value=&quot;%date [%thread] %-5level %logger &#8211; %message%newline&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/layout&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/appender&gt;</p>
<p>&nbsp;&nbsp;&nbsp; &lt;!&#8211; Console appender &#8211;&gt;<br />&nbsp;&nbsp;&nbsp; &lt;appender name=&quot;ConsoleAppender&quot; type=&quot;log4net.Appender.ConsoleAppender&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;target value=&quot;Console.Error&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;conversionPattern value=&quot;%message%newline&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/layout&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/appender&gt;<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;root&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;level value=&quot;DEBUG&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&lt;appender-ref ref=&quot;RollingFileAppender&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&lt;appender-ref ref=&quot;ConsoleAppender&quot; /&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/root&gt;&nbsp;&nbsp;&nbsp; <br />&lt;/log4net&gt;&nbsp;</font></p>
<p>In this configuration, I define two different appenders (RollingFile and Console), each with its own configuration. At the root level, I define the logging level at DEBUG (so I want to see everything until this level), and that the logging should be done to the two defined appenders. So, when I log something with this configuration, the log is done to a file and to the application&#039;s console.</p>
<p>So, you see that it is easy to add logging capabilities to your application.&nbsp;</p>
<p>The post <a href="https://blogit.create.pt/marcosilva/2007/07/29/using-log4net-logging-services/">Using log4net logging services</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/marcosilva/2007/07/29/using-log4net-logging-services/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
