<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>WCF</title>
        <link>http://geekswithblogs.net/SoftwareDoneRight/category/10584.aspx</link>
        <description>WCF</description>
        <language>en-US</language>
        <copyright>ChrisD</copyright>
        <managingEditor>chris@wtfsolutions.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Solved: Unit/Integration Testing of a WCF Service</title>
            <link>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/09/16/solved-unitintegration-testing-of-a-wcf-service.aspx</link>
            <description>&lt;p&gt;I've got a solution to running and debugging unit tests for WCF hosted services that utilize the CMServiceHost.  Whether your generate your serivce proxy via WSDL (Add Service Reference) or Shared Contract (ClientFactory), the solution is similar.  This email will detail how to invoke a WCF Service Host for testing in both scenarios. &lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Testing a service client generated via "Add Service Reference"&lt;/strong&gt;&lt;/font&gt;    &lt;br /&gt;I'll illustrate the solution using the following sample unit test.  Let's assume you've created a service proxy for the "TestService" service with Add Service Reference, and assigned it to the "TestSvc" namespace.   You're test looks like this: &lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;    [TestMethod]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test_InvokeTestService()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;            {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;                var client = &lt;span class="kwrd"&gt;new&lt;/span&gt; TestSvc.TestServiceClient();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;                var pingResp = client.Ping();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;                TestContext.WriteLine(&lt;span class="str"&gt;"Ping Response: {0}"&lt;/span&gt;, pingResp);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            } &lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;Let's assume this service is hosted by our ChannelDelivery.Host project and the above test lives int he ChannelDelivery.Test project. &lt;/p&gt;

&lt;p&gt;   1. Start by getting latest of the _ReferencedAssemblies in the dev branch of TFS.
  &lt;br /&gt;   2. In your Test Project, add a reference to the Platform.UnitTests_Utilities assembly (its available in the _ReferencedAssemblies\Framework folder in the Applications solutions).

  &lt;br /&gt;   3. Decorate your unit test with the AspNetDevelopmentServer attribute.  This attribute takes two arguments; a unique host idenfiter, and the location of the web site to run.  Assuming  You're running this for the ChannelDelivery unit tests, you'd specify the arguments as follows: [AspNetDevelopmentServer("ChannelDeliveryHost", @"ChannelDelivery\ChannelDelivery.Host")]

  &lt;br /&gt;   4.  In your unit test code, after your create your service proxy, and before you invoke the service operation, add a call to &lt;/p&gt;

&lt;p&gt;     client.Endpoint.Address= WebTestHelper.GetRedirectedEndpointAddress("ChannelDeliveryHost", TestContext, client.Endpoint.Address); &lt;/p&gt;

&lt;p&gt;Your code should now resemble: &lt;/p&gt;

&lt;p&gt;      [TestMethod]
  &lt;br /&gt;    [AspNetDevelopmentServer("ChannelDeliveryHost", @ "ChannelDelivery\ChannelDelivery.Host")]

  &lt;br /&gt;            public void Test_InvokeTestService()

  &lt;br /&gt;            {

  &lt;br /&gt;                var client = new TestSvc.TestServiceClient();

  &lt;br /&gt;               client.Endpoint.Address= WebTestHelper.GetRedirectedEndpointAddress("ChannelDeliveryHost", TestContext, client.Endpoint.Address);

  &lt;br /&gt;                var pingResp = client.Ping();

  &lt;br /&gt;                TestContext.WriteLine("Ping Response: {0}", pingResp);

  &lt;br /&gt;            } &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;Testing a service client that shares service contract and uses ClientFactory.&lt;/font&gt;&lt;/strong&gt;

  &lt;br /&gt;I'll illustrate the solution using the following sample unit test.  Let's assume you've created a service proxy for the "EchoService" service using the ClientFactory.   You're test looks like this: &lt;/p&gt;

&lt;p&gt;            [TestMethod]
  &lt;br /&gt;            public void Test_ChannelDeliveryEcho()

  &lt;br /&gt;            {

  &lt;br /&gt;                using (var svc = ClientFactory.ProduceServiceClient&amp;lt;IEchoService&amp;gt;())

  &lt;br /&gt;                {

  &lt;br /&gt;                    TestContext.WriteLine("Echo Test Results:" + svc.Client.Echo("Echo!")); &lt;/p&gt;

&lt;p&gt;                }       &lt;br /&gt;            } &lt;/p&gt;

&lt;p&gt;To enable integration testing of the EchoService: &lt;/p&gt;

&lt;p&gt;   1. Start by getting latest of the _ReferencedAssemblies in the dev branch of TFS.
  &lt;br /&gt;   2. Decorate your unit test with the AspNetDevelopmentServer attribute.  This attribute takes two arguments; a unique host identifier, and the location of the web site to run.  Assuming  You're running this for the ChannelDelivery unit tests, you'd specify the arguments as follows: [AspNetDevelopmentServer("ChannelDeliveryHost", @"ChannelDelivery\ChannelDelivery.Host")]

  &lt;br /&gt;   3.  In your unit test code, after your create your service proxy, and before you invoke the service operation,  call the RedirectServiceUri method on the service proxy, passing a reference to the TestContext. &lt;/p&gt;

&lt;p&gt;Your code should now resemble: &lt;/p&gt;

&lt;p&gt;            [TestMethod]
  &lt;br /&gt;            [AspNetDevelopmentServer("ChannelDeliveryHost",@"ChannelDelivery\ChannelDelivery.Host")]

  &lt;br /&gt;            public void Test_ChannelDeliveryEcho()

  &lt;br /&gt;            {

  &lt;br /&gt;                using (var svc = ClientFactory.ProduceServiceClient&amp;lt;IEchoService&amp;gt;())

  &lt;br /&gt;                {

  &lt;br /&gt;                    svc.RedirectServiceUri(TestContext);

  &lt;br /&gt;                    TestContext.WriteLine("Echo Test Results:" + svc.Client.Echo("Echo!"));

  &lt;br /&gt;                }       &lt;br /&gt;            } &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;font size="3"&gt;&lt;font size="2"&gt;Summary&lt;/font&gt;

      &lt;br /&gt;&lt;/font&gt;&lt;/strong&gt;When you debug or run this unit test, the AspNetDevelopmentServer (the one that runs when you press f5 on your web site), starts up and runs on a random port.   The WebTestHelper and RedirectServiceURI methods, uses the currently executing test context to determine the random port assignment, and determines the new service address based on the random port and current service uri.  The methods also ensure the new uri is valid by attempting to send a POST request to the uri. &lt;/p&gt;

&lt;p&gt;You can now run your integration unit tests that rely on hosted wcf services. &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The code for GetRedirectedEndpointAddress (aka GetRedirectedUriString) follows:&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1bdcca90-ccd4-405d-8e32-afa9f1e91e63" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt;
&lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt;
&lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt;
&lt;li&gt; &lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; GetRedirectedUriString(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; TestContext, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; defaultUriStr)&lt;/li&gt;
&lt;li&gt;{&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; GetRedirectedUriString(TestContext, defaultUriStr, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;
&lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; GetRedirectedUriString(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; TestContext, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; DefaultUriStr, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; HostIdentifier)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt;
&lt;li&gt;    Uri hostUri = getHostUriFromTestContext(TestContext, HostIdentifier);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (hostUri == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; DefaultUriStr;&lt;/li&gt;
&lt;li&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; defaultUri = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Uri(DefaultUriStr);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; newUriStr = hostUri.Scheme + &lt;span style="color:#a31515"&gt;"://"&lt;/span&gt; + hostUri.Authority + defaultUri.PathAndQuery;&lt;/li&gt;
&lt;li&gt;    newUriStr = EnsureValidUri(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Uri(newUriStr));&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; newUriStr;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;
&lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; Uri getHostUriFromTestContext(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; context, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; identifier)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt;
&lt;li&gt;    &lt;span style="color:#0000ff"&gt;const&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; DevServerKeyName = &lt;span style="color:#a31515"&gt;"AspNetDevelopmentServer."&lt;/span&gt;;&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af"&gt;BindingFlags&lt;/span&gt; bindingFlags = &lt;span style="color:#2b91af"&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span style="color:#2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;span style="color:#2b91af"&gt;BindingFlags&lt;/span&gt;.Instance;&lt;/li&gt;
&lt;li&gt;    &lt;span style="color:#2b91af"&gt;PropertyInfo&lt;/span&gt; info = context.GetType().GetProperty(&lt;span style="color:#a31515"&gt;"Properties"&lt;/span&gt;, bindingFlags);&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (info != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt;
&lt;li&gt;    {&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; contextProperties = info.GetValue(context, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff"&gt;as&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;&amp;gt;;&lt;/li&gt;
&lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (contextProperties != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt;
&lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.IsNullOrEmpty(identifier))&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt;
&lt;li&gt;                &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; entry = contextProperties.FirstOrDefault(kvp =&amp;gt; kvp.Key.StartsWith(DevServerKeyName));&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; entry.Value &lt;span style="color:#0000ff"&gt;as&lt;/span&gt; Uri;&lt;/li&gt;
&lt;li&gt;            }&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; uri = contextProperties[DevServerKeyName + identifier] &lt;span style="color:#0000ff"&gt;as&lt;/span&gt; Uri;&lt;/li&gt;
&lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; uri;&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt;
&lt;li&gt;    }&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;
&lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; EnsureValidUri(Uri targetUri )&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt;
&lt;li&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; uri = targetUri.ToString();&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; targetUri.Segments.Length; i++ )&lt;/li&gt;
&lt;li&gt;    {&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; response = WebTools.DoHttpPost(uri, &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (response.Contains(&lt;span style="color:#a31515"&gt;"404"&lt;/span&gt;))&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt;
&lt;li&gt;            &lt;span style="color:#0000ff"&gt;while&lt;/span&gt; (targetUri.Segments[i] == &lt;span style="color:#a31515"&gt;"/"&lt;/span&gt;)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;                i++;&lt;/li&gt;
&lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (i &amp;lt;= targetUri.Segments.Length)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;                uri = uri.Replace(targetUri.Segments[i] , &lt;span style="color:#a31515"&gt;""&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;        }&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; uri;&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt;
&lt;li&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; targetUri.ToString();&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; WriteMessageToTestContext(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; TestContext, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; message)&lt;/li&gt;
&lt;li&gt;{&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; mi = TestContext.GetType().GetMethod(&lt;span style="color:#a31515"&gt;"WriteLine"&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (mi != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt;
&lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; parameters = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] {message, &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] {}};&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;        mi.Invoke(TestContext, parameters);&lt;/li&gt;
&lt;li&gt;    }&lt;/li&gt;
&lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt; &lt;img src="http://geekswithblogs.net/SoftwareDoneRight/aggbug/141838.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ChrisD</dc:creator>
            <guid>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/09/16/solved-unitintegration-testing-of-a-wcf-service.aspx</guid>
            <pubDate>Thu, 16 Sep 2010 13:06:31 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/SoftwareDoneRight/comments/141838.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/09/16/solved-unitintegration-testing-of-a-wcf-service.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/SoftwareDoneRight/comments/commentRss/141838.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/SoftwareDoneRight/services/trackbacks/141838.aspx</trackback:ping>
        </item>
        <item>
            <title>Supporting Request Timeout with a WebClient</title>
            <link>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/09/14/supporting-request-timeout-with-a-webclient.aspx</link>
            <description>&lt;p&gt;I recently needed to adjust the timeout value of the HTTP Request being generated by WebClient.  My attempt to perform a POST of a large file over HTTP was causing the WebClient to timeout.  I thought adjusting the timeout value for the web client would fix this problem.  One problem, the WebClient doesn’t support a Timeout value. &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;I did some snooping via Reflector and discovered 2 important facts about the WebClient.  Fact #1, The WebClient isn’t sealed.  This means we can inherit from it.  Fact #2: The WebClient factories the HTTP Request from a protected method called GetWebRequest.  This means I’d only need to override the GetWebRequest method to affect the HTTP Request used by the web client. &lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;so..&lt;/p&gt;  &lt;p&gt;I created the following class:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:79e54735-253a-4437-83fe-20e3e7087cfc" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ExtendedWebClient&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  {&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; Timeout { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;override&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebRequest&lt;/span&gt; GetWebRequest(&lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt; address)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      {&lt;/li&gt; &lt;li&gt;          &lt;span style="color:#2b91af"&gt;WebRequest&lt;/span&gt; request = &lt;span style="color:#0000ff"&gt;base&lt;/span&gt;.GetWebRequest(address);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;          &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (request != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li&gt;              request.Timeout = Timeout;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;          &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; request;&lt;/li&gt; &lt;li&gt;      }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; ExtendedWebClient()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      {&lt;/li&gt; &lt;li&gt;          Timeout = 100000; &lt;span style="color:#008000"&gt;// the standard HTTP Request Timeout default&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;      }&lt;/li&gt; &lt;li&gt;  }&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;When I need to control the timeout of a request, I create an instance of my ExtendedWebClient and set the timeout property:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:be40c362-4422-4c26-9b25-fb3e870f475f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; client = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ExtendedWebClient&lt;/span&gt;())&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    client.Timeout = -1;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Let me know if you find this as useful as I did. &lt;/p&gt; &lt;img src="http://geekswithblogs.net/SoftwareDoneRight/aggbug/141799.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ChrisD</dc:creator>
            <guid>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/09/14/supporting-request-timeout-with-a-webclient.aspx</guid>
            <pubDate>Tue, 14 Sep 2010 17:52:36 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/SoftwareDoneRight/comments/141799.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/09/14/supporting-request-timeout-with-a-webclient.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/SoftwareDoneRight/comments/commentRss/141799.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/SoftwareDoneRight/services/trackbacks/141799.aspx</trackback:ping>
        </item>
        <item>
            <title>Solving &amp;ldquo;XmlSchemaException: The global element '&amp;lt;elementName&amp;gt;' has already been declared.&amp;rdquo;</title>
            <link>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/04/14/solving-ldquoxmlschemaexception-the-global-element-ltelementnamegt-has-already-been-declared.rdquo.aspx</link>
            <description>&lt;p&gt;I recently encountered this error when I attempted to consume a new hosted WCF service.  The service used the Request/Response model and had been properly decorated.  The response and request objects were marked as DataContracts and had a specified namespace.   My WCF service interface was marked as a ServiceContract and shared the namespace attribute value.   Everything should have been fine, right?&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;ServiceContract&lt;/span&gt;(Namespace = &lt;span style="color: #a31515"&gt;"http://schemas.myclient.com/09/12"&lt;/span&gt;)]
   &lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IProductActivationService
   &lt;/span&gt;{
       [&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;]
       &lt;span style="color: #2b91af"&gt;ActivateSoftwareResponse &lt;/span&gt;ActivateSoftware(&lt;span style="color: #2b91af"&gt;ActivateSoftwareRequest &lt;/span&gt;request);
   }&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;well, not exactly.  Apparently the WSDL generator was having an issue:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font color="#ff0000"&gt;System.Xml.Schema.XmlSchemaException: The global element '&lt;/font&gt;&lt;a href="http://schemas.myclient.com/09/12:ActivateSoftwareResponse"&gt;&lt;font color="#ff0000"&gt;http://schemas.myclient.com/09/12:ActivateSoftwareResponse&lt;/font&gt;&lt;/a&gt;&lt;font color="#ff0000"&gt;' has already been declared.&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After digging I’ve found the problem; the WSDL generator has some reserved suffixes for its entities, including Response, Request, Solicit (see &lt;a title="http://msdn.microsoft.com/en-us/library/ms731045.aspx" href="http://msdn.microsoft.com/en-us/library/ms731045.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms731045.aspx&lt;/a&gt;).  The error message is actually the result of a naming conflict.  The WSDL generator uses the namespace of the service to build its reserved types.  The service contract and data contract share a namespace, which coupled with the response/request name suffixes I was using in my class names, resulted in the SchemaException.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Fix:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Two options:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Rename my data contract entities to use a non-reserved keyword suffix (i.e.  change ActivateSoftwareResponse to ActivateSoftwareResp). or;&lt;/li&gt;

  &lt;li&gt;Change the namespace of the data contracts to differ from the service contract namespace.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I chose option 2 and changed all my data contracts to use a “&lt;span style="color: #a31515"&gt;&lt;a href="http://schemas.myclient.com/09/12/data"&gt;http://schemas.myclient.com/09/12&lt;strong&gt;/data&lt;/strong&gt;&lt;/a&gt;” &lt;font color="#000000"&gt;namespace value. This avoided a name collision and I was able to produce my WSDL and consume my service.
      &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/SoftwareDoneRight/aggbug/139258.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>ChrisD</dc:creator>
            <guid>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/04/14/solving-ldquoxmlschemaexception-the-global-element-ltelementnamegt-has-already-been-declared.rdquo.aspx</guid>
            <pubDate>Wed, 14 Apr 2010 15:01:53 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/SoftwareDoneRight/comments/139258.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/SoftwareDoneRight/archive/2010/04/14/solving-ldquoxmlschemaexception-the-global-element-ltelementnamegt-has-already-been-declared.rdquo.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/SoftwareDoneRight/comments/commentRss/139258.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/SoftwareDoneRight/services/trackbacks/139258.aspx</trackback:ping>
        </item>
    </channel>
</rss>
