Software Architecture – Tips for Development Model with Microsoft Visual Studio 2010+ .Net Framework 4.0 + Sliverlight 4.0

Environment:
Microsoft Visual Studio 2010+ .Net Framework 4.0 + Sliverlight 4.0
WCF + Nhibernate + Sliverlight

Assumption: This document assumes that you have initial understanding of WCF and NHibernate.

Tips:
1. Entities will be shared b/w NHibernate and WCF, so sample entity will look like as:

[DataContract]    
    public class City
    {
        public City() { }

        [DataMember]
        public virtual int CityID { get; set; }

        [DataMember]
        public virtual string CityCode { get; set; }

        [DataMember]
        public virtual string Description { get; set; }
             
    }

Corresponding HBM will look like as:

<class name="SilerverlightTest.Web.City, SilerverlightTest.Web" table="GeneralCityMaster" lazy="false" >
      <id name="CityID" column="CityID">
        <generator class="native" />
      </id>
      <property name="CityCode" column="CityCode" />
      <property name="Description" column="Description" />
       </class>

2. Incase there are circular references between the entities of Data contract, all entities must be marked as IsReference = true. See below:

 [DataContract(IsReference = true)] 
public class CurrencyRate : ParentClass<CurrencyRate>
{
 }

Search goolge with keywords “WCF + NHibernate + circular reference” for more details and see top 5 links for theory related to it.

3. As lazy loading can’t work across the WCF wire, so all entities must be marked as lazy=”false” in HBM file. See Below.

<class name="Pegasus.BusinessObjects.General.Main.Company, PCMS-SL.Web" table="GeneralCompanyMaster" lazy="false" >

4. Incase of ManyToOne relationship in class of HBM files, make sure to add option not-found=”ignore” so that if relation is missing then exception is not thrown.

<many-to-one name="ModifiedBy" column="ModifiedBy" class="Pegasus.BusinessObjects.General.Administration.Employees, PCMS-SL.Web" cascade="none"   lazy="false" not-found="ignore"/>

5. In all *.svc.cs, services must be marked as [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
See Blow:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AccountsManager : IAccountsManager
    {
}

6. For many issues, WCF Service will show following error as:
{System.TimeoutException: The HTTP request to ‘http://localhost/PCMS-SL.Web/businessmanagers/MastersManager.svc&#8217; has exceeded the allotted timeout. The time allotted to this operation may have been a portion of a longer timeout. —> System.Net.WebException —> System.Net.WebException
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.c__DisplayClass5.b__4(Object sendState)
at System.Net.Browser.AsyncHelper.c__DisplayClass2.b__0(Object sendState)

That is general error, possible error can be anything that is specified above.So try to investigate the root cause of this error.

7. In order to see the exception generated in WCF Service enble error in web.config of ASP Project as:

<behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

8. Copy ClientAcessPolicy.xml at the root of ASP .net Project, with content as:

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from>
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

Otherwise following error will occur:

An error occurred while trying to make a request to URI “http://localhost:1498/SecurityManager.svc&#8221;. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details.

Search google with this exception and see top 5 urls for theory related to it.

9. NHibernate will list for IList for collection definition. But WCF Look for List so a trick is to use properties as :

<bag name="InstrumentList" cascade="all" inverse="true" lazy="false">
        <key column="Cityid" />
        <one-to-many class="SilerverlightTest.Web.Instrument, SilerverlightTest.Web"  />
      </bag>

Code for collection will be as:

public virtual IList<Instrument> InstrumentList {get;set;}

        [DataMember(Name = "InstrumentList")]
        public  List<Instrument> InstrumentListPrivate
        {
            get { return  new List<Instrument>(this.InstrumentList) ; }
            set { this.InstrumentList = value; }
        }  

For reference see the link below:
http://stackoverflow.com/questions/1538811/nhibernate-and-wcf-ilist-conflict

10. Exception as:
{System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. —> System.Net.WebException: The remote server returned an error: NotFound. —> System.Net.WebException: The remote server returned an error: NotFound.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.c__DisplayClass5.b__4(Object sendState)
at System.Net.Browser.AsyncHelper.c__DisplayClass2.b__0(Object sendState)
— End of inner exception stack trace —
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)

That is general error, possible error can be anything that is specified above. So try to investigate the root cause of this error.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: