I am converting an old Classic ASP API to WCF. I have some code kinda sorta maybe working, but I do NOT like it.
Firstly, there's a problem is with the URITemplate setup. When I try to hit say, "http://192.168.1.88:90/GetEmployees.svc" that works giving me that generic page:
"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://192.168.1.88:90/GetEmployees.svc?wsdl"...
It also works if I hit it via SOAP NSURL action from inside my XCode/iPhone app. I get responses.
However, I'm supposed to be able to hit "http://192.168.1.88:90/GetEmployees.svc/employees" and get the json output in a browser... but this never works. All I get are either a blank page (if I turn custom asp errors off) or the generic asp error page telling
me to adjust my web.config to turn off custom errors like so: <customErrors mode="Off"/> (but if i do, I just get that blank screen...)
Here's some code. It s a hodgepodge of stuff I kept trying. Sometimes overwriting some code from a previous tute/example. I need someone to help me sort this all out.
CODE:
IGetEmployees.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace WcfService3
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IGetEmployees" in both code and config file together.
[ServiceContract]
public interface IGetEmployees
{
[OperationContract]
//attribute for returning JSON format
[WebInvoke(Method = "GET",
//RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Json,
//BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "employees")]
//method
List<Employee> GetAllEmployeesMethod();
}
}
GetEmployees.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
namespace WcfService3
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GetEmployees" in code, svc and config file together.
public class GetEmployees : IGetEmployees
{
public List<Employee> GetAllEmployeesMethod()
{
List<Employee> mylist = new List<Employee>();
string strcon = ConfigurationManager.ConnectionStrings["iOSConn"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strcon))
{
conn.Open();
string cmdStr = String.Format("Select firstname,lastname from Users");
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
mylist.Add(new Employee(rd.GetString(0), rd.GetString(1)));//, rd.GetDecimal(2)));
}
conn.Close();
}
return mylist;
}
[DataContract]
public class Employee
{
[DataMember]
public string firstname { get; set; }
[DataMember]
public string lastname { get; set; }
//[DataMember]
//public decimal salary { get; set; }
public Employee(string first, string last)//, decimal sal)
{
firstname = first;
lastname = last;
//salary = sal;
}
}
}
Thanks, I think. I know WCF isn't RESTful by default. However, there are many articles (I can't find onhe that fits me) that accomplish what I want. I mean, this code DOES work back and forth to my iOS app, which is what I really need. I just want to know
why the URITemplate part isn't working...
I'll take a look at your link...forgive me if your answer seems.. like a near-zero effort response to what I'm looking for just so you can get your 'check as answer' rating+1. (If I'm wrong correct me and I'll concede my apologies.)
This code works to an ios APP via SOAP calls and there are many articles online on how to do exactly what I'm trying to do. The issue really has been differences between 3.5, 4, and 4.5 WCF tutorials having mixed versions.
Simple question, why doesn't the URITemplate work?
Isn't there something in the code I've written that you can tell isn't setup/written properly?
Try switching from the WebInvoke attribute to the WebGet attribute. If this doesn't work, try defining the GetAllEmployees method as IEnumerable<Employee> in place of List<Employee>.
Christopher Reed, MCT, MCPD, MCTS, Microsoft Specialist, MTA
"The oxen are slow, but the earth is patient."
according to this http://stackoverflow.com/questions/5995425/http-400-bad-request-when-calling-wcf-service-operation there is still a config error and I'm doing nearly exactly the same thing, but they seemed to have communicated directly to finish and/or
just not finished the post.
I feel like its something very small and simple to change to make it work in a browser... this is frustrating. However, at least I AM getting communication directly to my iOS client... but I thought this is suposed to work this way also, otherwise what is
the URITemplate for since my calls to WCF are more SOAP-like...?
In your config file, you have the contract in your endpoint as "WcfService3.GetEmployees". You, however, have an interface marked as the ServiceContract, so your contract should be "WcfService3.IGetEmployees".
Christopher Reed, MCT, MCPD, MCTS, Microsoft Specialist, MTA
"The oxen are slow, but the earth is patient."
Marked as answer by Beau_Damore on Dec 20, 2012 05:16 PM
Beau_Damore
Member
120 Points
305 Posts
Help with newbie WCF developer
Dec 18, 2012 09:37 PM|LINK
Hello All,
I am converting an old Classic ASP API to WCF. I have some code kinda sorta maybe working, but I do NOT like it.
Firstly, there's a problem is with the URITemplate setup. When I try to hit say, "http://192.168.1.88:90/GetEmployees.svc" that works giving me that generic page:
"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://192.168.1.88:90/GetEmployees.svc?wsdl"...
It also works if I hit it via SOAP NSURL action from inside my XCode/iPhone app. I get responses.
However, I'm supposed to be able to hit "http://192.168.1.88:90/GetEmployees.svc/employees" and get the json output in a browser... but this never works. All I get are either a blank page (if I turn custom asp errors off) or the generic asp error page telling me to adjust my web.config to turn off custom errors like so: <customErrors mode="Off"/> (but if i do, I just get that blank screen...)
Here's some code. It s a hodgepodge of stuff I kept trying. Sometimes overwriting some code from a previous tute/example. I need someone to help me sort this all out.
CODE:
IGetEmployees.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.ServiceModel.Web; namespace WcfService3 { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IGetEmployees" in both code and config file together. [ServiceContract] public interface IGetEmployees { [OperationContract] //attribute for returning JSON format [WebInvoke(Method = "GET", //RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, //BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "employees")] //method List<Employee> GetAllEmployeesMethod(); } }GetEmployees.svc.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Data.SqlClient; using System.Configuration; namespace WcfService3 { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GetEmployees" in code, svc and config file together. public class GetEmployees : IGetEmployees { public List<Employee> GetAllEmployeesMethod() { List<Employee> mylist = new List<Employee>(); string strcon = ConfigurationManager.ConnectionStrings["iOSConn"].ConnectionString; using (SqlConnection conn = new SqlConnection(strcon)) { conn.Open(); string cmdStr = String.Format("Select firstname,lastname from Users"); SqlCommand cmd = new SqlCommand(cmdStr, conn); SqlDataReader rd = cmd.ExecuteReader(); if (rd.HasRows) { while (rd.Read()) mylist.Add(new Employee(rd.GetString(0), rd.GetString(1)));//, rd.GetDecimal(2))); } conn.Close(); } return mylist; } [DataContract] public class Employee { [DataMember] public string firstname { get; set; } [DataMember] public string lastname { get; set; } //[DataMember] //public decimal salary { get; set; } public Employee(string first, string last)//, decimal sal) { firstname = first; lastname = last; //salary = sal; } } }:::Web.config:::
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="iOSConn" connectionString=" Server=localhost;Database=iosTest;User ID=abcdef;Password=abcdef;Trusted_Connection=False;"/> </connectionStrings > <system.web> <compilation debug="true" targetFramework="4.0" /> <customErrors mode="Off"/> </system.web> <system.serviceModel> <services> <service name="WcfService3.GetEmployees"> <endpoint address="" binding="webHttpBinding" contract="WcfService3.GetEmployees" behaviorConfiguration="WebBehavior"/> <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="WebBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>Consultant
www.beaudamore.com
Careed
All-Star
18774 Points
3637 Posts
Re: Help with newbie WCF developer
Dec 18, 2012 09:59 PM|LINK
You need to look at WCF Data Services for what you're trying to do as this is RESTful while WCF Services are not RESTful by default.
http://msdn.microsoft.com/en-us/library/cc668792.aspx
"The oxen are slow, but the earth is patient."
Beau_Damore
Member
120 Points
305 Posts
Re: Help with newbie WCF developer
Dec 18, 2012 11:09 PM|LINK
Thanks, I think. I know WCF isn't RESTful by default. However, there are many articles (I can't find onhe that fits me) that accomplish what I want. I mean, this code DOES work back and forth to my iOS app, which is what I really need. I just want to know why the URITemplate part isn't working...
I'll take a look at your link...forgive me if your answer seems.. like a near-zero effort response to what I'm looking for just so you can get your 'check as answer' rating+1. (If I'm wrong correct me and I'll concede my apologies.)
This code works to an ios APP via SOAP calls and there are many articles online on how to do exactly what I'm trying to do. The issue really has been differences between 3.5, 4, and 4.5 WCF tutorials having mixed versions.
Simple question, why doesn't the URITemplate work?
Isn't there something in the code I've written that you can tell isn't setup/written properly?
Consultant
www.beaudamore.com
Careed
All-Star
18774 Points
3637 Posts
Re: Help with newbie WCF developer
Dec 19, 2012 12:43 AM|LINK
Thinking of some different.....so.....
Try switching from the WebInvoke attribute to the WebGet attribute. If this doesn't work, try defining the GetAllEmployees method as IEnumerable<Employee> in place of List<Employee>.
"The oxen are slow, but the earth is patient."
Beau_Damore
Member
120 Points
305 Posts
Re: Help with newbie WCF developer
Dec 19, 2012 04:46 PM|LINK
Neither worked...
according to this http://stackoverflow.com/questions/5995425/http-400-bad-request-when-calling-wcf-service-operation there is still a config error and I'm doing nearly exactly the same thing, but they seemed to have communicated directly to finish and/or just not finished the post.
I feel like its something very small and simple to change to make it work in a browser... this is frustrating. However, at least I AM getting communication directly to my iOS client... but I thought this is suposed to work this way also, otherwise what is the URITemplate for since my calls to WCF are more SOAP-like...?
Consultant
www.beaudamore.com
Careed
All-Star
18774 Points
3637 Posts
Re: Help with newbie WCF developer
Dec 20, 2012 04:12 AM|LINK
In your config file, you have the contract in your endpoint as "WcfService3.GetEmployees". You, however, have an interface marked as the ServiceContract, so your contract should be "WcfService3.IGetEmployees".
"The oxen are slow, but the earth is patient."
Beau_Damore
Member
120 Points
305 Posts
Re: Help with newbie WCF developer
Dec 20, 2012 05:18 PM|LINK
I had juuuuust figured that out by looking at another example noticed teh same thing...always one darn char isn't it?....
Thanks so much.
oh, almost forgot, also had to have a "/" in the address attribute too.
Consultant
www.beaudamore.com