user3409274
user3409274

Reputation:

Not able to pass the values retrieved from db in WCF to Web Controller in MVC 4

I need to write a method which retrieves data from db and need to pass this data to web controller. I have written a method in WCF and calling this method from web controller in MVC 4.

But it is giving exception: This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).

Inner Exception: The underlying connection was closed: An unexpected error occurred on a receive.

Problem statement: Need to get data from db using linq in wcf and need to pass that to a controller in MVC.

For the exception I tried with following solutions,but still i'm facing the problem:

1>Suggested to add Servicepointmanager in Web.config

<system.net>
    <settings>
      <servicePointManager expect100Continue="false" />
    </settings>
  </system.net>

2>Suggested to add max buffer size and max received size in web.config:

<basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" maxBufferSize="64000000" maxReceivedMessageSize="64000000" />
  </basicHttpBinding>

What i'm doing wrong??

In WCf:

public dynamic GetCountry()
        {
            var ddlCountry = (from s in dbEntity.Countries
                              select s);
            return ddlCountry;
        }

In Controller calling this wcf method as:

public dynamic GetAllCountries()
        {
            var country = objSvcMasterConfig.GetCountry();
            return country ;
        }

Upvotes: 0

Views: 364

Answers (1)

user3409274
user3409274

Reputation:

This is how i solved my problem:

  • 1> Instead of Dynamic datatype i used list datatype
  • 2> Created data-contract and operation Contract

[Data Contract]

    public class Countries
    {
       //Added my data contract members
     }

Service Contract:

   [OperationContract]
        List<Countries> GetCountry();
  • 3> Implemented operation Contract in Service

    public List<Countries> GetCountry()
    {
        try
        {
            List<Countries> objCountry = new List<Countries>();
            var query = (from s in dbEntity.Countries select new Countries() { CountryID = s.CountryID, Name = s.Name });
            objCountry = query.ToList<Countries>();
            return objCountry.ToList();
        }
        catch
        {
            return null;
        }
    
    }
    
  • 4> Called this WCF service method in MVC Controller

     var result=objSvc.GetCountry();
    

Upvotes: 1

Related Questions