26
Nov
09

enabling Async methods for wcf in wpf

By default when you add a WCF Service reference in WPF, it does not give async functions. If your application needs async features, while adding a reference to the service, lookout for a ‘Advanced’ button on Add service reference window at bottom right corner. On clicking on that button, a new window appears. There is a checkbox called ‘Generate Asynchronous Operations’. Hit Ok and you are done!

Simple isn’t it :)

25
Nov
09

custom setting tab order in wpf application

For the framework element set KeyboardNavigation.TabIndex="<number goes here>"

Simple isn’t it?

24
Nov
09

if you need to send more data to wcf service

For example, you need to upload log file in case of error from desktop application to remote site using WCF service and if the log file is too big, you might have to tweak your WCF service’s Web.config.

By default the configuration for the service will be

      <services>
            <service behaviorConfiguration="ServiceBehavior" name="Service">
                <endpoint address="" binding="basicHttpBinding" contract="IService">
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>

You need to specify the binding manually and add binding reference as highlighted below.   

 

    <services>
            <service behaviorConfiguration="ServiceBehavior" name="Service">
                <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService" contract="IService">
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
                        maxReceivedMessageSize="2147483647" messageEncoding="Text"
                        textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                        <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
                            maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                        <security mode="None">
                            <transport clientCredentialType="None" proxyCredentialType="None"
                                realm="" />
                            <message clientCredentialType="UserName" algorithmSuite="Default" />
                        </security>
                    </binding>
                </basicHttpBinding>
            </bindings>

That’s it :) Simple isn’t it?

24
Nov
09

clone your custom objects with 2 lines of code in C#

There may be many instances where we have to clone a object. For example, we open the object for edit in a form and after few modifications, if the user decides to abandon changes/cancel modifications and if we are using the main object to do all the changes, it would be a disaster.

Cloning is a way out of it. I remember a time when we had to write another clone method to achieve the cloning of object. With C#, its 2 lines of code!

Your class can be like this..

public class myclass
       {
           public int id { get; set; }
           public string name { get; set; }
           public string value { get; set; }
       }

Now to make it clone-able change it this way…

public class myclass :ICloneable
        {
            public int id { get; set; }
            public string name { get; set; }
            public string value { get; set; }

            public object Clone() { return this.MemberwiseClone(); }

        }

Try this..

            myclass a = new myclass();
            a.id = 1;
            a.name = "asdf";
            a.value = "qwer";
            myclass b = a.Clone() as myclass;
            b.value = "zxcv";
            myclass c = a;
            Console.Write(a.value + " "  + b.value + " " + c.value) ;

output will be

              qwer zxcv qwer

Simple isn’t it?

More ways of cloning objects are shown here

Technorati Tags: ,,,
16
Nov
09

multiline textbox in wpf

As a windows forms application/web application developer you might be looking for MultiLine property while developing in WPF. You will be surprised to see that there is no property like that.

Trick is you have to set AcceptsReturn="True" TextWrapping="Wrap" and VerticalScrollBarVisibility="Auto" to get yourself a multiline textbox

09
Nov
09

if you are expecting too much data from wcf service

Use WCF basicHTTPBinding as the binding. By default it will be wsHTTPBinding

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="ServiceBehavior" name="Service">
                <endpoint address="" binding="basicHttpBinding" contract="IService">
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
    </system.serviceModel>

On your client application [web/wpf/silverlight] set the same with maxReceivedMessageSize, maxBufferPoolSize, maxStringContentLength and maxArrayLength to 2147483647 as shown below

<system.serviceModel>
       <bindings>
           <basicHttpBinding>
             <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                 openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                 allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647" messageEncoding="Text"
                 textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
               <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
                   maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
               <security mode="None">
                 <transport clientCredentialType="None" proxyCredentialType="None"
                     realm="" />
                 <message clientCredentialType="UserName" algorithmSuite="Default" />
               </security>
             </binding>
           </basicHttpBinding>
       </bindings>
       <client>
           <endpoint address="http://yourwebserver/yourservice.svc"
               binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
               contract="ServiceReference1.IService" name="BasicHttpBinding_IService" />
       </client>
   </system.serviceModel>

That should do :)

09
Nov
09

wcf: maximum string length quota (8192) has been exceeded

If you get ‘The maximum string content length quota (8192) has been exceeded while reading XML data’ error from WCF service, it is because the output has restrictions in web.config of client application.

Go ahead and set maxReceivedMessageSize, maxStringContentLength, maxDepth, maxArrayLength sizes to the max possible 2147483647 in client application’s (WPF / Silverlight / ASP.Net ) web.config/app.config.

<system.serviceModel>
        <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IAdwService"
            bypassProxyOnLocal="false"
            transactionFlow="false"
            hostNameComparisonMode="StrongWildcard"
            maxBufferPoolSize="524288"
            maxReceivedMessageSize="2147483647"
            messageEncoding="Text"
            textEncoding="utf-8"
            useDefaultWebProxy="true">
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4012" maxNameTableCharCount="8192"
                />
                <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true" establishSecurityContext="true" />
                </security>
            </binding>
        </wsHttpBinding>

Do not unnecessarily increase the value as it might defeat the purpose under DOS attacks.

08
Nov
09

avoid uac on WPF application deployed on Vista / windows 7

Sometimes when you are building a full trust app to be deployed via clickonce, things like editing registry, custom protocol access etc might require user’s permission. To avoid such UAC messages, turn on requestedExecutionLevel to asInvoker in your app.manifest under properties folder.

More info here

06
Nov
09

IIs7 wcf configuration

If you are seeing 404- file not found error for your favorite svc file on IIS7

execute this in command prompt

C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe -i

Yeppie… your svc files work like charm :)

05
Nov
09

When application is closed, Notify Icon in system tray remains

In Windows forms or WPF application if we are using Notify Icon in the system tray, before you close the application by issuing System.Windows.Application.Current.Shutdown(); set the NotifyIcon object’s icon to null!

That clears the icon from the system tray :)

 




@pooran

 

November 2009
M T W T F S S
« Oct    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Visits so far..

  • 135