WCF WebService Not Returning DataTable - wcf

I am beginning to use WCF to write a web service instead of the old asp.net asmx way. I want to be able to return JSON so I can't do it the old way. If I am returning a string or int the Web Service works fine but if I try to return a DataTable it becomes unresponsive and I get nothing. I have tried debugging to see where it blows up and it won't even stop on a breakpoint.
My Class file looks like this:
public string XMLData(string id)
{
return "You requested " + id;
}
public string JSONData(string id)
{
return "You requested " + id;
}
public DataTable TestDT()
{
DataTable Testing = new DataTable("TestDT");
return Testing;
}
My interface file looks like this:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "testdt/")]
DataTable TestDT();
The json method as well as the xml method works perfectly fine. When I call TestDT I get a standard IE Page showing no connectivity. I have tried it with data or no data in the datable with the same results.
One other note here: when I run the WCF service locally (Hitting PLAY) my test application shows now services I see this:
How can I get this to work with a datable?
Edit#1: I actually resolved the issue with the data not returning. I finally gave up on trying to return a dataset and instead I created a jagged array and ended up return it as JSON. I'll post it later as an answer so people know how I did it. What I am more interested in knowing now is part 2. Why is my WCF Test Client not showing any methods (see image attached) My hunch is its related to the web.config so I am posting that below:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="DBDjuggleWS" connectionString="Data Source=localhost;Initial Catalog=PrayUpDev;Persist Security Info=True;User=DjuggleMaster;Password=DJPassword!" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name ="PrayUpService.PrayUp" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="PrayUpService.IPrayUp" behaviorConfiguration ="web"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 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="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

you shoud return DataSet instead of DataTable but Returning data sets from web services is not typically considered a “good practice”. here you can read about it:
Why returning dataset or data table from WCF service is not a good practice? What are the Alternatives?
i highly recommend use the DataSet’s methods to get the data in XML format and pass the XML string instead of the DataSet itself to the service.
PassDataSet(dsDataSet.GetXmlSchema(), dsDataSet.GetXml())

Related

WCF REST Failed Network Error when writing directly to HttpResponse

I am creating a WCF REST webservice that will be used to download large files (potentially more than 2GB). The files are csv files that are not stored on a hard drive, instead they are formed in runtime when the request is made. Because of that I chose to write csv file directly into HttpResponse avoiding any intermediate containers that will not hold more than 2GB of data. This is a test code I wrote that does what I've described:
[ServiceContract]
public interface IDownloadService
{
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "")]
void Download();
}
public class DownloadService : IDownloadService
{
public void Download()
{
var response = HttpContext.Current.Response;
response.Clear();
response.ContentType = "application/csv";
response.AddHeader("Content-Disposition", "attachment; filename=myfile.csv");
//This is a small test csv file, it will be replaced with a big file,
//that will be formed in runtime and written piece by piece into Response.OutputStream
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes("1,2,3"));
response.Flush();
response.Close();
response.End();
}
}
This is my Web.config, just in case:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="RestWcf.DownloadService">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="RestWcf.IDownloadService"/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
Now here's the interesting part when I call this webservice method from Google Chrome, first it starts downloading the file and at the end of the downloading Chrome gives me the error Failed - Network error, when I try to call it from Postman I get Could not get any response message with no response generated, finally when I try to call it from Fiddler I get 504 response with more informative message ReadResponse() failed: The server did not return a complete response for this request. Server returned 384 bytes.
Any ideas on what's going on here and how can I fix this weird behavior?

WCF Rest service not working

ILeaveManagement class
[ServiceContract]
public interface ILeaveManagement
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "get")]
List<ServiceReference1.LeaveRequest> GetLeaveDetails();
}
LeaveManagement class
public class LeaveManagement : ILeaveManagement
{
public List<ServiceReference1.LeaveRequest> GetLeaveDetails()
{
try
{
var entities = new ServiceReference1.leaverequest_Entities(new Uri(serviceUrl));
var result = entities.LeaveRequestCollection;
return result.ToList();
}
catch
{
return new List<ServiceReference1.LeaveRequest>();
}
}
}
configuration
<service behaviorConfiguration="DRLExternalList.LeaveManagementBehavior" name="DRLExternalList.LeaveManagement">
<endpoint address="" binding="wsHttpBinding" contract="DRLExternalList.ILeaveManagement"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<behavior name="DRLExternalList.LeaveManagementBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
I have deployed the project in IIS 7.5. When i run the application , it is saying BadRequest.
I have verrified in fiddler. i saw 400 error.
Please help me on this.
Try using webHttpBinding in your endpoint instead of the wsHttpBinding, or add it as an additional one and change the address. I use a bindingNamespace in my project, but I don't think you need it.
<endpoint address="XMLService"
binding="webHttpBinding"
behaviorConfiguration="restXMLBehavior"
contract="DRLExternalList.ILeaveManagement">
</endpoint>
Add an Endpoint Behavior
<endpointBehaviors>
<!-- Behavior for the REST endpoint -->
<behavior name="restXMLBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
I also annotate the OperationContract slightly differently, but it shouldn't make all that much of a difference. I'll give it to you just in case...
[WebGet(UriTemplate = "/GetLeaveDetails", ResponseFormat = WebMessageFormat.Xml)]
To call the service, it would look like this using the XMLService endpoint name:
http://myWebHost.com/WebService/MyService.svc/XMLService/GetLeaveDetails
Hosting an wcf service into a website issue : System.ArgumentException: ServiceHost only supports class service types
the above link helped me to solve my issue.
<%# ServiceHost Language="C#" Debug="true" Service="restleave.ProductRESTService" %>

wcf service returns "method not allowed" error

I'm trying to pass object parameter as json format to wcf restful service.
service conratc code like this;
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
UriTemplate="PR")]
[OperationContract]
TWorkRequestPostResult PostRequest(TWorkRequestPostArgs args);
And my web.config file like this;
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- 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>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
When I trying call the service with "http://localhost/serviceurl/PR" url, Service returned "Method not allowed" error message.
Are you calling the service from the browser? If so, the browser requests the service using HTTP GET while the service method is mapped to HTTP POST, Method = "POST", thus resulting in the error "Method not allowed".
To fix, either change to Method = "GET" if it makes sense with regards to REST or try calling the service method from a tool that supports POST, e.g. Fiddler or WcfTestClient

WCF REST WebGet for user defined parameters

I have below operation contract with WebGet defined as follows.
[OperationContract]
[WebGet(UriTemplate = "UpdateUserDetails/?configdata={_userConfigData}&configresult={_configResult}&clientip={_clientIP}&adminname={AdminName}")]
public void UpdateUserDetails(UserConfigData _userConfigData, ConfigResult _configResult, string _clientIP, string AdminName)
When I run the service, I am getting below error. Any ideas how to fix this issue?
Operation 'UpdateUserDetails' in contract 'UserConfigService' has a query variable named '_userConfigData' of type Service1.WCF.UserConfig.UserConfigData', but type 'Service1.WCF.UserConfig.UserConfigData' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.
I will assume that you use Json object to request data.
it should be like this:
[OperationContract]
[WebInvoke(UriTemplate = "UpdateUserDetails?_clientIP={_clientIP}&AdminName={AdminName}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public void UpdateUserDetails(UserConfigData _userConfigData, ConfigResult _configResult, string _clientIP, string AdminName)
And JSON data seems to be like this:
{
"_userConfigData":{
"Property1":"value",
"Property2":"value",
"Property3":"value"
..and so on...
},
"_configResult":{
"Property1":"value",
"Property2":"value",
"Property3":"value"
..and so on...
}
}
There is a good application for testing Rest services, you can try to use:
Fiddler
Additional Info
In response to the result "getting Method not found"
You may not have defined the endpoint or the service address properly. Your webconfig file should have this kind of config.
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="soapBinding">
<security mode="None"></security>
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="webBinding"></binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 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>
</behaviors>
<services>
<!-- USING SOAP-->
<service behaviorConfiguration="defaultServiceBehavior" name="MyProject.WCF.UserConfig.UserConfigService">
<endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="soapBinding" contract="MyProject.WCF.UserConfig.IUserConfigService"></endpoint>
</service>
<!-- USING JSON-->
<service behaviorConfiguration="defaultServiceBehavior" name="MyProject.WCF.UserConfig.UserConfigService">
<endpoint address="json" binding="webHttpBinding" bindingConfiguration="webBinding" behaviorConfiguration="jsonBehavior" contract="MyProject.WCF.UserConfig.IUserConfigService"></endpoint>
</service>
</services>
</system.serviceModel>
The address seems like this:
SOAP
localhost:1706/soap/UserConfigService.svc
JSON
localhost:1706/json/UserConfigService.svc
For better reference you could try to watch here:
How to create simple REST Based WCF Service with JSON format
you have to use string, u cant use an object as query string prameters. it wont convert your query string to an object. those variables should be defined as string.
Here's a link on implementing a custom QueryStringConverter, which will do what you want it to. Note also (mentioned in that post) that it might be better to pass a (possibly) complicated object like UserConfigData or ConfigResult as POST data, rather than in the URL. Considering your method is called "UpdateUserDetails", it's probably best to use a POST (WebInvoke) instead of a GET (WebGet) anyway, in the spirit of REST.

WCF webHttpBinding post to apache service

I am trying to communicate with an Apache service from a WCF client
I have set up the client like this:
<client>
<endpoint name="ApacheService"
address="SomeUrl"
behaviorConfiguration="ApacheBehavior"
binding="webHttpBinding"
contract="ISomeContrect" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="ApacheBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
and my contract looks like
[OperationContract]
[WebInvoke(Method = WebRequestMethods.Http.Post,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "?user={username}&action=someaction")]
void dosomeaction(string username, List<SomeJSONSerializableObject> data);
Don't know what framework is used on the receiving side but the user and action variables are presented as get variables and the json payload is not seen at all.
debug started
post:
Array
(
)
get:
Array
(
[user] => someusername
[action] => someaction
)
json:
I did some local tests with a WCF service and it works fine.
Any ideas what ca be wrong?
Never mind. It was some error on the Apache side