Monday, April 22, 2013

Remote Event Receiver Debugging (Using Developer Tools for VS 2012 Preview)

For Part 2 using Developer Tool for Visual Studio 2012 RTM

If we are developing SharePoint App for ProviderHosted / AutoHosted. We may have a requirement to create remote event receivers to handle events related to a list. 

The remove event receivers are actually handed using a service that needs to be hosted in another web project. When we do debugging web project is hosted on local machine's IIS Express. 


In this scenario it is quite difficult for both local IIS Express and remote SharePoint server to talk to each other due to network boundaries. To make debugging to work we require to do some manual changes in web.config of web project and AppManifest file of the SharePoint App Project. We also require to make use of Azure Service Bus.


For more information about Windows Azure Service Bus.


Steps to make debugging work.

  1. Create Autohosted SharePoint App with remote event receiver.
  2. Register for Microsoft Azure account.
  3. Create service Bus namespace. for e.g. if namespace is remoteevent then URL will be like https://{namespace-created}.servicebus.windows.net
    e.g.
    https://remoteevent.servicebus.windows.net
  4. Right click web project. Select Nuget Package. Find Azure Service Bus and Install it.
  5. We need to make use of these values. Get these values according to your project.
    1. web project url on IIS Express: http://localhost:2917
    2. Bus Service namespace URL(from point 3): https://{namespace-created}.servicebus.windows.net
    3. Your unique web service address: https://{namespace-created}.servicebus.windows.net/myservice1
    4. Default Issuer: (Open azure serice bus account Select your namespace and click "Access Key". write down Default Issuer)
    5. Default Key: (Open azure serice bus account Select your namespace and click "Access Key". write down Default Key)
    6. Client Secret:Value of Key "ClientSecret" inside section <appSettings> of web.config file
  6. Open web.config file of web project.
  7. Replace section <system.serviceModel> with following code
    <system.serviceModel>
  8.   <bindings>
        <basicHttpBinding>

          <!--Used by app for SharePoint-->
          <binding name="secureBinding">
            <security mode="Transport" />
          </binding>
        </basicHttpBinding>

        <!-- Service Bus Binding -->
        <basicHttpRelayBinding>
          <binding name="BasicHttpRelayBindingConfig">
            <security relayClientAuthenticationType="None" />
          </binding>
        </basicHttpRelayBinding>

        <!-- Service Bus Binding -->
      </bindings>
      <protocolMapping>
        <add  binding="basicHttpBinding" scheme="https" bindingConfiguration="secureBinding" />
      </protocolMapping>
      <extensions>

        <!-- In this extension section we are introducing all known service 
            bus extensions. Users can remove the ones they don't need. -->
        <behaviorExtensions>
          <add name="transportClientEndpointBehavior" type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement,  Microsoft.ServiceBus,  Version=1.8.0.0,  Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
          <add name="serviceRegistrySettings" type="Microsoft.ServiceBus.Configuration.ServiceRegistrySettingsElement, Microsoft.ServiceBus, Version=1.8.0.0,Culture=neutral,  PublicKeyToken=31bf3856ad364e35" />
        </behaviorExtensions>
        <bindingExtensions>
          <add
              name="basicHttpRelayBinding" type="Microsoft.ServiceBus.Configuration.BasicHttpRelayBindingCollectionElement, 
                        Microsoft.ServiceBus, 
                        Version=1.8.0.0, 
                        Culture=neutral, 
                        PublicKeyToken=31bf3856ad364e35" />
        </bindingExtensions>
      </extensions>

      <!-- Service Bus Binding -->
      <services>
        <clear />
        <service
            name="{Replace with Point 6b}"
            behaviorConfiguration="default">
          <endpoint
              address="{Replace with Point 6a}"
              binding="basicHttpBinding"
    contract="Microsoft.SharePoint.Client.EventReceivers.IRemoteEventService"/>
          <endpoint
              address="{Replace with Point 6a}/mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
          <endpoint
              address="{Replace with Point 6c}"
    contract="Microsoft.SharePoint.Client.EventReceivers.IRemoteEventService"
              binding="basicHttpRelayBinding"
              bindingConfiguration="BasicHttpRelayBindingConfig"
              behaviorConfiguration="sharedCredentials" />
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="default">

            <!-- To avoid disclosing metadata information, 
                    set the values below to false before deployment. -->
            <serviceMetadata
                httpGetEnabled="true"
                httpsGetEnabled="true"/>

            <!-- To receive exception details in faults for debugging 
                    purposes, set the value below to true. Set to false before 
                    deployment to avoid disclosing exception information. -->
            <serviceDebug includeExceptionDetailInFaults="true" />
          </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
          <behavior name="sharedCredentials">
            <transportClientEndpointBehavior
                credentialType="SharedSecret">
              <clientCredentials>
                <sharedSecret
                    issuerName="{Replace with Point 6d}"
                    issuerSecret="{Replace with Point 6e}" />
              </clientCredentials>
            </transportClientEndpointBehavior>
            <serviceRegistrySettings discoveryMode="Public" />
          </behavior>
        </endpointBehaviors>
      </behaviors>

      <!-- Service Bus Binding -->
    </system.serviceModel>

  9. In new <system.serviceModel> section replace
    {Replace with Point 6a}value you get from point 6 a
    e.g.
    http://localhost:2917
    {Replace with Point 6b}value you get from point 6 b
    e.g.
    https://{namespace-created}.servicebus.windows.net
    {Replace with Point 6c}value you get from point 6 c
    e.g.
    https://{namespace-created}.servicebus.windows.net/myservice1
    {Replace with Point 6d}value you get from point 6 d
    {Replace with Point 6e}value you get from point 6 e

  10. Right click AppManifest file inside App project and click View code
  11. Replace </AutoDeployedWebApplication> with following code:
    <AutoDeployedWebApplication>
        <DebugInfo 
            ClientSecret="{Replace with Point 6f}" 
    AppUrl="{Replace with Point 6a};{Replace with Point 6c};" />
    </AutoDeployedWebApplication>
  12.  Open Element.xml file of you remote event receiver. Replace URL value with 6c.

That's all we need to change to make remote debugging enable.


No comments:

Post a Comment

Popular Posts