IT tutorials
 
Applications Server
 

Using Windows Azure WCF Services in SharePoint and Office : Create the WCF Service

12/12/2012 11:40:34 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Create the WCF Service

  1. Open Visual Studio 2010, create a new blank solution, and name it AzureWCFSolution.

  2. Right-click the solution and select Add | New Project.

  3. In the Cloud templates section, select the Windows Azure Project template. Name the project CustomerInformation, and click OK.

  4. Select the WCF Service Web Role template. Click the small pencil icon to edit the default name, and name the new service GetCustomersFromAzure.

    image with no caption
  5. Click OK. After Visual Studio completes the project structure, right-click the project and select Add | Class. Name the class file Customer.cs and then click Add. You’ll use this class as an in-memory object to create and store data. It exposes six properties that correspond to a customer ID, title, first name, last name, email, and phone number, as shown in the bold code here:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace GetCustomrsFromAzure
    {
        public class Customer
        {
            public string custID { get; set; }
            public string custTitle { get; set; }
            public string custFirstName { get; set; }
            public string custLastName { get; set; }
            public string custEmail { get; set; }
            public string custPhone { get; set; }
        }
    }
  6. Double-click the Service1.cs file and then rename Service1 to GetCustomers and IService1 to IGetCustomers. Then right-click the file names and select Rename to ensure that the class and interface file names are the same as the class and interface namespaces.

  7. In the GetCustomers service class, edit the code as shown in the following bold code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace GetCustomersFromAzure
    {
        public class GetCustomers : IGetCustomers
        {
            List<Customer> myCustomerList = new List<Customer>();
            List<Customer> returnListOfCustomers = new List<Customer>();
    
            #region Customer Methods
            public List<Customer> getACustomer(int custID)
            {
                generateCustomerData();
                string strCustID = custID.ToString();
                var returnListOfData = (from customer in myCustomerList
                                        where customer.custID == strCustID
                                        select customer).ToArray();
                List<Customer> returnCustomer = returnListOfData.ToList();
                return returnCustomer;
            }
    
            public List<Customer> getAllCustomers()
            {
                generateCustomerData();
                var returnListOfData = (from customer in myCustomerList
                                        select customer).ToArray();
    
                returnListOfCustomers = returnListOfData.ToList();
                return returnListOfCustomers;
            }
    
            private void generateCustomerData()
            {
                Customer cust1 = new Customer();
                cust1.custTitle = "Dr.";
                cust1.custID = "1";
                cust1.custFirstName = "Ben";
                cust1.custLastName = "Smith";
                cust1.custEmail = "[email protected]";
                cust1.custPhone = "425-555-0177";
                myCustomerList.Add(cust1);
    ...
                Customer cust10 = new Customer();
                cust10.custTitle = "Dr.";
                cust10.custID = "10";
                cust10.custFirstName = "Dorena";
                cust10.custLastName = "Paschke";
                cust10.custEmail = "[email protected]";
                cust10.custPhone = "425-555-0156";
                myCustomerList.Add(cust10);
            }
            #endregion
    
            #region Credit Score Method
    
            public int GetPersonalCreditScore()
            {
                int creditScore = 0;
    
                try
                {
                    Random random = new Random();
                    creditScore = random.Next(500, 800);
    
                    return creditScore;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
    
            #endregion
        }
    }

    The preceding code is the core service code that uses the Customers class to create an in-memory data construct that is returned when the service is called. The getACustomer and getAllCustomers methods both return a list collection, but GetACustomer returns a single record from the list collection. Note that the generateCustomerData method code has been trimmed to reduce redundancy. The code creates 10 customers that the service passes back to the calling application as a list collection object—you could choose to create more or fewer customer records, because the list is an in-memory object.

    Note

    Alternatively, you could choose to use the Entity Data Model framework to bind a database such as Microsoft SQL Azure to your service . Rather than using an in-memory object, you’d use a data context and Microsoft Language-Integrated Query (LINQ) to query and return the appropriate records by using a list collection or other data construct. 

    This code also returns a random integer value (creditScore). In a real-world application, you’d include some processing here that runs against a customer record (using a Social Security number, for example) to get the credit score, but for this example, the important point is to show how the service is used.

  8. You’re now finished with the core service code. Double-click the IGetCustomers file to edit the service contract. Amend the service contract code as shown in the following bold code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    using System.ServiceModel.Activation;
    
    namespace GetCustomersFromAzure
    {
        [ServiceContract]
        public interface IGetCustomers
        {
            [OperationContract]
            List<Customer> getACustomer(int custID);
    
            [OperationContract]
            List<Customer> getAllCustomers();
    
            [OperationContract]
            int GetPersonalCreditScore();
        }
    }
  9. You now want to ensure that your service is configured to deploy to Windows Azure. To make sure this is possible, edit the web.config file to match the following bold code. Note that the commented-out code in the <serviceBehaviors> section is the default code created by Visual Studio, and the bold code shows the amendments you need to make to handle the load-balancing workaround for services deployed to Windows Azure:

    <system.serviceModel>
        <services>
          <service behaviorConfiguration="GetCustomersFromAzure.Service1Behavior"
            name="GetCustomersFromAzure.GetCustomers">
            <endpoint address="" binding="basicHttpBinding"
              contract="GetCustomersFromAzure.IGetCustomers">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract=
    "IMetadataExchange" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <!--<behavior name="GetCustomersFromAzure.Service1Behavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>-->
            <behavior name="GetCustomersFromAzure.Service1Behavior">
              <serviceMetadata httpGetEnabled="true" />
              <useRequestHeadersForMetadataAddress>
                <defaultPorts>
                  <add scheme="http" port="81" />
                  <add scheme="https" port="444" />
                </defaultPorts>
              </useRequestHeadersForMetadataAddress>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
  10. Press F6 to build the changes. After the application compiles successfully, right-click the cloud project and select Publish. This invokes the Deploy Windows Azure dialog box. Select Create Service Package Only and click OK. This invokes Windows Explorer with the necessary configuration and package files that need to get deployed to Windows Azure. Copy the folder path to the Clipboard.

  11. Now navigate to your Windows Azure developer portal (https://windows.azure.com/Default.aspx) and create a new service. To do this, click New Hosted Service, and then complete the information in the New Hosted Service dialog box (enter a name for the service, the URL prefix, region, and version, and then click Browse twice to load your package and configuration files). When you’ve finished, click OK. Windows Azure publishes your WCF service.

You should now see something similar to Figure 1. You can click the URI in the Domain Name System (DNS) name and append the service endpoint (GetCustomers.svc) to the DNS name (otherwise, you will get an access error to the root DNS).

WCF service management in the Windows Azure developer portal.

Figure 1. WCF service management in the Windows Azure developer portal.

Your service definition page should now be exposed in your web browser. You can click the WSDL link to get more detailed information about the service, as shown in Figure 2.

WCF service definition page in Windows Azure.

Figure 2. WCF service definition page in Windows Azure.

At this stage, you’ve finished creating the WCF service. In the next sections, you will use this service in SharePoint in various ways. Copy the URI of the service endpoint to your Clipboard, because you’ll need it in the next exercise.

 
Others
 
- Microsoft Lync Server 2010 : Dependent Services - Public Key Infrastructure (part 3) - Understanding the Lync Server Certificate
- Microsoft Lync Server 2010 : Dependent Services - Public Key Infrastructure (part 2) - Certificate Types for Lync Server
- Microsoft Lync Server 2010 : Dependent Services - Public Key Infrastructure (part 1) - Installing Certificate Services
- Microsoft Lync Server 2010 : Dependent Services - Domain Name System, Network Dependencies
- Microsoft Lync Server 2010 : Dependent Services - Active Directory
- SharePoint 2010 : Customizing the Search Results and Search Center - Improving the User Experience (part 2)
- SharePoint 2010 : Customizing the Search Results and Search Center - Improving the User Experience (part 1) - Contextual Promotions
- Exchange Server 2010 : Standards and Protocols - Other Key Technologies Used by Exchange
- Microsoft Dynamics CRM 2011 : Viewing Campaign Results
- Microsoft Dynamics CRM 2011 : Converting a Campaign Response
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us