How to: Programmatically Configure a WCF Endpoint

This topic includes procedures that describe how developers can create a SharePoint Web Part that uses data from a Windows Communication Foundation (WCF) Web service. The Web Part programmatically configures the WCF clients.

The procedures use the Partner Portal application's Contoso Pricing and Product Catalog services. They assume you have the Contoso.LOB.Services project installed.

Note

Installing the Partner Portal application also installs the Contoso.LOB.Services project.

To create a new Web Part project

  1. In Visual Studio, point to New on the File menu, and then click Project. Under Project types, click SharePoint. Under Templates, click Web Part. Use "MyWcfWebPart" as the project name. Click OK.
  2. In the Select Trust Level dialog box, select Full Trust (Deploy to GAC), and then click OK.

To add proxy classes for the Web services to the Web part project

  1. Open a Visual Studio 2008 command prompt.

  2. Use the cd command to change the working directory to the Web Part project directory.

  3. Run the following command:

    svcutil.exe https://computername:8686/contoso.lob.services/pricing.svc/noconfig /out:pricingproxy.cs

  4. Include the generated proxy class into the project. To do this, do the following:

    1. In Solution Explorer, right-click the project name, point to Add, and then click Existing Item.
    2. Browse to the Pricingproxy.cs file, and then click it.
    3. Click OK.
  5. Run the following command:

    svcutil.exe http://machinename:8585/contoso.lob.services/productcatalog.svc/noconfig /out:productcatalogproxy.cs

  6. Include the generated proxy class into the project. To do this, do the following:

    1. In Solution Explorer, right-click the project name, point to Add, and then click Existing Item.
    2. Browse to the Productcatalogproxy.cs file, and then click it.
    3. Click OK.
  7. In Solution Explorer, right-click References, click Add Reference, and then add the following references:

    • System.ServiceModel
    • System.Runtime.Serialization

To implement the Web Part class

  1. Add the following method to the WebPart1 class by pasting it into the WebPart1.cs file.

    private PricingClient ConfigurePricingProxy()
    {
        SecurityBindingElement securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
        HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();
        httpsTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Ntlm;
    
        CustomBinding binding = new CustomBinding(securityElement, httpsTransport);
        binding.Name = "pricingBinding";
    
        // Replace <machinename> with the name of the development server.
        EndpointAddress remoteAddress = new EndpointAddress("https://<machinename>:8686/Contoso.LOB.Services/pricing.svc");
    
        PricingClient client = new PricingClient(binding, remoteAddress);
        client.ClientCredentials.UserName.UserName = "Partner1";
        using (HostingEnvironment.Impersonate())
        {
            client.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        }
    
        return client;
    }
    
  2. Add the following method to the WebPart1 class by pasting it into the WebPart1.cs file.

    private ProductCatalogClient ConfigureProductCatalogProxy()
    {
        BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
    
        // Replace <machinename> with the name of the development server.
        EndpointAddress remoteAddress = new EndpointAddress("http://<machinename>:8585/Contoso.LOB.Services/productcatalog.svc");
    
        ProductCatalogClient client = new ProductCatalogClient(binding, remoteAddress);
        using (HostingEnvironment.Impersonate())
        {
            client.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        }
    
        return client;
    }
    
  3. Copy the following code into the CreateChildControls method of the WebPart1 class.

    base.CreateChildControls();
    
    const string PRODUCT_SKU = "1000000000";
    
    Price price;            
    using (PricingClient pricingClient = ConfigurePricingProxy())
    {
        price = pricingClient.GetPriceBySku(PRODUCT_SKU);
    }
    
    Product product;
    using (ProductCatalogClient productCatalogClient = ConfigureProductCatalogProxy())
    {
        product = productCatalogClient.GetProductBySku(PRODUCT_SKU);
    }
    
    LiteralControl content = new LiteralControl();
    content.Text = string.Format(CultureInfo.CurrentCulture, "Product: {0}<br>Price: {1}", product.Name, price.Value);
    
    this.Controls.Add(content);
    
  4. Highlight the unresolved class names, and then press CTRL+PERIOD. Select the namespace from the list. This automatically adds the using statements.

  5. Update the Web Part definition file. To do this, do the following:

    1. Open the WebPart1.webpart file.
    2. Update the Title property to My Pricing and Product Web Part.

To test the Web Part

  1. Specify a start debug URL. To do this, do the following:
    1. Right-click the project file, and then click Properties.
    2. Click the Debug tab. Under Start Action, set the Start browser with URL option to the address of a valid SharePoint site.
    3. Press F5.
  2. Add the Web Part to a page. To do this, do the following:
    1. In SharePoint, click Edit Page on the Site Actions menu.
    2. In any Web Part zone, click the Add a Web Part button.
    3. Under Miscellaneous, select My Product and Pricing Web Part, and then click Add.
    4. Verify that you see Blood Pressure Kit with a price of 319.99.

Home page on MSDN | Community site