Code first with npgsql and EF6 - c#

I am trying to create a simple example of code first with npgsql and Entity Framework but continually hit an error. Why? I have read all I can read, but have found no solution!
The credentials are fine, I can log in with PgAdmin without issues from the same machine as I'm running the C#. Tables are not created. The user is a Superuser.
When I try to .SaveChanges() I always hit the exception UpdateException: An error occurred while updating the entries. See the inner exception for details. with an inner exception of PostgresException: External component has thrown an exception.
I am completely flummexed by this. I think the most pertinant part of my code is the app.config:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.2.2.0" newVersion="3.2.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<connectionStrings>
<add name="contxt" connectionString="User ID=joe;Password=test;Host=192.168.xxx.xxx;Port=5432;Database=mydb" providerName="Npgsql" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql" />
<add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
The actual code couldn't be simpler:
class Program
{
static void Main(string[] args)
{
using (var db = new Contxt())
{
var foo = new Foo { Name = "asdf" };
db.Foos.Add(foo);
db.SaveChanges();
}
}
}
public class Foo
{
public Int64 Id { get; set; }
public string Name { get; set; }
}
public class FooMapper : EntityTypeConfiguration<Foo>
{
public FooMapper()
{
ToTable("Foos").HasKey<Int64>(x=>x.Id);
Property(x => x.Name).HasMaxLength(255).IsVariableLength().IsRequired();
}
}
public class Contxt : DbContext
{
public DbSet<Foo> Foos { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("public");
base.OnModelCreating(modelBuilder);
}
}

Related

Error in Retrieving Objects from WCF Service

New to WCF Services and needs some help.
So I have a client (basic console app) to test the WS (which is hosted on a test server not localhost)
With it I can pass and retrieve standard objects like string and int to and fro.
I can also pass custom classes to the WS but not retrieve them. I get either timeout or
An error occurred while receiving the http response to... 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 the
the service shutting down)
The underlying connection was closed. An unexpected error occurred on receive.
Unable to read data from the transport connection, an existing connection
was forcibly closed by the remote host.
I searched about endpoints and every single one seemed different and not obviously adaptable I am currently using the stock web.config when the project was created below which I think is where my issue is but finding difficult to know what to put.
WS Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings />
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
<connectionStrings>
<add name="CNS" connectionString="xyz" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Service Method in Question
public Request GetOrderByRef(string Ref)
{
AssetStockEntity db = new AssetStockEntity();
try
{
RequestEntity _requestef = new RequestEntity();
_requestef = db.RequestEntities.FirstOrDefault(x => x.Incident == Ref);
if (_requestef == null)
return null;
else
return TranslateRequestEntityToRequest(_requestef);
}
catch (Exception ex)
{
string debug = ex.ToString();
}
}
EDIT
Database Connection Test (Works as expected)
public string GetOrderByRefTest(string Ref)
{
AssetStockEntity db = new AssetStockEntity();
try
{
RequestEntity _requestef = new RequestEntity();
_requestef = db.RequestEntities.FirstOrDefault(x => x.Incident == Ref);
if (_requestef == null)
return null;
else
return _requestef.Requested_By;
}
catch (Exception ex)
{
string debug = ex.ToString();
return debug;
}
}
I'm using the translate call to convert EF objects to prevent exposure as from what I can understand is bad.
Class
[DataContract]
public class Request
{
[DataMember]
public int RId { get; set; }
[DataMember]
public string Incident { get; set; }
[DataMember]
public string Requested_By { get; set; }
[DataMember]
public string Authorised_By { get; set; }
[DataMember]
public virtual ICollection<Request_Items> Request_Items { get; set; }
}
Interface
[ServiceContract]
public interface Iws
{
[OperationContract]
Request GetOrderByRef(string Ref);
}
Test Console
static void Main(string[] args)
{
ASM.IwsClient ws = new ASM.IwsClient();
try
{
Console.WriteLine(ws.GetOrderByRef("ABC123").Requested_By);
Thread.Sleep(5000);
}
catch (Exception ex)
{
Console.WriteLine(ex);
Thread.Sleep(500000);
}
}
** Solved.
This issue was a parameter I left out for brevity was actually the cause, it wasn't serialised and thus rejected.
The cause was another parameter "Request_Items" which wasn't serialised with DataContract in its class.

unable to add data to SQL database using entity framework in windows service

I wrote a windows service for a project in which I needed to store some data in a database. I created the table with the required columns and created an entity model from the database. Context and mapping are done automatically by VS 2012. In order to test if the data is being saved, I hard coded some values and ran the service but the data doesn't get saved in the database.
Here is a sample service I wrote to test this out:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace serviceWithDatabase
{
public partial class testService : ServiceBase
{
Database1Entities db = new Database1Entities();
public testService()
{
InitializeComponent();
test();
}
protected override void OnStart(string[] args)
{
}
public void test()
{
Table t = new Table();
t.ticker = "goog";
t.Day1 = 1234;
t.Day2 = 4567;
t.Day3 = 7890.56;
db.Tables.Add(t);
db.SaveChanges();
}
protected override void OnStop()
{
}
}
}
model for the table:
namespace serviceWithDatabase
{
using System;
using System.Collections.Generic;
public partial class Table
{
public int Id { get; set; }
public string ticker { get; set; }
public Nullable<double> Day1 { get; set; }
public Nullable<double> Day2 { get; set; }
public Nullable<double> Day3 { get; set; }
}
}
App.config code:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="Database1Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database1.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
I can manually go into the database via VS 2012 and add data but it won't save data via the windows service. Anyone have any suggestions ?
the database was stored within the project and VS was updating the wrong database. When you place the database outside of the project folder (e.g. on your desktop) it works perfectly fine. This issue cost me a weekend lol. Hope it helps others :)
Try deleting the App.config and passing in your connection string directly some other way.
The App.config seemed to prevent compilation for me.

How do I resolve NHibernate.MappingException

I have a database, hbm mapping file and the App.config located in a class library. Now from a test project I reference that library and attempt to call a HibernateHelper class I create, at runtime the following error is thrown :
NHibernate.MappingException : Could not compile the mapping document: HibernateExample.Mappings.Products.hbm.xml
Please keep in mind that this is a class library that is being reference from a Test project.
If I change it output type to console application, it runs fine. But when I change it back to class library and reference it from my Test Project it throws the above mention error.
I tried adding config.Configure() but that throws a NhibernateDuplicateMapping exception.
FIXED:
Fixed the duplication mapping issue by removing from appconfig. and fixed the problem mapping entity by placing a hibernate.cfg.xml file in my Test project as well.
public sealed class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
const string Connectionstring = "servicestring";
public static void OpenSession()
{
var config = new Configuration();
config.Configure();
config.AddAssembly(Assembly.GetCallingAssembly());
_sessionFactory = config.BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
ISession session = null;
if (_sessionFactory == null)
OpenSession();
if (_sessionFactory != null)
{
session = _sessionFactory.OpenSession();
}
return session;
}
public static void CloseSessionFactory()
{
if (_sessionFactory != null)
{
_sessionFactory.Close();
}
}
// var dsn = ConfigurationManager.ConnectionStrings[Connectionstring].ConnectionString;
//config.SessionFactory().Integrate.Using<MsSqlCeDialect>().Connected.ByAppConfing(dsn);
// System.Diagnostics.Debug.WriteLine("My connection string: "+dsn);
//Get NHibernate configuration
//_sessionFactory = config.BuildSessionFactory();
//config.AddAssembly("HibernateExample");
}
Any ideas?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class"> NHibernate.Driver.SqlServerCeDriver</property>
<property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
<property name="connection.connection_string">Data Source=FirstSample.sdf;</property>
<property name="show_sql">true</property>
<mapping assembly="HibernateExample"/>
</session-factory>
</hibernate-configuration>
<connectionStrings>
<add name="testconnectionstring"
connectionString="Data Source=|DataDirectory|\FirstSample.sdf;Integrated Security=True"
providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urnchemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845DCD8080CC91" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-9.0.242.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HibernateExample" namespace="HibernateExample.Domain" >
<class name="Product" table="Products">
<id name="Id" type="integer">
<generator class="identity"/>
</id>
<property name="Name" type="string"/>
<property name="Category" type="string"/>
<property name="Discontinued" />
</class>
</hibernate-mapping>
Exception Thrown:
Test 'NunitTest.TestClass.canquerydb' failed: NHibernate.MappingException : Could not compile the mapping document: HibernateExample.Mappings.Products.hbm.xml
----> System.InvalidOperationException : Could not find the dialect in the configuration
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
NHibernateTest\NHibernateHelper.cs(21,0): at HibernateExample.NHibernateTest.NHibernateHelper.openSession()
NHibernateTest\NHibernateHelper.cs(28,0): at HibernateExample.NHibernateTest.NHibernateHelper.GetCurrentSession()
TestClass.cs(21,0): at NunitTest.TestClass.canquerydb()
--InvalidOperationException
at NHibernate.Dialect.Dialect.GetDialect(IDictionary`2 props)
at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
From the error, it appears that you are not configuring the Dialect before adding the mapping. This is required.
Here's a simple piece of basic configuration code:
var configuration = new Configuration();
configuration.SessionFactory().Integrate.Using<MsSql2012Dialect>()
.Connected.ByAppConfing("connName");//sic
//now you can add the mappings

Common.Logging with PostSharp and NLog 2.0

I use Common.Logging as a wrapper around NLog 2.0. I've done this so that I can replace NLog with another logging provider in the future.
I also use PostSharp to not write a try catch block everytime I need one. I have a class that inherits the OnMethodBoundaryAspect:
[Serializable]
public class LogMethodAttribute : OnMethodBoundaryAspect
{
private ILog logger;
public LogMethodAttribute()
{
this.logger = LogManager.GetCurrentClassLogger();
}
public override void OnEntry(MethodExecutionArgs args)
{
logger.Debug(string.Format("Entering {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));
}
public override void OnExit(MethodExecutionArgs args)
{
logger.Debug(string.Format("Leaving {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));
}
public override void OnException(MethodExecutionArgs args)
{
logger.Error(args.Exception.Message,args.Exception);
}
}
I have configured Common.Logging as follows in my web.config:
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
<arg key="configType" value="FILE" />
<arg key="configFile" value="~/NLog.config" />
</factoryAdapter>
</logging>
</common>
NLog.Config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true"
internalLogLevel="Debug"
internalLogToConsoleError="true"
internalLogFile="c:\new projects/nlog-app.txt"
>
<!--
See http://nlog-project.org/wiki/Configuration_file
for information on customizing logging rules and outputs.
-->
<targets>
<target name="database"
xsi:type="Database"
commandText="INSERT INTO LogEvent(EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES(#EventDateTime, #EventLevel, #UserName, #MachineName, #EventMessage, #ErrorSource, #ErrorClass, #ErrorMethod, #ErrorMessage, #InnerErrorMessage)"
dbProvider="System.Data.SqlClient">
<connectionString>
Data Source=...;Initial Catalog=myDB;User Id=user;Password=pass;
</connectionString>
<installConnectionString>
Data Source=...;Initial Catalog=myDB;User Id=user;Password=pass;
</installConnectionString>
<!-- parameters for the command -->
<parameter name="#EventDateTime" layout="${date:s}" />
<parameter name="#EventLevel" layout="${level}" />
<parameter name="#UserName" layout="${identity}" />
<parameter name="#MachineName" layout="${machinename}" />
<parameter name="#EventMessage" layout="${message}" />
<parameter name="#ErrorSource" layout="${event-context:item=error-source}" />
<parameter name="#ErrorClass" layout="${event-context:item=error-class}" />
<parameter name="#ErrorMethod" layout="${event-context:item=error-method}" />
<parameter name="#ErrorMessage" layout="${event-context:item=error-message}" />
<parameter name="#InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
<!-- commands to install database -->
<install-command>
<text>CREATE DATABASE myDB</text>
<connectionString> Data Source=...;Initial Catalog=myDB;User Id=user;Password=pass;</connectionString>
<ignoreFailures>true</ignoreFailures>
</install-command>
<install-command>
<text>
CREATE TABLE LogEvent(
EventId int primary key not null identity(1,1),
EventDateTime datetime,
EventLevel nvarchar(50),
UserName nvarchar(50),
MachineName nvarchar(1024),
EventMessage nvarchar(MAX),
ErrorSource nvarchar(1024),
ErrorClass nvarchar(1024),
ErrorMethod nvarchar(1024),
ErrorMessage nvarchar(MAX),
InnerErrorMessage nvarchar(MAX));
</text>
</install-command>
<!-- commands to uninstall database -->
<uninstall-command>
<text>DROP DATABASE myDB</text>
<connectionString> Data Source=...;Initial Catalog=myDB;User Id=user;Password=pass;</connectionString>
<ignoreFailures>true</ignoreFailures>
</uninstall-command>
</target>
</targets>
<rules>
<logger name="*" levels="Error" writeTo="database" />
</rules>
</nlog>
The problem is that nothing is inserted in my table. When I put a logger in for example my HomeController on the index page and I call my logger.Error("an error") it adds a record to my table.
Can somebody help me?
Are you decorating your controller methods with the LogMethodAttribute that you created?
Also, you'll want to adjust your logger rule to include more levels outside of just "Error", otherwise that's all you'll log.
Give this a try:
<rules>
<logger name="*" minLevel="Trace" writeTo="database" />
</rules>
Edit:
Have you tried moving your logger initialization into your method?
public override void OnEntry(MethodExecutionArgs args)
{
this.logger = LogManager.GetCurrentClassLogger();
logger.Debug(string.Format("Entering {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));
}
Per Donald Belcham's Pluralsight course, aspect constructors are not executed at runtime, so perhaps your logger is not getting set properly.
add a static property logger in your class Aspect
public class LogAspect : OnMethodBoundaryAspect
{
/// <summary>
/// Gets or sets the logger.
/// </summary>
public static ILogger logger { get; set; }
set logger variable in your application init method with your ILogger class and exclude all methods before this initialization with AttributeExclude.
[LogAspect(AttributeExclude = true)]
protected void Application_Start()
{
_windsorContainer = new WindsorContainer();
ApplicationDependencyInstaller.RegisterLoggingFacility(_windsorContainer);
LogAspect.logger = _windsorContainer.Resolve<ILogger>();

svcutil.exe or testing wcf service JSON ouput?

this post is off the back of my last question (thought i'd start a new question).
i am trying to create and test a simple (simple to you not me lol) WCF web service that outputs JSON. I beleive (with help) the application is ok but i need to test it and i a not sure how.
I'll attach my code below but here is what is happening, if i run the application it loads http://localhost:52002/ and i get the welcome to ASP.NET page, if run the application while i am in my .svc it loads http://localhost:52002/MyTestService.svc and i get the page:
You have created a service.
To test this service, you will need to create a client and use it to
call the service. You can do this using the svcutil.exe tool from the
command line with the following syntax:
svcutil.exe http://localhost:52002/MyTestService.svc?wsdl
I have been told i can http://localhost:52002/MyTestService.svc/GetResults to see the JSON output (see code below) but all i get is:
The webpage cannot be found
Any suggestions would be great, i apologies now for being abit new to all this and please ask you to maybe spell things out abit more then you normally would lol
Here is my code and tanks very much
Person.cs
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public int Age { get; set; }
public Person(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
}
}
MyTestServices.svc.cs
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = `AspNetCompatibilityRequirementsMode.Allowed)]`
public class TestService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public List<Person> GetResults()
{
List<Person> results = new List<Person>();
results.Add(new Person("Peyton", "Manning", 35));
results.Add(new Person("Drew", "Brees", 31));
results.Add(new Person("Tony", "Romo", 29));
return results;
}
MyTestService.svc
<%# ServiceHost Language="C#"
Service="TestService.TestService"
Factory="System.ServiceModel.Activation.ServiceHostFactory" %>
Web.Config
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<bindings />
<client />
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
I always use fiddler to debug the request. With the new Web Api for WCF you can set the accept header for the client to application/JSON and the response will be formatted as JSON.
I am not a WCF expert, so I cannot tell you what is wrong with your current application, but I can give you instructions for setting up a very simple example using your code, which will return a result in Fiddler.
Create a new ASP.NET Empty Web Application
Add a code file with the following:
Normally I wouldn't put everything in one file, but for simplicity...
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WebApplication1
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
List<Person> GetResults();
}
public class TestService : ITestService
{
public List<Person> GetResults()
{
List<Person> results = new List<Person>();
results.Add(new Person("Peyton", "Manning", 35));
results.Add(new Person("Drew", "Brees", 31));
results.Add(new Person("Tony", "Romo", 29));
return results;
}
}
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public int Age { get; set; }
public Person(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
}
}
}
Update the web.config file:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="test.svc"
service="WebApplication1.TestService"
factory="System.ServiceModel.Activation.WebServiceHostFactory" />
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
</configuration>
You don't need a .svc file, so start the application, making note of the root url used by the ASP.NET Development Server. On the Request Builder tab of fiddler enter your url:
Click the Execute button and you should see your service results:
Hopefully you can use this to figure out what you need to update on your service to get it running. Good luck!
I do not see your interface, but you are missing some stuff if you want it to be rest.
//Add this to your method
[WebGet(UriTemplate="/Results")]
public List<Person> GetResults()
{
List<Person> results = new List<Person>();
results.Add(new Person("Peyton", "Manning", 35));
results.Add(new Person("Drew", "Brees", 31));
results.Add(new Person("Tony", "Romo", 29));
return results;
}
Now you should be able to call it using http://localhost:52002/MyTestService.svc/Results. Just change the uri tempalte to what you want it to be.
You may also need to change the factory to be the WebServiceHostFactory.
<%# ServiceHost Service="TestService.MyTestService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Categories