<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>WCF - SeeSharp</title>
        <description>A C# development blog</description>
        <link>/Tags/WCF/RSS</link>
        <language>en</language>
        <image>
            <url>http://www.hightech.ir/favicon.png</url>
            <title>SeeSharp</title>
            <link>/Tags/WCF/RSS</link>
            <width>64</width>
            <height>64</height>
        </image>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>CrossDomain Problem with Silverlight Applications</title>
            <description>&lt;p&gt;
	When creating Line of Business (LoB) applications with Silverlight, the only choice to get the data to the application is through webservices. People having their Silverlight application hosted on the same webserver as the service have almost no problem, but things get interesting when service is not hosted on the same machine as the application, and it gets hairy when you &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/bb332338.aspx#msdnwcfhc_topic3&quot;&gt;self-host a WCF&lt;/a&gt; service. Let&amp;#39;s see how it is done in each scenario.&lt;/p&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;&lt;br /&gt;
	Background&lt;/span&gt;&lt;br /&gt;
	Generally, RIA applications including &lt;a href=&quot;http://www.adobe.com/products/air/&quot;&gt;Adobe AIR&lt;/a&gt; or &lt;a href=&quot;http://www.microsoft.com/silverlight/&quot;&gt;Microsoft Silverlight&lt;/a&gt; due to certain considerations, won&amp;#39;t let your application use a remote service, if the service does not allow you to connect explicitly. This is done through a Client Policy file that should be available at the root of the webserver running the service. The client policy file, which is an XML document, grants the client permission to connect to the service. For example, if your webservice is running on a domain, e.g.&lt;/p&gt;
&lt;pre&gt;
&lt;span style=&quot;font-size: 14px;&quot;&gt;http://www.hightech.ir/OrderingService&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
	your client policy file should be located at the root of the same domain, for example for a Silverlight application using the above service, it&amp;#39;d be located at:&lt;/p&gt;
&lt;pre&gt;
&lt;span style=&quot;font-size: 14px;&quot;&gt;http://www.hightech.ir/clientaccesspolicy.xml&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
	For more information about this file and content of the file, you can check out the specification &lt;a href=&quot;http://www.senocular.com/pub/adobe/crossdomain/policyfiles.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;&lt;br /&gt;
	Web hosted Web Service&lt;/span&gt;&lt;br /&gt;
	This is the easiest of them all. Since your webservice is hosted in a webserver, you just need to create a xml file and place it at the root of your webserver. To make sure it works, open the address to the Client Policy file in your web browser. This works painlessly for WCF services using basic HTTP binding that are hosted on IIS.&lt;/p&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;&lt;br /&gt;
	JBoss hosted JAX-WS Service&lt;/span&gt;&lt;br /&gt;
	Since &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_API_for_XML_Web_Services&quot;&gt;JAX-WS&lt;/a&gt; services comply with SOAP standards, thse services are considered regular http webservices, so if you have read my &lt;a href=&quot;http://www.hightech.ir/SeeSharp/Silverlight-J2EE-Backend-Part-One&quot;&gt;series&lt;/a&gt; describing how you can use JBoss to host your services and create a Silverlight rich client to consume those services, you already have figured out the basics.&lt;/p&gt;
&lt;p&gt;
	But JBoss is an application server...How can I add a xml file to the root of the &amp;quot;application server&amp;quot;? JBoss comes with a built-in &lt;a href=&quot;http://httpd.apache.org/&quot;&gt;apache webserver&lt;/a&gt; and there is a special folder in your JBoss installation that is considered the webserver root. So just like IIS where you copied your file to the root of the webserver, copy the client policy file into:&lt;/p&gt;
&lt;pre&gt;
&lt;span style=&quot;font-size: 14px;&quot;&gt;%JBoss Folder%/server/%installation%/deploy/jboss-web.deployer/ROOT.war/&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
	The above path translates into this on my machine:&lt;/p&gt;
&lt;pre&gt;
&lt;span style=&quot;font-size: 14px;&quot;&gt;C:\Java\JBoss-4.2.3.GA\server\default\deploy\jboss-web.deployer\ROOT.war\&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
	&lt;span class=&quot;subtitle&quot;&gt;&lt;br /&gt;
	Self-Hosted WCF Service&lt;/span&gt;&lt;br /&gt;
	It gets pretty interesting when you&amp;#39;re hosting the WCF services yourself. When it comes to hosting the WCF service, it is possible to use a Console Application (or much better a Windows Service) to host the services for you. This way, you just have a Windows service or an application, which both are way different than a webserver. Where should you place the client policy file now?&lt;/p&gt;
&lt;address&gt;
	Note: There are many benefits that comes free when hosting the WCF service in IIS, so self-host the services in case you have a good reason to.&lt;br /&gt;
	&amp;nbsp;&lt;/address&gt;
&lt;p&gt;
	To return the client policy file when a request comes through you need to create a special Service Host that does just that. It is mandatory to use WebHttpBinding to make this work. Then you can create a WCF service that returns the content of the client policy. Here&amp;#39;s an example:&lt;/p&gt;
&lt;pre class=&quot;brush:c-sharp&quot;&gt;
[ServiceContract(Namespace = ServiceSettings.Config.GeneralNamespace)]
public interface ICrossDomainService
{
    [OperationContract]
    [WebGet(UriTemplate = &amp;quot;/clientaccesspolicy.xml&amp;quot;)]
    Stream ProvidePolicyFile();
}

public class CrossDomainService : ICrossDomainService
{
    public Stream ProvidePolicyFile()
    {
        string result = @&amp;quot;&amp;lt;?xml version=&amp;quot;&amp;quot;1.0&amp;quot;&amp;quot; encoding=&amp;quot;&amp;quot;utf-8&amp;quot;&amp;quot;?&amp;gt;
&amp;lt;access-policy&amp;gt;
&amp;lt;cross-domain-access&amp;gt;
    &amp;lt;policy&amp;gt;
        &amp;lt;allow-from http-request-headers=&amp;quot;&amp;quot;*&amp;quot;&amp;quot;&amp;gt;
            &amp;lt;domain uri=&amp;quot;&amp;quot;*&amp;quot;&amp;quot;/&amp;gt;
        &amp;lt;/allow-from&amp;gt;
        &amp;lt;grant-to&amp;gt;
            &amp;lt;resource path=&amp;quot;&amp;quot;/&amp;quot;&amp;quot; include-subpaths=&amp;quot;&amp;quot;true&amp;quot;&amp;quot;/&amp;gt;
        &amp;lt;/grant-to&amp;gt;
    &amp;lt;/policy&amp;gt;
&amp;lt;/cross-domain-access&amp;gt;
&amp;lt;/access-policy&amp;gt;&amp;quot;;
        return ConvertToStream(result);
    }

    private Stream ConvertToStream(string result)
    {
        WebOperationContext.Current.OutgoingResponse.ContentType = &amp;quot;application/xml&amp;quot;;
        return new MemoryStream(Encoding.UTF8.GetBytes(result));
    }
}
&lt;/pre&gt;
&lt;p&gt;
	Did you notice the extra WebGet attribute beside the ServiceContract? Specifying the UriTemplate will do the trick and will send the hard-coded content of the client policy when a request at specified Uri comes through.&lt;/p&gt;
&lt;address&gt;
	Note: Make sure you don&amp;#39;t mix up these two bindings. For WCF service to be consumable in Silverlight client, you need to have a BasicHttpBinding but for the client access content binding should be WebHttpBinding.&lt;/address&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/CrossDomain-Problem-With-Silverlight-Applications</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/CrossDomain-Problem-With-Silverlight-Applications</guid>
            <pubDate>Tue, 18 May 2010 00:00:00 GMT</pubDate>
            <category>Silverlight</category>
            <category>WCF</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Silverlight + RESTful POX</title>
            <description>&lt;p&gt;I'm working on a small project for a Swedish company. They need their application be available via web, so it's time to put that Silverlight knowledge at work. I decided to use Silverlight for UI technology and have a REST service layer to provide the required data. After all Silverlight and REST services should have no problem. Silverlight experience was great. You have LINQ at your disposal to process the REST results and although the set of available commands in Silverlight is less than WPF, but this makes it is easier to learn and work with at the same time. There was a couple of gatchas along the way, so I'm writing this as a note-to-self.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;span class=&quot;subtitle&quot;&gt;Service Contract vs. Object Model&lt;/span&gt;
&lt;p&gt;The domain model is very simple and consist of a couple of entities, so what REST Services expose as data contracts are more or less the same as what I use in application layer. Silverlight's class library is not the same as a normal class library so I can not reuse the domain model available in my Silverlight library. This will lead to two set of object models. One for the domain model and the other for the service layer as data contracts. This separation or concern is a good idea, but for this small application it is overkill, but there's nothing I can do with it. With the help of LINQ2XML the best choice for exposing the contract model is POX (Plain old XML) so result of our services would be in XML parse by LINQ and converted to our domain model.&lt;/p&gt;&lt;br /&gt;

&lt;span class=&quot;subtitle&quot;&gt;Unit Testing&lt;/span&gt;
&lt;p&gt;Also because of different class library outputs, you can not use unit testing frameworks the way you usually do. This means you can not use xUnit, nUnit, etc. Hopefully there's a test harness to allow both UI and API level testing when developing Silverlight and it works just like MSTest does. Download the binary files &lt;a href=&quot;http://www.microsoft.com/downloads/details.aspx?FamilyId=EA93DD89-3AF2-4ACB-9CF4-BFE01B3F02D4&amp;amp;displaylang=en&quot;&gt;here&lt;/a&gt; and read &lt;a href=&quot;http://www.jeff.wilcox.name/2008/03/silverlight2-unit-testing/&quot;&gt;this&lt;/a&gt; to know how to add templates to your VS.NET to easily create a test application.&lt;/p&gt;&lt;br /&gt;

&lt;span class=&quot;subtitle&quot;&gt;RESTful POX&lt;/span&gt;
&lt;p&gt;To achieve this the service contract is decorated with REST attributes, That is a WebGetAttribute if you're exposing the operation via GET verb or a WebInvokeAttribute if you're exposing it via POST.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Note : The thing was that exposing &lt;strong&gt;IList&amp;lt;T&amp;gt;&lt;/strong&gt; as a returned result of a service resulted a non functioning service with the following message : &quot;Request Error : The server encountered an error processing the request. See server logs for more details.&quot; It turned out exposing the &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt; worked. The reason behind this is probably the same thing as exposing an object&#226;€™s interface via normal (Soap) WCF service which also doesn't work due to interfaces not being serializable.&lt;br /&gt;&lt;br /&gt;&lt;/em&gt;So here's the service contract :&lt;br /&gt;&lt;/p&gt;

&lt;pre class=&quot;brush:c-sharp&quot;&gt;
[ServiceContract]
public interface IDataServices
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = &quot;/Advertisers&quot;)]
    List&amp;lt;Advertiser&amp;gt; GetAdvertisers();
}
&lt;/pre&gt;

&lt;p&gt;and the implementation is self explanatory. What you get is a service exposed at a URL like &lt;a title=&quot;http://localhost:9999/Services/DataServices.svc/Advertisers&quot; href=&quot;http://localhost/Services/DataServices.svc/Advertisers&quot;&gt;http://localhost/Services/DataServices.svc/Advertisers&lt;/a&gt;. In order to get rid of the nasty .svc extension you either need to use IIS URL Rewriting Module which is only available on IIS 7 or use IIS Wildcard Mapping plus a HttpModule to make this happen. Read &lt;a href=&quot;http://www.west-wind.com/weblog/posts/570695.aspx&quot;&gt;here&lt;/a&gt; for complete information.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Note : The .svc extension does not matter from operational perspective. It is computer applications that connect to these URLs after all, but from purists point of view it does matter.&lt;br /&gt;&lt;br /&gt;&lt;/em&gt;The final part is the configuration which is standard WCF configuration like identifying service and contract, exposing EndPoints, etc. :&lt;br /&gt;&lt;/p&gt;

&lt;pre class=&quot;brush:xml&quot;&gt;
&amp;lt;system.serviceModel&amp;gt;
   &amp;lt;serviceHostingEnvironment aspNetCompatibilityEnabled=&quot;true&quot; /&amp;gt;
   &amp;lt;behaviors&amp;gt;
     &amp;lt;endpointBehaviors&amp;gt;
       &amp;lt;behavior name=&quot;WebBehavior&quot;&amp;gt;
         &amp;lt;webHttp/&amp;gt;
       &amp;lt;/behavior&amp;gt;
     &amp;lt;/endpointBehaviors&amp;gt;
   &amp;lt;/behaviors&amp;gt;

   &amp;lt;services&amp;gt;
     &amp;lt;service name=&quot;Reklam.UI.Web.Services.DataServices&quot;&amp;gt;
       &amp;lt;endpoint behaviorConfiguration=&quot;WebBehavior&quot;
                 binding=&quot;webHttpBinding&quot;
                 contract=&quot;Reklam.UI.Web.Services.IDataServices&quot; /&amp;gt;
     &amp;lt;/service&amp;gt;
   &amp;lt;/services&amp;gt;
&amp;lt;/system.serviceModel&amp;gt;
&lt;/pre&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/silverlight-restful-pox</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/silverlight-restful-pox</guid>
            <pubDate>Wed, 28 Jan 2009 15:45:00 GMT</pubDate>
            <category>Silverlight</category>
            <category>WCF</category>
            <category>REST</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>Transferring large DataSets with WCF</title>
            <description>I was working on an application which transfered a large amount of data in a DataSet object to a client. Everything worked fine for small amounts of data, but as soon as the DataSet got bigger than 100 rows, client didn't get the message sent by the server anymore!&lt;br /&gt;&lt;br /&gt;It took some time to figure out what the problem was. If you're transferring a large amount of data and having the same problem you may need to do two things :&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;You should set the MaxReceivedMessageSize on your binding to a larger value. If the message sent by the client/server exceeds this value, they will not recieve it. Use this value cautiously because setting it a value larger than the data you can load into your RAM could at least crash the application, and also you might expose your server to Denial of Service attacks.&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;You might also need to set the MaxArrayLength property of the ReaderQuotas object on your client/server binding, especially if you recieve serialization exceptions when the data is being serialized to be sent to client/server. Remember to add a reference to System.Runtime.Serialization in case you need to do this.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Here's how you can do this :&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;          NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, true);&lt;br /&gt;          binding.MaxReceivedMessageSize = 10485760;&lt;br /&gt;          binding.ReaderQuotas.MaxArrayLength = 10485760;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Also, if you're transferring data objects like large files, audio or video data, etc. you may consider using streaming message mode of WCF. In this mode, the large message is split into chunks and each chunk is loaded into memory and sent through the service. For more info on this see http://msdn2.microsoft.com/en-us/library/ms731913.aspx.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-3844837663518431320?l=heskandari.blogspot.com'/&gt;&lt;/div&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/transferring-large-datasets-with-wcf</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/transferring-large-datasets-with-wcf</guid>
            <pubDate>Sat, 16 Feb 2008 10:06:00 GMT</pubDate>
            <category>NET</category>
            <category>WCF</category>
        </item>
        <item>
            <dc:creator>HEskandari</dc:creator>
            <title>WCF Shortcomings and Dark Sides</title>
            <description>I've been busy for last months, working on not-much-new-anymore, .NET 3.0 foundations, with more focus on WPF and WCF.&lt;br /&gt;&lt;br /&gt;Here are some drawbacks and shortcomings I encountered when testing the WCF :&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;I tried getting a DataSet containing 100 data rows from a service, using TCP binding with reliable session. When the data was bigger than a few KiloBytes, client didn't recieve the message from the service, and because of Reliable Session, service kept sending the results again and again in specific time intervals!&lt;br /&gt;&lt;br /&gt;It turned out that there's a setting to specify the maximum message size client can recieve. Client will NOT receive messages larger than that, and what made me wonder is how low the default value is!&lt;/li&gt;&lt;br&gt;&lt;li&gt;There's no way to configure the service client/server from external configuration file or other means, e.g. from database, xml document, etc! You only get two options : configure from code, or application/web config file. Back in remoting days, we could pass a configuration file to the remoting subsystem and let the system configure itself from that file. This is VERY annoying specially for service clients where the application configuration is a bad idea to use if you have curious users running your application! Altough, there's a workaround to use an external configuration file on the client, there's none for the service side. If you're intrested see &lt;a href=&quot;http://weblogs.asp.net/cibrax/archive/2007/10/19/loading-the-wcf-configuration-from-different-files-on-the-client-side.aspx&quot;&gt;here&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;br&gt;&lt;li&gt;You need a ServiceHost to host a service in self-hosting mode. A service host can not host multiple services. Although, you can use multiple service hosts, but I guess it could be provided out of the box in WCF.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;As you see, drawbacks (so far) is very small comparing to all you get using WCF. In case you have something you think is missing from WCF, you can send your feedback to microsoft at their connect site : &lt;a href=&quot;https://connect.microsoft.com/wcf/&quot;&gt;https://connect.microsoft.com/wcf/&lt;/a&gt; Hopefully, they'll consider it if there's enough votes.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-7336288647567174024?l=heskandari.blogspot.com'/&gt;&lt;/div&gt;</description>
            <link>http://www.hightech.ir/SeeSharp/wcf-shortcomings-and-dark-sides</link>
            <guid isPermaLink="true">http://www.hightech.ir/SeeSharp/wcf-shortcomings-and-dark-sides</guid>
            <pubDate>Tue, 05 Feb 2008 11:18:00 GMT</pubDate>
            <category>NET</category>
            <category>WCF</category>
        </item>
    </channel>
</rss>
