Debug WCF Restful service (POST/GET) – Part 2


There are many way to debug WCF/Cloud service hosted in Azure and you can refer the below tutorial but I prefer to debug the service old and easy way by deploying the service in local IIS then publish to Azure.

https://docs.microsoft.com/en-us/azure/vs-azure-tools-debug-cloud-services-virtual-machines

https://docs.microsoft.com/en-us/azure/vs-azure-tools-debugging-cloud-services-overview

In Part 1, I explain how to create and deploy WCF Restful service in Azure.

https://arvindcsit.blog/crm-and-azure-integration/

I will use the same existing code and will add another POST method to explain debugging.

I am going to create one POST method which takes User Name and Password as request body and return predefined token.

So I added below code in Part 1 source code.

Contract: ICRMService.cs

Under ServiceContract

[OperationContract]
[WebInvoke(Method = “POST”,
UriTemplate = “/getauth”,
ResponseFormat = WebMessageFormat.Json)]
CRMTokenResponse GetCRMToken(CRMTokenRequest tokenReqeust);

DataContract
[DataContract]
public class CRMTokenResponse
{
public CRMTokenResponse()
{
this.Token = “”;
this.Error = “”;
}
[DataMember(IsRequired = false)]
public string Token { get; set; }
[DataMember(IsRequired = false)]
public string Error { get; set; }
}
[DataContract]
public class CRMTokenRequest
{
[DataMember]
public string Username { get; set; }
[DataMember]
public string Password { get; set; }
}
CRMService.svc.cs
public CRMTokenResponse GetCRMToken(CRMTokenRequest tokenRequest)
{
CRMTokenResponse resp = new CRMTokenResponse();
if (tokenRequest != null)
{
string userName = WebConfigurationManager.AppSettings[“CRM.Username”];
string password = WebConfigurationManager.AppSettings[“CRM.Password”];
string tokenId = WebConfigurationManager.AppSettings[“CRM.Token”];
if (tokenRequest.Username == null || tokenRequest.Password == null)
{
resp.Error = “Enter UserName and Password”;
return resp;
}
if (tokenRequest.Username.Equals(userName) && tokenRequest.Password.Equals(password))
{
resp.Token = tokenId;
}
else
{
resp.Error = “Invalid Username & Password”;
}
}
else
{
resp.Error = “Request is Null”;
}
return resp;
} 

Web.config

Add these three lines under <appSettings></appSettings> tag.
<add key=”CRM.Username” value=”Arvind” />
<add key=”CRM.Password” value=”Singh” />
<add key=”CRM.Token” value=”8178746c24b9f5544d259f2a38d7c1a785bc70a2″ />

We are done with code changes, make sure you build the project in Debug mode and deploy in local IIS.

Browse the service and make sure the service is running without any error. I will use PostMan to hit and debug the service.

Set the breakpoint and attached to W3WP process. Select the Content Type and Request Body as below.

Then Click “Send” and cursor will stop at breakpoint as shown below.

you can see the token value while debugging as below

Finally, you get output in output window of PostMan as below.

Similarly, you can debug it for GET method. Once your service is working fine, you can publish your service to Azure using steps mentioned in PART 1 (https://arvindcsit.blog/crm-and-azure-integration/)

Hope this helps you.

 

 

 

 

Interacting Silverlight with CRM Forms


Silverlight application can get a reference to the Xrm.Page Object instance using either of following approaches.

1. Using HTML bridge feature of Silverlight

ScriptObject  xrm = (ScriptObject)HtmlPage.Window.GetProperty(“Xrm”);

ScriptObject  page= (ScriptObject)xrm.GetProperty(“Page”);

2. Using Dynamic Language Runtime

Using DLR you can utilize the dynamic language keywords which allow resolving method calls at runtime.

dynamic  xrm = (ScriptObject)HtmlPage.Window.GetProperty(“Xrm”);

 

Below is some important methods/property which is useful for Silverlight Application.

1. Getting the current Theme: This is useful if you are trying to style your Silverlight content in a similar way the user will see the web client. Valid Values are Default, Office12Blue, and Office14Silver.

var theme= xrm.Page.context.getCurrentTheme();

2.      Getting Org Name:

var orgName= xrm.Page.context.getOrgUniqueName();

3.      Getting the Server URL:

var serverUrl = xrm.Page.context.getServerUrl();

4.      Getting the User ID:

Knowing the user id that is working with the current page can be helpful when you need to do things like retrieve all the records that are owned by that person to present on the page.

 

var  userID = xrm.Page.context.getUserId();

5.      Getting the user’s Role:

var roles = xrm.Page.context.getUserRoles();

6.      Getting the entity logical Name:

String entityname =xrm.Page.data.entity.getEntityName();

7.      Getting the Entity Id:
Guid entityID = xrm.Page.data.entity.getId();

8.      Checking if Entity is Dirty:

By checking the dirty flag at the entity level you can quickly determine if there have been any changes to any of the fields. This doesn’t give you field level granularity you have to check each attribute if you need that.

bool isDirty = xrm.Page.data.entity.getIsDirty();

9.      Getting the Data as XML:

Using this feature you can get a string that represents the XML that would be sent to the server when the record is saved. The XML contains only the fields which have been modified.

String dataXml = xrm.Page.data.entity.getDataXml();

10.  Saving Data to the Server:

The save function allows you to simulate saving data to the CRM server just like if the user hit the save button in the ribbon.

Xrm.Page.data.entity.save()
or
Xrm.Page.data.entity.save(“saveandclose”)
or
Xrm.Page.data.entity.save(“saveandnew”)

11.  Working with the Entity Attributes

You can get to the attributes on an entity via the Attributes collection. The different attribute types can have special methods that are only for their specific type.The following shows which methods each type of attribute currently has.

All Attribute have these methods:

addOnChange,fireOnChange, getAttributeType,getFormat,getInitialValue, getIsDirty, getName, getParent,getRequiredLevel, getSubmitMode, getUserPrivelege, getValue, removeOnChange, setRequiredLevel, setSubmitMode, and setValue

Money,decimal,integer and double have these methods too:

getMax, getMin, and getPrecision

Boolean and Optioset attributes have these methods:

getInitalValue

Optionset attributes have these methods:

getOption, getOptions, getSelectedOption, and getText

 

12.  UI Methods:

The UI methods are high level methods located at Xrm.page.ui and are the starting point for working with the UI controls. This is also the starting point for looking for Controls and Tabs.

Refreshing the Ribbon:

This method is beyond helpful if you are doing any enable/display rules that depends on values on the form. After the value is changed on the form you can use this method to force the ribbon to re-evaluate the data in the form so the ribbon is updated.

refreshRibbon();

13.  Working with Form Controls:

The following methods are on all controls:

getControlType, getDisabled, getLabel, getName, getParent, setDisabled(all except web resources), setFocus, setLabel, and setVisible

The following methods are specific to Lookups;

addCustomeView, getDefaultView, and setDefaultView

The following methods are specific to Option Sets

adoption,clearOptions, and removeOption

The following methods are specific to Web Resources:

getData, getObject, setData, keep in mind the get/setData can only be used with Silverlight Web resources

The Following methods are specific to IFrames:

getSrc, setSrc, and getInitalUrl

The following methods are specific to Subgrids:

refresh