Rants Tagged with “WCF”
1 (Total Pages: 1/Total Results: 8)
Today I was working with a client and ran into a problem I didn't expect. This particular problem had to do with Silverlight consuming a WCF Service. Sometimes WCF causes me to spew four letter words. There is a class of WCF problems that cause this: connection rejection. WCF has been designed to prevent common DDoS and other attacks that could cause servers to fail or at least not serve honest requests. To this end the default size of a request is quite small. In fact, its usually 64K in size. This size is fine for most every request but occassionally when a client wants to send a collection of things to the server this size is too small. But we'll get to that in a minute. First, some background...
This particular client has a large service that's been working for quite a while, then suddenly a single call he has was being rejected. The new service call he was making was sending back a list of a couple of hundred of POCO classes. He had been downloading large lists before so on the surface this should have worked. But it didn't.
The cause was that the default WCF limits were rejecting the connection. This type of error where its just failing, even Fiddler isn't helpful. There is no return code, the connection is just severed. Debugging it requires some trial and error which sucks. The calls that were returning a lot of data were working fine because the default ClientConfig file looks like this:
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_MyService">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<extendedProtectionPolicy policyEnforcement="Never" />
</httpTransport>
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://127.0.0.1.:8000/MyService.svc"
binding="customBinding"
bindingConfiguration="CustomBinding_MyService"
contract="MyServices.MyService"
name="CustomBinding_MyService" />
</client>
</system.serviceModel>
</configuration>
The maxReceivedMessageSize and maxBufferSize both accept these large responses from the server. But the reverse isn't true. The server's defaults are about 64K. Since we got it working, I wanted to show what we did so maybe others could use these server settings to address it.
The trick is to change the customBinding in the web.config to use larger defaults. I picked 2MB as it its a reasonable size. Of course setting them to 2GB like shown above will work but it does leave you more vulnerable to attacks. Pick a size that isn't larger than your largest request but isn't overly large. Its a guessing game. To set these, you need to add them to your web.config is to put them on the httpTransport node:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TestLargeWCF.Web.MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2097152"
maxBufferSize="2097152"
maxBufferPoolSize="2097152" />
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="Web.MyServiceBehavior"
name="TestLargeWCF.Web.MyService">
<endpoint address=""
binding="customBinding"
bindingConfiguration="customBinding0"
contract="TestLargeWCF.Web.MyService"/>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
That's the trick. Hope it helps you not waste your day like I wasted mine ;)
If you've never had the chance to visit my sister site (http://www.silverlightdata.com), now's a good time. I've updated my examples there to include my MVVM, Prism and Declarative UI examples (to go with the skinning/switchable Astoria example). Take a look if you're doing Silverlight data-based applications.
If you have spent anytime with Silverlight, you've likely run across the cross-site scripting issue. Essentially, the browser doesn't let you do web requests from other sites than the one you're hosted in. This is to prevent nasty script kiddies from doing nefarious things.
While I hope that Microsoft solves this in the way that Flash does (essentially a white-list that is located on the server that says what sites are ok), I do suggest a workaround: proxy calls offsite through your server. You can create a simple service on your site that returns data from another site. Then in Silverlight its a matter of making a request up to your own server to get the data and work with it in whatever way you want.
Luckily with .NET 3.5 and WCF's new REST stack, this is really easy. For example, here is a simple WCF service using the new WebGet attribute to specify that it can be called like a REST service:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(
RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
// Add [WebGet] attribute to use HTTP GET
[WebGet(ResponseFormat=WebMessageFormat.Xml)]
[OperationContract]
public XElement DoWork()
{
return XDocument.Load("http://wildermuth.com/rss").Root;
}
}
The trick here is to add the WebGet attribute to your method. Note I am specifying that I want XML (JSON is the default) so I can get the data back to Silverlight as XML. As a return type I am specifying XElement (XDocument may make more sense but its not Serializable) so we load a XDocument and just return the root of the document. Voila, a service you can call from Silverlight to call out to another service.
I could have changed this to accept a parameter with the request to make and I didn't do this on purpose. You can imagine if you leave an open relay like that open, you're inviting script kiddies to do nasty things.
What do you think?
I am delving into WCF and AJAX (not at the same time) lately so I wanted to see if they were compatible. According to this whitepaper on ASP.NET (follow the link and scroll down to "Support for WCF Web Services"), the RTM of AJAX does not support WCF. It seems they removed it so they could make it work better in a later release. The promise is that by the Orcas release of VS, they will be compatible.
This further cements my opinion that releasing .NET 3.0 without FULL tool and compatibility is nonsense. Without a good across the platform support (e.g. WCF and ASP.NET stack working well together), a workable WPF editor (Cider is horribly broken currently...change the default editor for XAML to XML, you'll be happier), and projects that actually compile out of the box (WPF projects don't compile currently without some hand-editing of the XAML). Microsoft has always been about tools more than technology, that's why I've been with them so long. If we need to cruft together a bunch of installs to make stuff work, I'd be doing that in Java and Linux.
It looks like most .NET 3.0 development should wait until late 2007 when Orcas ships...but that's just my opinion...
Come and get it. The newest drop of the Visual Studio Tools for .NET 3.0. Make sure you get the .NET 3.0 Runtime (and SDK) and the Visual Studio Workflow Tools RC5 while you're at it!
NOTE: No WCF Designer in this drop and Cider (the WPF designer) doesn't work in VS Express.
I have built a test-vista machine to do some WinFX stuff on and it got me wondering...why do I need to run the WinFX runtime installation on Vista? Isn't this supposed to be pre-installed? Aren't some built-in Vista apps already using WinFX? I am so confused. Anyone know?
And they're up on MSDN! Wahoo!
The PDC talk is heating up and it is clear to me that there is a huge number of 'wow' features that will be unveiled in LA. It seems like most of the other bloggers are talking about what I think is protected behind the multitude of NDA's I've signed. So to be safe I am keeping my mouth shut...tightly. What I can say is that what you'll see at the show about Whidbey, Yukon and Longhorn are phenomenal. Some of it is evolutionary, but much of it is revolutionary. I think you’ll be pleased... I am.
Since Don (Box) can’t seem to not talk about it, Indigo has gotten me really intrigued. I haven’t seen any of it, but I really want to know everything I can about it. That’s where I will spend my time at the PDC.
On a more mundane note, it looks INETA is going to be fielding its own band at the PDC. So far I know its Richard Hale Shaw, Carl Frankin and I. But I expect it to be a blast a number of people to come out of the woodwork to play.
In the last four years, I have been so heads down in my writing, developing and spelunking that I have neglected my own music ramblings (see http://mp3.com/shawntwain). The PDC has given me a reason to spend some time tuning up the old fingers.