in

ASP.NET Weblogs

This Blog

Syndication

Sponsors

News


Follow HosamKamel on Twitter

Hosam Kamel's Blog

May 2011 - Posts

  • DevLabs: Code Contracts

    dd491992.codecontracts_project(en-us)

    Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of pre-conditions, post-conditions, and object invariants. Contracts act as checked documentation of your external and internal APIs. The contracts are used to improve testing via runtime checking, enable static contract verification, and documentation generation. Code Contracts bring the advantages of design-by-contract programming to all .NET programming languages. We currently provide three tools:

    • Runtime Checking. Our binary rewriter modifies a program by injecting the contracts, which are checked as part of program execution. Rewritten programs improve testability: each contract acts as an oracle, giving a test run a pass/fail indication. Automatic testing tools, such as Pex, take advantage of contracts to generate more meaningful unit tests by filtering out meaningless test arguments that don't satisfy the pre-conditions.
    • Static Checking. Our static checker can decide if there are any contract violations without even running the program! It checks for implicit contracts, such as null dereferences and array bounds, as well as the explicit contracts. (Premium Edition only.)
    • Documentation Generation. Our documentation generator augments existing XML doc files with contract information. There are also new style sheets that can be used with Sandcastle so that the generated documentation pages have contract sections.

    Code Contracts comes in two editions:

    • Code Contracts Standard Edition: This version installs if you have any edition of Visual Studio other than the Express Edition. It includes the stand-alone contract library, the binary rewriter (for runtime checking), the reference assembly generator, and a set of reference assemblies for the .NET Framework.
    • Code Contracts Premium Edition: This version installs only if you have one of the following: Visual Studio 2008 Team System, Visual Studio 2010 Premium Edition, or Visual Studio 2010 Ultimate Edition. It includes the static checker in addition to all of the features in the Code Contracts Standard Edition.

    Get Started

    Digg This
    [tweetmeme source="HosamKamel" service="bit.ly" only_single="false"]
  • Accessing SharePoint 2010 web services While configuring a site for mixed mode authentication

    Today I've faced an issue while accessing SharePoint web services to achieve certain integration needs between my application and SharePoint site.

    The integration way is design to use SharePoint web services as the main way to access SharePoint data.

    I’ve write a small code snippet to query a document library and retrieve its folder and subfolders recursively and it works great on my SharePoint development environment but when trying it on another SharePoint environment it doesn’t work and always through access denied although I have passed all the necessary credentials to the web service object whatever I’m doing I’m always get

    E_ACCESSDENIED  Server was unable to process request. ---> Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

    After comparing between my development environment and the other environment I’ve discovered that my application is using classic mode (Windows authentication mode) while the other environment is using Claim based mode (mixed mode).

    Below are the recommended solution to access the SharePoint 2010 web services regarding to the authentication mechanism the application configured:

    1- Extend your application to use Windows Authentication only

    Extend your mixed authentication web application, and create a zone just for Windows Authentication, then change the Web Reference URL in the properties of your web service, to use that extended URL and port. You should have no issues of this kind anymore, the following article illustrate the steps clearly

    The request failed with the error message: Object moved (SharePoint 2010, Web Services & FBA) http://gvaro.wordpress.com/2011/01/04/the-request-failed-with-the-error-message-object-moved-sharepoint-2010-web-services-fba/

    2- Add the following code snippet to the generated reference file.

    You must add an additional header to your request. The header name must be X-FORMS_BASED_AUTH_ACCEPTED and the value must be f. Adding that header will tells SharePoint to use Windows authentication.

    Edit your generated reference and append the following method

    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    
    {
    
    System.Net.WebRequest wr = null;
    
    try
    
    {
    
    wr = base.GetWebRequest(uri);
    
    wr.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
    
    }
    
    catch (Exception ex)
    
    {
    
    //Some error handling goes here.
    
    }
    
    return wr;
    
    }

    A good reference at MSDN: Retrieving Data from a Multi-Authentication Site Using the Client Object Model and Web Services in SharePoint 2010  http://msdn.microsoft.com/en-us/library/hh124553.aspx#Y1200

    Enjoy Smile

    Digg This
    [tweetmeme source="HosamKamel" service="bit.ly" only_single="false"]
More Posts