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:
[ServiceContract]
public interface IInstaearnService
{
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json, ResponseFormat =
WebMessageFormat.Json,UriTemplate = "GetApps")]
[OperationContract]
string GetApps();
}
My service Class:
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:
<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>
<!--
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<!--
<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.