Click here to Skip to main content
15,885,920 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have create a json based WCF, Which is running correctly on local but throws error when we host it on IIS and access that online service.I searched a lot on google bu did not find any solution.

My service Inteface:

C#
[ServiceContract]
public interface IInstaearnService
{
  [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped,
   RequestFormat = WebMessageFormat.Json, ResponseFormat =
   WebMessageFormat.Json,UriTemplate = "GetApps")]
  [OperationContract]
   string GetApps();
}


My service Class:
C#
public class InstaearnService : IInstaearnService
{
public string GetApps()
     {
         try
         {
             ClsApp objClsApp = new ClsApp();
             using (SqlConnection con = new    SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
             {
                 #region Get app
                 con.Open();
                 string sAppQry = "Select Id, Name, ImageName, State, Location, Age, InstallPoints, OpenPoints, RegisterPoints, Url" +
                                  " from mstAppDetails" +
                                  " where OfferFrom <= convert(date, getdate()) and OfferTo>=convert(date, getdate())" +
                                  " and status=1";

                 using (SqlDataAdapter sqlDa = new SqlDataAdapter(sAppQry, con))
                 {
                     DataSet dsApp = new DataSet();
                     sqlDa.Fill(dsApp);

                     if (dsApp != null && dsApp.Tables.Count > 0 && dsApp.Tables[0].Rows.Count > 0)
                     {
                         for (int i = 0; i < dsApp.Tables[0].Rows.Count; i++)
                         {
                             ClsApp objInnerClsApp = new ClsApp();
                             objInnerClsApp.sId = dsApp.Tables[0].Rows[i]["Id"].ToString();
                             objInnerClsApp.sName = dsApp.Tables[0].Rows[i]["Name"].ToString();
                             objInnerClsApp.sImageName = dsApp.Tables[0].Rows[i]["ImageName"].ToString();
                             objInnerClsApp.sState = dsApp.Tables[0].Rows[i]["State"].ToString();
                             objInnerClsApp.sLocation = dsApp.Tables[0].Rows[i]["Location"].ToString();
                             objInnerClsApp.iAge = Convert.ToInt32(dsApp.Tables[0].Rows[i]["Age"]);
                             objInnerClsApp.iInstallPoints = Convert.ToInt32(dsApp.Tables[0].Rows[i]["InstallPoints"]);
                             objInnerClsApp.iOpenPoints = Convert.ToInt32(dsApp.Tables[0].Rows[i]["OpenPoints"]);
                             objInnerClsApp.iRegisterPoints = Convert.ToInt32(dsApp.Tables[0].Rows[i]["RegisterPoints"]);
                             objInnerClsApp.sUrl = dsApp.Tables[0].Rows[i]["Url"].ToString();

                             objClsApp.lstClsApp.Add(objInnerClsApp);
                         }
                     }
                 }
                 #endregion
             }

             string sJsonResult = JsonConvert.SerializeObject(objClsApp);

             return sJsonResult;
         }
         catch (Exception ex1)
         {
             return ex1.InnerException.ToString();
         }
     }
}


My Web.config:

HTML
<system.serviceModel>
    <protocolMapping>
      <add binding="webHttpBinding" scheme="https" />
    </protocolMapping>
    <bindings>
      <webHttpBinding>
        <binding name="webHttp" allowCookies="true"
                 maxReceivedMessageSize="20000000"
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
          <readerQuotas maxDepth="32"
               maxArrayLength="200000000"
               maxStringContentLength="200000000"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <!-- 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="webBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="InstaearnServices.InstaearnService">
        <endpoint address="http://localhost:51193/InstaearnService.svc" contract="InstaearnServices.IInstaearnService" binding="webHttpBinding" behaviorConfiguration="webBehavior" />
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <directoryBrowse enabled="true" />
  </system.webServer>
  <system.diagnostics>
    <sources>
      <source name="UserTraceSource" switchValue="Warning, ActivityTracing">
        <listeners>
          <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Logs\UserTraces.svclog" />
        </listeners>
      </source>
    </sources>
    <trace autoflush="true" />
  </system.diagnostics>


I am getting following error:

<message>Object reference not set to an instance of an object.
<stacktrace><![CDATA[at TestApp.Resolve[T]() in C:\Users\TestApp\getApp.cs:line 77
at TestApp.getApp() in C:\Users\TestApp\getApp.cs:line 77
at WCF.Services.TestApp.GetApp() in
C:\Users\TestApp\getApp.cs:line:line 20
at SyncInvokeValidateUser(Object , Object[] )

Please provide my some solution.

Thanks in advance.
Posted
Comments
Richard Deeming 8-Jun-15 4:34am    
Does your web.config file have a connectionStrings entry called "myConnectionString"?
yashu verma 8-Jun-15 4:35am    
yes, it has.
ZurdoDev 8-Jun-15 8:29am    
1. Reply to the comment so that the user is notified.
2. The stack trace says the error is on line 77 of getApp.cs. You need to fix that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900