Hi Guys,
I'm currently developing a project using WCF (hosted on local IIS) which connects to a local SQL database using LINQ to get data.
My problem is that when I run the code, 9/10 times it'll fail with the "Specified Cast Is Not Valid" error, but the other 1/10 times it will return the correct value (a messagebox with the records 'name' field.
The code snippets below are from my wcf datacontract/servicecontract, server and client code. There is only one record in the table im retrieving.
WCF Service (IFaveoService.cs)
[ServiceContract]
[ServiceKnownType(typeof(SRType))]
public interface IFaveoService
{
[OperationContract]
IEnumerable<SRType> GetSRTypeByID(int srID);
}
[DataContract]
[Database]
public class FaveoData : DataContext
{
public Table<SRType> SRTypes;
public FaveoData(string connStr)
: base(connStr)
{
}
}
[DataContract]
[Table(Name = "SRTYPES")]
public class SRType
{
[DataMember]
[Column(IsPrimaryKey=true, IsDbGenerated=true)]
public int srTypeID
{
get;
set;
}
[DataMember]
[Column]
public string srName
{
get;
set;
}
}
Server (FaveoService.cs)
class FaveoService : IFaveoService
{
public IEnumerable<SRType> GetSRTypeByID(int srID)
{
string userInfo = "User Id=xx;Password=xx;Trusted_Connection=False";
string connStr = @"Server=.\SQLExpress;Server=xx;Database=FaveoData;"+userInfo;
FaveoData db = new FaveoData(connStr);
//select the record where srTypeID==1 from SRTYPES table in db
IEnumerable<SRType> types = from t in db.SRTypes where t.srTypeID == 1 select t;
return types;
}
}
Client (Program.cs)
client = new FaveoServiceClient();
client.Open();
try
{
FaveoServices.SRType[] sr = client.GetSRTypeByID(1);
foreach(FaveoServices.SRType t in sr)
{
MessageBox.Show(t.srName.ToString());
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
client.Close();
Web.Config
<?xml version="1.0"?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
<source name="System.ServiceModel" switchValue="Verbose,ActivityTracing"
propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="c:\inetpub\wwwroot\faveoservice\web_messages.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
<add initializeData="c:\inetpub\wwwroot\faveoservice\web_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="32768"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<netTcpBinding>
<binding sendTimeout="00:03:00" receiveTimeout="00:03:00" />
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="FaveoServices.FaveoService">
<endpoint address="" binding="wsHttpBinding" contract="FaveoServices.IFaveoService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/FaveoService" />
</baseAddresses>
<timeouts closeTimeout="00:03:00" openTimeout="00:03:00" />
</host>
</service>
</services>
</system.serviceModel>
</configuration>
When debugging the server code, FaveoService.cs, I get the "Specified cast not valid" in the local variable types->Results View->base, but then the exception clientside says:
"An error occurred while receiving the HTTP response to [url]http://mmorris-pc2/FaveoService/FaveoService.svc[/url]. 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). See server logs for more details."
InnerException {"The underlying connection was closed: An unexpected error occurred on a receive."} System.Exception {System.Net.WebException}
InnerException {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."} System.Exception {System.IO.IOException}
(theres nothing in the event logs & no wcf logs were created!)
As mentioned before ... sometimes it works ... but most of the time it doesnt. I'm not sure what was different for it to work that one time :(
Any ideas? From what I can see all of my types are the same?!
Thanks,
Michael